Jump to content

Iteration...


s_ainley87

Recommended Posts

Hello,

 

Could some one tell me how I could iterate through this array, i need to iterate through the array and every time there is a new product to create the appropiate hidden fields.

 

my current code is this....

 

<?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\">£{$_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.

// 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
} 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=\"hidden\" name=\"item_description_$count\" value=\"{$row['category_name']}\" />}
<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\"/>";
$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>";

echo $row;
?>
			</div>
	</div>



</div>
</body>
</html>

 

the array is in a session that look like this

} Array ( [cart] => Array ( [79] => Array ( [quantity] => 1 [price] => 49.99 ) [57] => Array ( [quantity] => 1 [price] => 249.99 ) ) ) } Array ( [cart] => Array ( [79] => Array ( [quantity] => 1 [price] => 49.99 ) [57] => Array ( [quantity] => 1 [price] => 249.99 ) ) )[/code=][/code]

Link to comment
https://forums.phpfreaks.com/topic/102882-iteration/
Share on other sites

Warning: Invalid argument supplied for foreach() in /home/jetexed/public_html/store/basket.php on line 171

} Array ( [cart] => Array ( [79] => Array ( [quantity] => 1 [price] => 49.99 ) [69] => Array ( [quantity] => 1 [price] => 29.99 ) ) )

Warning: Invalid argument supplied for foreach() in /home/jetexed/public_html/store/basket.php on line 171

} Array ( [cart] => Array ( [79] => Array ( [quantity] => 1 [price] => 49.99 ) [69] => Array ( [quantity] => 1 [price] => 29.99 ) ) )

 

 

Above are the errors I am getting, I do not understand this.

Link to comment
https://forums.phpfreaks.com/topic/102882-iteration/#findComment-527022
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.