s_ainley87 Posted April 30, 2008 Share Posted April 30, 2008 Hello, I currently making a shooping cart with php, the shopping cart is held in a session $_SESSION['cart'] I am using the product id's as keys (pid) when I echo $pid i get id of however many products there are in the basket so if there are two products in the cart then I would get say 56, 87 as the product ID's what I am asking is that I need to loop through these products to get all the values that the basket holds, I am sure there will be a way to say for example whil all the pid have not been accessed { //do something with the form } and keep doing that until it has accesed all the pid that are in the cart. this is the code I am using maybe I have the totally wrong idea of how to do this so if some could point me in the right direct that would be great. <?php //this page dispalys the contents of the shopping basket //this page also lets the user update the contents of their basket // Check if the form has been submitted (to update the cart). if (isset($_POST['submitted'])) { // Check if the form has been submitted. // Change any quantities. foreach ($_POST['qty'] as $k => $v) { // Must be integers! $pid = (int) $k; $qty = (int) $v; if ( $qty == 0 ) { // Delete. unset ($_SESSION['cart'][$pid]); } elseif ( $qty > 0 ) { // Change quantity. $_SESSION['cart'][$pid]['quantity'] = $qty; } } // End of FOREACH. } // End of SUBMITTED IF. // Check if the shopping cart is empty. $empty = TRUE; if (isset ($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $key => $value) { if (isset($value)) { $empty = FALSE; break; // Leave the loop. } } // End of FOREACH. } // End of ISSET IF. // Display the cart if it's not empty. if (!$empty) { require_once ('include/mysql_connect.php'); // Connect to the database. // Retrieve all of the information for the prints in the cart. $query = "SELECT category.category_name, product.product_id, product.product_name FROM category, product WHERE category.category_id = product.category_id AND product.product_id IN ("; foreach ($_SESSION['cart'] as $pid => $value) { $query .= $pid . ','; } $query = substr ($query, 0, -1) . ') ORDER BY product.product_id ASC'; $result = mysql_query ($query, $dbc); // Create a table and a form. echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center"> <tr> <td align="left" width="30%" bgcolor="#FF6600"><b>Category</b></td> <td align="left" width="30%" bgcolor="#FF6600"><b>Product Name</b></td> <td align="right" width="10%" bgcolor="#FF6600"><b>Price</b></td> <td align="center" width="10%" bgcolor="#FF6600"><b>Qty</b></td> <td align="right" width="10%" bgcolor="#FF6600"><b>Total Price</b></td> </tr> <form action="basket.php" method="post"> '; // Print each item. $total = 0; // Total cost of the order. while ($row = mysql_fetch_array ($result)) { $count = $count+1; $subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $_SESSION['cart'][$row['product_id']]['price']; $total += $subtotal; //setting table variables $catname = $row['category_name']; $prodname = $row['product_name']; $price = $_SESSION['cart'][$row['product_id']]['price']; $quantity = $_SESSION['cart'][$row['product_id']]['quantity']; // Print the row. echo " <tr> <td align=\"left\" bgcolor=\"#cccccc\">{$row['category_name']}</td> <td align=\"left\" bgcolor=\"#cccccc\">{$row['product_name']}</td> <td align=\"right\" bgcolor=\"#cccccc\">£{$_SESSION['cart'][$row['product_id']]['price']}</td> <td align=\"center\" bgcolor=\"#cccccc\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['quantity']}\" /></td> <td align=\"right\" bgcolor=\"#cccccc\">£" . number_format ($subtotal, 2) . "</td> </tr>\n"; } // End of the WHILE loop. mysql_close($dbc); // Close the database connection. // Print the footer, close the table, and the form. echo ' <tr> <td colspan="4" align="right"><b>Total:<b></td> <td align="right">£' . number_format ($total, 2) . '</td> </tr> </table><div align="center"><input type="submit" name="submit" value="Update My Cart" /> <input type="hidden" name="submitted" value="TRUE" /> </form><br /><br />'; ?> <?php echo"<form id=\"googlecheckout\" method=\"POST\" action = \"https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/453070710704027\" accept-charset=\"utf-8\">"; while (count > 0) { echo "<input type=\"hidden\" name=\"item_description_$count\" value=\"$catname\" /> <input type=\"hidden\" name=\"item_name_$count\" value=\"$prodname\" /> <input type=\"hidden\" name=\"item_price_$count\" value=\"$price\" /> <input type=\"hidden\" name=\"item_quantity_$count\" value=\"$quantity\"/> <input type=\"hidden\" name=\"item_currency_$count\" value=\"GBP\"/>"; echo $pid; $count --; } echo "<input type=\"image\" name=\"Google Checkout\" alt=\"Fast checkout through Google\" src=\"http://checkout.google.com/buttons/checkout.gif?merchant_id=453070710704027&w=180&h=46&style=white&variant=text&loc=en_US\" height=\"46\" width=\"180\"/> </form>"; } else { echo '<p>Your cart is currently empty.</p>'; } ?> sorry for the long post Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 What do you mean by "have not been accessed" But something like this? <?php foreach($_SESSION['cart'] as $prod){ echo $prod.'<br>'; } ?> Quote Link to comment Share on other sites More sharing options...
s_ainley87 Posted April 30, 2008 Author Share Posted April 30, 2008 Thanks for your help little guy, maybe i paraphased a little too much, basically what I have is $_SESSION['cart'] that holds the quantity and price of a product that is the cart, what I want be able to do is also go through the cart and pull out the product name and also the category name, and assign all for of those to variables and then and then use the variables as values in hidden form fields, and I need to do this for each product that is in the cart but I have no idea how. Also when I used you code it jsut echos ARRAY. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 Please do this, and post the results: <?php print_r($_SESSION['cart']); ?> Quote Link to comment Share on other sites More sharing options...
s_ainley87 Posted April 30, 2008 Author Share Posted April 30, 2008 Hi, the print_r function returned, Array ( [56] => Array ( [quantity] => 1 [price] => 549.99 ) ) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 Give this a try: <?php foreach($_SESSION['cart'] as $prod){ foreach($_SESSION['cart'][$prod] as $val){ echo $val.'<br>'; } } ?> Quote Link to comment Share on other sites More sharing options...
s_ainley87 Posted April 30, 2008 Author Share Posted April 30, 2008 I have just implemented this, and I got these two error messages, Warning: Illegal offset type in /home/jetexed/public_html/store/basket.php on line 162 Warning: Invalid argument supplied for foreach() in /home/jetexed/public_html/store/basket.php on line 162 line 162 is foreach($_SESSION['cart'][$prod] as $val){ Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 Im thinking this may work... Sorry about this trail and error... <?php foreach($_SESSION['cart'] as $key => $prod){ foreach($_SESSION['cart'][$key] as $val){ echo $_SESSION['cart'][$key][$val].'<br>'; } } ?> Quote Link to comment Share on other sites More sharing options...
s_ainley87 Posted April 30, 2008 Author Share Posted April 30, 2008 Hi that got rid of the error, but is is not echoing....would doing print_r or is it dumpvar work? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 <?php foreach($_SESSION['cart'] as $key => $prod){ echo 'Quantity: '.$_SESSION['cart'][$key]['quantity'].'<br>'; echo 'Price: '.$_SESSION['cart'][$key]['price'].'<br>'; } ?> Quote Link to comment Share on other sites More sharing options...
s_ainley87 Posted April 30, 2008 Author Share Posted April 30, 2008 You are a genious, I could kiss you . How would I go about getting the product name and category name as they are not help in the session.... Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 so prod name and category are not in the session? Like so: <?php foreach($_SESSION['cart'] as $key => $prod){ $query = "SELECT * FROM productTable WHERE id = '$key'"; $sql = mysql_query($query); $row = mysql_fetch_array($sql); echo 'Quantity: '.$_SESSION['cart'][$key]['quantity'].'<br>'; echo 'Price: '.$_SESSION['cart'][$key]['price'].'<br>'; echo 'P Name: '.$row['productName'].'<br>'; echo 'P Categ: '.$row['cagegory'].'<br>'; } ?> Quote Link to comment Share on other sites More sharing options...
s_ainley87 Posted April 30, 2008 Author Share Posted April 30, 2008 Thankyou so much for your help it is now working perfectly except for GoogleCheckout, but I can work that out thankyou so much. 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.