ievernova Posted May 8, 2009 Share Posted May 8, 2009 Hi, I m trying to export a cart script into paypal, for paypal to understand that ive sent multiple items i need to give each product in the array a number starting from one going up in numberical order, iv tried to use a for loop with an increment counthing variable for($counter=1;$counter>0;$coutner++) but cant get it to work with this existing code; whats the easiest way to do this?! i know how to get all of the variables and stuff into the form i need to send to paypal so i would just like to know how to output the number from a variable where it says in teh code below... i want the final output to be something like this 1 ProductA Quantity: 20 2 ProductB Quantity: 15 3 ProductC Quantity: 6 this is the point at which i output the cart before proceeding to the checkout, function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM syn_vinyl_product WHERE prod_id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$prod_title.' by '.$prod_desc.'</td>'; $output[] = '<td>£'.$price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>£'.($price * $qty).'</td>'; $output[] = '<td>////////// OUTPUTT THE NUMBER HERE /////////////</td>'; $total = ''; $total += $price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } THANKS ALOT!!! Quote Link to comment Share on other sites More sharing options...
ievernova Posted May 8, 2009 Author Share Posted May 8, 2009 the cart code <?php ########## ### saved from http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart ########## // Include MySQL class require_once('mysql.class.php'); // Include database connection require_once('global.inc.php'); // Include functions require_once('functions.inc.php'); // Start the session session_start(); // Process actions $cart = $_SESSION['cart']; $action =''; if (isset($_GET['action'])) { $action = $_GET['action']; }; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.