...

View Full Version : Foreach and arrays



graham23s
02-14-2008, 11:38 PM
Hi Guys,

iun the shopping cart form, im using [] arrays in the forms to pass to a foreach on submission to update mysql

shoppingcart.php


// -- UPDATE --//
if($_GET['action'] == 'update')
{

$product_id_to_update = $_POST['pid'];
$quantity_to_update = $_POST['q'];

foreach($quantity_to_update as $value)
{

// update query //
$queryupdate = "UPDATE `fcp_orders` SET `qty`='$value' WHERE `product_id`='$product_id_to_update'";
echo $queryupdate;
echo ("<br />");

}

}

// there is products there so display them //
print("<form action=\"shoppingcart.php?action=update\" method=\"post\">");
print("<table width=\"95%\" class=\"shop_table\" border=\"1\" bordercolor=\"#CCCCCC\" cellpadding=\"5\" cellspacing=\"0\">\n");
print("<tr>\n");
print("<td align=\"center\" class=\"shop_header\">Image</td><td align=\"center\" class=\"shop_header\">Description</td><td align=\"center\" class=\"shop_header\" >Quantity</td><td align=\"center\" class=\"shop_header\">Total Price</td><td align=\"center\" class=\"shop_header\">Delete</td>\n");
print("</tr>\n");
print("<tr>\n");

// loop //
while($row = mysql_fetch_array($resultcart))
{

$product_id = $row['product_id'];
$product_qty = $row['qty'];

// get the product information //
$queryproductinformation = "SELECT * FROM `fcp_products` WHERE `id`='$product_id'";
$resultproductinformation = mysql_query($queryproductinformation);

// make an array //
$rows = mysql_fetch_array($resultproductinformation);

// more vars //
$product_name = $rows['product_name'];
$product_price = $rows['product_price'];

// math //
$total_price = $product_price * $product_qty;

// session in avar //
$session_id = $_SESSION['id'];

// put the total quantity in mysql besides the order //
$finalupdate = mysql_query("UPDATE `fcp_orders` SET `qty_total`='$total_price' WHERE `product_id`='$product_id'");

print("<td width=\"5%\" align=\"center\"><img src=\"images/image.gif\" alt=\"Product Image\" title=\"Product Image\" /></td><td align=\"center\"><a href=\"productinformation.php?productid=$product_id\">$product_name</a></td><td align=\"center\"><input type=\"hidden\" name=\"pid[]\" value=\"$product_id\"><input type=\"text\" name=\"q[]\" size=\"5\" value=\"$product_qty\"></td><td width=\"10%\" align=\"center\">&pound;$total_price</td><td align=\"center\"><a href=\"shoppingcart.php?action=remove\"><img src=\"images/button_delete.gif\" border=\"0\"></a></td></tr>\n");

}

// get the total price due //
$queryprice = "SELECT SUM(qty_total) as `total` FROM `fcp_orders` WHERE `customer_id`='$session_id'";
$resultsprice = mysql_query($queryprice);
$r = mysql_fetch_array($resultsprice);

// vars //
$total_due = $r['total'];

// shipping //
$shipping_costs = 10;

// total owed //
$total_owed = $total_due + $shipping_costs;

// end the table //
print("<tr>");
print("<td colspan=\"5\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Update Cart\"></td>");
print("</tr>");
print("<tr>");
print("<td colspan=\"4\" align=\"right\">Sub Total -</td><td align=\"left\"><b>&pound;$total_due</b></td>");
print("</tr>");
print("<tr>");
print("<td colspan=\"4\" align=\"right\">Shipping -</td><td align=\"left\"><b>&pound;$shipping_costs</b></td>");
print("</tr>");
print("<tr>");
print("<td colspan=\"4\" align=\"right\">Total -</td><td align=\"left\"><b>&pound;$total_owed</b></td>");
print("</tr>");
print("</table></form>");

the way it is setup now it displays:


UPDATE `fcp_orders` SET `qty`='577' WHERE `product_id`='Array'
UPDATE `fcp_orders` SET `qty`='277' WHERE `product_id`='Array'
UPDATE `fcp_orders` SET `qty`='377' WHERE `product_id`='Array'

which is what i wanted so far but i need the product to be an integer rather than the array, is there a way i can pass them both through the foreach or it there a better way?

thanks guys

Graham

Fumigator
02-15-2008, 12:58 AM
Use a "for" loop instead of a "foreach" loop. Since you are assigning the index of the array numerically starting with 0 (that's what a blank [] does), then you can use an iterator to loop through each element.



for ($i = 0; $i < count($_POST['pid']); $i++) {
//example of how to access the array elements
echo "qty is {$_POST['q'][$i]} and the id is {$_POST['pid'][$i]}<br />\n"; //replace with your query code
}

Ultragames
02-15-2008, 10:07 AM
Don't forget to use mysql_real_escape_string to prevent SQL injections. Check this out:


foreach( $i=0; $i<count($_POST['q']); $i++ ) {

// update query //
$product_id_to_update = mysql_real_escape_string($_POST['pid'][$i]);
$quantity_to_update = mysql_real_escape_string($_POST['q'][$i]);
$queryupdate = "UPDATE `fcp_orders` SET `qty`='{$quantity_to_update}' WHERE `product_id`='{$product_id_to_update}'";
echo $queryupdate."<br />";

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum