Hello Everyone,

I have built session cart but I am issues iterating through it and getting the data from it I need, basically I need from the array the catergory name, product name, price and quantity but have no idea how to do it :-(, the array looks like this,

Code:

Array ( [cart] => Array ( [57] => Array ( [quantity] => 1 [price] => 249.99 ) [74] => Array ( [quantity] => 1 [price] => 69.99 ) ) )

how would I get all this data from the array using a foreach, I have no idea how to form it :-(.

Please Help Thanks

     $cart[] =  Array ("item" => 57, "quantity" => 1, => "price" => 249.99);
    $cart[] = Array ("item" => 58, "quantity" => 3, => "price" => 279.99);
    
    foreach($cart as $item){
       echo($item['item'] . "<br>" . $item['quantity'] . "<br>" . $item['price'] . "<br><br>");
    }

    Something like that?

    What you posted was a 4-dimensional array which is quite hard to work with

      Thanks for the reply, ideally i need the foreach loop to work regardless of what data is in the array would seeing the full code help? What i mean is that if the user adds a camera then I need to get those details, or if they add a book i need to get both the camera and book details as they need to be added to a form with hidden fields. If you want to see my full code just let me know?

        Oh, i see your dilemma.
        As far as i know you can't do a foreach with named array ($array['variable'])

          What would you suggest as the best course of action?

          this is my full code

          <?php
          session_start()
          ?>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
          <title>JetStore :: Shopping With Jetexed Corporation Ltd</title>
          <?php
          require_once ('include/mysql_connect.php');
          ?>
          <link href="CSS/finallayout.css" rel="stylesheet" type="text/css" />
          </head>
          
          <body>
          <div id="wrapper">
          
          <div id="header">
          <div align="right" class="headlink">
          <a href="index.php">Home</a> | <a href="login.php">Login/Register</a> | <a href="contact.php">Contact Us</a> | <a href="site.php">Site Map</a>
          
          </div>
          </div>
          
          	<div id="navigation">
          		<!--Class CURRENT must be changed for each page-->
          		<ul class="jetnav"> 
          		<li class="current"><a href="index.php"><b>Home</b></a></li> 
          		<li><a href="adobe.php"><b>Adobe Software</b></a></li>
          		<li><a href="microsoft.php"><b>Microsoft Software</b></a></li>
          		<li><a href="security.php"><b>PC Security</b></a></li>
          		<li><a href="digital.php"><b>Digital Photography</b></a></li>
          		<li><a href="mp3.php"><b>Mp3 Players</b></a></li>
          		<li><a href="books.php"><b>Books</b></a></li>
            		</ul>
          	</div>
          
          		<div id="mainwrapper">
          
          		  <div id="userlinks">
          			<div align="center">
          			<h3>Customer Login</h3>
          			</div>
          			<form name="login" action="processlogin.php" method="post">
          			  <div align="center">Email:<br />
          			    <input type="text" name="username"/>
          			    <br />
          			Password:<br />
          			<input type="password" name="pword"/>
          			<br />				
          			<input type="submit" value="Login"/>
          			<input type="button" value="Register" onclick="parent.loaction='register.php'"/>
          			  </div>
          			</form>
          			<div align="center">
          			<h3>Customer Links</h3>
          			</div>
          			<p align="center"><a href="deatils.php" target="_self"></a><img src="images/mydetails.gif" width="129" height="45" /></p>
          			<p align="center"><img src="images/mybasket.gif" width="129" height="45" /></p>
          			<p align="center"><img src="images/logmeout.gif" width="129" height="45" /></p>
          			<p align="center"><img src="images/jetexedadvert.jpg" width="200" height="200" /></p>
          		  </div>
          
          		  <div id="main">
          			<h3>Your Basket</h3>
          			<?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\">&#163;{$_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\">&#163;" . number_format ($subtotal, 2) . "</td>
          </tr>\n";
          }// End of the WHILE loop.
          
          mysql_close($dbc); // Close the database connection.
          
          // close the table, and the form.
          echo '	<tr>
          	<td colspan="4" align="right"><b>Total:<b></td>
          	<td align="right">&#163;' . 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
          } else {
          	echo '<p>Your cart is currently empty.</p>';
          }	
          ?>
          				<?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=\"text\" name=\"item_description_$count\" value=\"'$catname'\" />
          <input type=\"text\" name=\"item_name_$count\" value=\"$prodname\" />
          <input type=\"text\" name=\"item_price_$count\" value=\"$price\" />
          <input type=\"text\" name=\"item_quantity_$count\" value=\"$quantity\"/>
          <input type=\"text\" name=\"item_currency_$count\" value=\"GBP\"/>";
          $count --;	
          print_r($_SESSION);
          }
          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>";
          
          ?>
          			  </div>
          		</div>
          
          
          
          </div>
          </body>
          </html>
          
          
            Write a Reply...