Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Foreach and arrays

    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

    PHP Code:
     // -- 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:

    PHP Code:
    UPDATE `fcp_ordersSET `qty`='577' WHERE `product_id`='Array'
    UPDATE `fcp_ordersSET `qty`='277' WHERE `product_id`='Array'
    UPDATE `fcp_ordersSET `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

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

    PHP Code:
    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


  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Don't forget to use mysql_real_escape_string to prevent SQL injections. Check this out:

    PHP Code:
    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 />"


    If I'm postin here, I NEED YOUR HELP!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •