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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    173
    Thanks
    2
    Thanked 9 Times in 9 Posts

    convert forloop to foreach

    hi all

    can the below code be done with "foreach" loop

    i know how to use foreach loop with single array.

    but how to use foreach loop with 2 different arrays ?

    PHP Code:
    $ppid = array();
    $ppid $_REQUEST['updt_pid'];
    $quantity = array();
    $quantity $_REQUEST['quantity'];
    for(
    $i=0;$i<count($quantity);$i++) {
            
    $cart_update_query "UPDATE cart_table SET quantity=$quantity[$i] where product_id=$ppid[$i]";
            
    $cart_update_result=mysql_query($cart_update_query)or die(mysql_error());
            } 
    vineet
    Last edited by vineet; 01-26-2013 at 01:03 PM.

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    334
    Thanks
    2
    Thanked 48 Times in 47 Posts
    You can...but it would involve just a bit less characters of code.
    PHP Code:
    $i 0;

    foreach(
    $quantity as $key=>$value) {
            
    $cart_update_query "UPDATE cart_table SET quantity=$value where product_id=$ppid[$i]";
            
    $cart_update_result=mysql_query($cart_update_query)or die(mysql_error());
    $i++;        


  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    IMO that's far worse to look at. You're much better off either combining the arrays and using the key/value options of the foreach to represent them OR sticking with the for loop since there are two arrays.
    Foreach is honestly the worst of the lot. The only way to kill an iteration is with a break, and it is designed to work with only one array. For and [do/]while loops are much more controllable.
    You'll also need to escape this data prior to going into the query. As it sits, its open to injection. My suggestion would be to use the mysqli with prepared statements instead:
    PHP Code:
    $sQry 'UPDATE cart_table SET quantity=? WHERE product_id = ?';
    if (
    $stmt $con->prepare($sQry))
    {
        
    $stmt->bind_param('ii'$quant$prod);
        
    $aCombined array_combine($_REQUEST['updt_pid'], $_REQUEST['quantity']);
        foreach (
    $aCombined AS $prod => $quant// that above is how you should combine to use the foreach.  Can be done with your original.
        
    {
            
    $stmt->execute();
        }
        
    $stmt->close();

    Which you should consider since the mysql library is now officially deprecated.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder
    Join Date
    Jun 2008
    Posts
    173
    Thanks
    2
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by Redcoder View Post
    You can...but it would involve just a bit less characters of code.
    PHP Code:
    $i 0;

    foreach(
    $quantity as $key=>$value) {
            
    $cart_update_query "UPDATE cart_table SET quantity=$value where product_id=$ppid[$i]";
            
    $cart_update_result=mysql_query($cart_update_query)or die(mysql_error());
    $i++;        

    thanks redcoder

    for this solution

    vineet

  • #5
    Regular Coder
    Join Date
    Jun 2008
    Posts
    173
    Thanks
    2
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by Fou-Lu View Post
    IMO that's far worse to look at. You're much better off either combining the arrays and using the key/value options of the foreach to represent them OR sticking with the for loop since there are two arrays.
    Foreach is honestly the worst of the lot. The only way to kill an iteration is with a break, and it is designed to work with only one array. For and [do/]while loops are much more controllable.
    You'll also need to escape this data prior to going into the query. As it sits, its open to injection. My suggestion would be to use the mysqli with prepared statements instead:
    PHP Code:
    $sQry 'UPDATE cart_table SET quantity=? WHERE product_id = ?';
    if (
    $stmt $con->prepare($sQry))
    {
        
    $stmt->bind_param('ii'$quant$prod);
        
    $aCombined array_combine($_REQUEST['updt_pid'], $_REQUEST['quantity']);
        foreach (
    $aCombined AS $prod => $quant// that above is how you should combine to use the foreach.  Can be done with your original.
        
    {
            
    $stmt->execute();
        }
        
    $stmt->close();

    Which you should consider since the mysql library is now officially deprecated.
    hi Fou-Lu

    i dont know anything about prepared statements

    but i will definately look into combining arrays solution

    vineet


  •  

    Posting Permissions

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