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 4 of 4

Thread: Php foreach

  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Php foreach

    Hi guys i just want to be able to add this form to a mysql update command
    E.g.

    mysql_query("UPDATE table SET product_quantity = '.$product_count.' WHERE product_id = '.$product_id.' ");

    for each array.

    I done some test below just to see how it is processed... i need it to add like... Q then P then Q then P.


    Im not sure how to do it, i tried adding two foreach commands inside each other although that came up with 3 times the results.
    I was trying to add two foreach in one statement so i could reference to both at the same time.

    All help would be great.

    Thanks!


    Q:13
    Q:1
    Q:1
    Q:12
    P:3
    P:5
    P:1
    P:4

    PHP Code:
    include_once('content/page_header.php'); 
        
    //POST VARS
    $quantity_post $_POST['quantity'];
    $product_post $_POST['product_id'];


    $string "";

    foreach(
    $quantity_post as $quantity){
        
    $string .= "Q:".$quantity."<br/>";
    }
    foreach(
    $product_post as $product){
        
    $string .= "P:".$product."<br/>";
    }

    echo 
    $string

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    63
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Not sure if understand you correctly but if you want to get the product ID and quantity in the same query you could do this. Providing the product ID and quantity are in the same sequence in both arrays.

    PHP Code:
    $quantity_post $_POST['quantity']; 
    $product_post $_POST['product_id'];

    $count count$quantity_post );

    for ( 
    $i 0$i $count$i++ ) {
        
    mysql_query('UPDATE table SET product_quantity = "' $quantity_post[$i] . '" WHERE product_id = "' $product_post[$i] . '"' );

    This code is untested and a very rough example!

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,296
    Thanks
    13
    Thanked 345 Times in 341 Posts
    this is a prime example where you would use Prepared Statements.
    PHP Code:
    try
    {
      
    // add connection details, of course
      
    $pdo = new PDO($dsn$login$password);
      
    // set error handling
      
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
      
    // prepare query
      // added bonus, you’re completely safe from SQL Injection
      
    $ps $pdo->prepare('UPDATE table SET product_quantity = ? WHERE product_id = ?');
      
    // prepare parameters
      
    $ps->bindParam(1$prodPDO::PARAM_INT);
      
    $ps->bindParam(2$quantPDO::PARAM_INT);
      
    // prepare data
      
    $data array_combine($_POST['product_id'], $_POST['quantity']);
      
    // run
      
    foreach ($data as $prod => $quant)
      {
        
    $ps->execute();
      }
    }
    catch (
    Exception $e)
    {
      echo 
    'made an oopsie!';
      
    error_log($e->getMessage());

    Last edited by Dormilich; 06-26-2013 at 06:40 AM. Reason: grammar
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    New Coder
    Join Date
    Apr 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ah thankyou David that works perfectly!

    Also thanks Dormilich, i would have tried that but i do not understand it yet, so i need to read into that at a later date.

    Thanks For Your Help Guys



    Quote Originally Posted by davidjones1990 View Post
    Not sure if understand you correctly but if you want to get the product ID and quantity in the same query you could do this. Providing the product ID and quantity are in the same sequence in both arrays.

    PHP Code:
    $quantity_post $_POST['quantity']; 
    $product_post $_POST['product_id'];

    $count count$quantity_post );

    for ( 
    $i 0$i $count$i++ ) {
        
    mysql_query('UPDATE table SET product_quantity = "' $quantity_post[$i] . '" WHERE product_id = "' $product_post[$i] . '"' );

    This code is untested and a very rough example!


  •  

    Posting Permissions

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