Current location: Hot Scripts Forums » Programming Languages » PHP » Loop items in session for Paypal form


Loop items in session for Paypal form

Reply
  #1 (permalink)  
Old 05-03-09, 06:48 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Loop items in session for Paypal form

I have a basic shopping cart script that works fine, storing items in a session. The only thing I can't get figured out is how to loop through the items in the session so that multiple items can be added into a Paypal form.

As I understand it, multiple items in the Paypal form need to be listed as "item_name_1", "item_name_2", etc. I can't figure out a for/while loop to increment the item/form names to do this. Any help is appreciated.

PHP Code:

function showCart() { 
    global 
$db
    
$cart $_SESSION['cart']; 
    
$zipcode $_SESSION['zipcode']; 
    if (
$cart) { 
        
$items explode(',',$cart); 
        
$contents = array(); 
        foreach (
$items as $item) { 
            
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1
        } 
        
$output[] = '<form action="cart.php?action=update" method="post" id="cart">'
        
$output[] = '<table class="producttable">'
    
$output[] = '<tr>'
    
$output[] = '<td class="productinfo" width="30%">Product</td>'
    
$output[] = '<td class="productinfo" width="10%">Price</td>'
    
$output[] = '<td class="productinfo" width="10%">Shipping</td>'
    
$output[] = '<td class="productinfo" width="10%">Remove Item</td>'
    
$output[] = '<td class="productinfo" width="5%">Qty.</td>'
              
$output[] = '<td class="productinfo" width="10%">Total</td>';   
    
$output[] = '</tr>'

        foreach (
$contents as $id=>$qty) { 
            
$sql 'SELECT * FROM product_details WHERE id = '.$id
            
$result $db->query($sql); 
            
$row $result->fetch(); 
            
extract($row); 

    
$output[] = '<tr>'
    
$output[] = '<td class="productinfo">'.$part.' <br/> '.$name.'<br />Shipping Weight: '.$weight.' lbs.<br/> 
Package Dimensions:<br/> '
.$length.'" x '.$width.'" x '.$height.'"</td>'
    
$output[] = '<td class="productinfo">$ '.$price.'</td>'
$service '03'
$length $length
$width $width
$height $height
$weight $weight
$dest_zip $zipcode

$getrate ups($dest_zip,$service,$weight,$length,$width,$hei  ght); 
$perc .1
$rate_changed_by $getrate $perc
$rate $getrate $rate_changed_by

$rate number_format$rate2'.'',' ); 
    
$output[] = '<td class="productinfo">$ '.$rate.' ea</td>'
    
$output[] = '<td class="productinfo"><a href="cart.php?action=delete&id='.$id.'">Remove</a></td>'
    
$output[] = '<td class="productinfo"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'
              
$product_rate $price $rate
              
$product_price = ( float ) $product_rate * ( int ) $qty
              
$product_price number_format$product_price2'.'',' ); 
              
$output[] = '<td class="productinfo">$' $product_price '</td>';   
              
$total_price += ( float ) $product_price
              
$total_price number_format$total_price2'.'',' ); 
    
$output[] = '</tr>'
    } 



    
$output[] = '<tr>'
    
$output[] = '<td colspan="6" class="producttotal">Total Product + Shipping: <strong>$'.$total_price.'</strong><br/><br/></td>'
    
$output[] = '</tr></table>'

    
$output[] = '<table><tr>'
    
$output[] = '<td colspan="6" class="productinfo">Make any changes you like to your order, then click the "Update My Cart" button.<br/><br/><button type="submit">Update My Cart</button><br/><br/></td>'
    
$output[] = '</tr></table>'
    
$output[] = '</form>'
     
   if(
$zipcode) { 
    
$output[] = '<table><tr>'
    
$output[] = '<td colspan="6" class="productinfo"><br/>'
    
$output[] = '<form action="cart.php?action=updatezip" method="post" id="cart">'
    
$output[] = 'Your ZIP Code: '.$zipcode.' <input type="text" name="zipcode" size="10"> <input type="submit" value="Update ZIP Code"><br/><br/></td>'
    
$output[] = '</tr></table>'
    
$output[] = '</form>'
$output[] = '<table><tr>'
$output[] = '<td colspan="6" class="productinfo">'
$output[] = 'If you are ready to checkout, click the button below to be transferred to <a href="http://www.paypal.com">PayPal®</a>.<br/><br/>'

// Start Paypal Form 

$output[] = '<form name="PayPal" method="post" action="https://www.paypal.com/cgi-bin/webscr">'
$output[] = '<input type="hidden" name="cmd" value="_cart"> 
  <input type="hidden" name="business" value="paypalemailaddress"> 
  <input type="hidden" name="currency_code" value="USD"> 
  <input type="hidden" name="upload" value="1">'


  
// start a loop to increment form names 

$output[] = 
    <INPUT TYPE="hidden" NAME="item_name_" VALUE="'
.$name.'"> 
    <INPUT TYPE="hidden" NAME="item_number_" VALUE="'
.$part.'"> 
    <INPUT TYPE="hidden" NAME="amount_" VALUE="'
.$price.'"> 
    <INPUT TYPE="hidden" NAME="shipping_" VALUE="'
.$shipping.'">'

// end loop 
     
$output[] = '<INPUT TYPE="hidden" NAME="total" VALUE="'.$total_price.'">'

     
$output[] = '<input name="submit" type="submit" value="Click Here To Pay Via PayPal.com"> 
</FORM>'


// End Paypal Form 

$output[] = '</td></tr></table>'

                 } elseif(!
$zipcode) { 
    
$output[] = '<table><tr>'
    
$output[] = '<td colspan="6" class="productinfo"><br/>'
    
$output[] = '<form action="cart.php?action=updatezip" method="post" id="cart">'
    
$output[] = 'Your ZIP Code is needed to calculate shipping costs before being able to make your purchase: <input type="text" name="zipcode" size="10"><br/><br/> 
              <input type="submit" value="Add ZIP Code"> </td>'

    
$output[] = '</tr></table>'
    
$output[] = '</form>'
               } 

    } else { 
        
$output[] = '<p><br/>You shopping cart is empty.</p>'
    } 
    return 
join('',$output); 
    } 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding session values into Paypal form mdhall PHP 1 05-02-09 04:03 PM
ajax checking and onsubmit issue follower JavaScript 4 10-12-08 04:45 PM
Iterating Associative arrays with foreach divya PHP 13 11-21-07 02:12 PM
Session Security (Continued from post below) nova912 PHP 1 09-05-06 10:29 AM
ASP Form, using FOR Loop dpeters ASP 3 06-21-06 08:23 AM


All times are GMT -5. The time now is 07:07 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.