Jump to content

Recommended Posts

Hello, is it possible to loop through a multidimensional array? and get details from it and send those details somewhere else?

 

				<?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;
	//managing the cart changing the quantities
	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 products 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; //create a counter...implement further on
	$subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $_SESSION['cart'][$row['product_id']]['price'];//do some maths for the subtotals
	$total += $subtotal; //and the grand total is

	//setting table variables possible used further down for google checkout.
	$catname = $row['category_name'];
	$prodname = $row['product_name'];
	$price = $_SESSION['cart'][$row['product_id']]['price'];
	$quantity = $_SESSION['cart'][$row['product_id']]['quantity'];

	// Print the cart details.
	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\">";
/* GOOGLE CHECKOUT STILL UNDER DEVELOPMENT*/
foreach ($_SESSION['cart'] as $pid=>$v) {
foreach ($v AS $key=>$value) {
while ($count > 0){
unset ($pid);
$_GET['$pid'];

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=\"\" />
<input type=\"hidden\" name=\"item_quantity_$count\" value=\"\"/>
<input type=\"hidden\" name=\"item_currency_$count\" value=\"GBP\"/>";
$count --;
echo $key;
}
}
}
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>';
}



?>

Link to comment
https://forums.phpfreaks.com/topic/103390-is-it-possible/
Share on other sites

Whenever you ask "Is it possible to do <insert thing here>?" then the answer would in most cases be "yes, it is". So is it in this case. You just need to nest the loops.

 

E.g.

<?php
for ($i = 0; $i <= 10; $i++) {
    for ($j = 0; $i <= 10; $j++) {
        $var = $array[$i][$j];
    }
}
?>

That would loop through a 2d array.

Link to comment
https://forums.phpfreaks.com/topic/103390-is-it-possible/#findComment-529500
Share on other sites

the output of var_dump($_session) was this,

 

array(1) { ["cart"]=>  array(2) { [79]=>  array(2) { ["quantity"]=>  int(1) ["price"]=>  string(5) "49.99" } [69]=>  array(2) { ["quantity"]=>  int(1) ["price"]=>  string(5) "29.99" } } } array(1) { ["cart"]=>  array(2) { [79]=>  array(2) { ["quantity"]=>  int(1) ["price"]=>  string(5) "49.99" } [69]=>  array(2) { ["quantity"]=>  int(1) ["price"]=>  string(5) "29.99" } } }

 

now this is jibberish to me  >:( if seeing the visual basket will help solve the issue then it can be found here

http://www.jetexed.org.uk

Link to comment
https://forums.phpfreaks.com/topic/103390-is-it-possible/#findComment-529899
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.