Jump to content

Is this foreach() correct/possible?


jj20051

Recommended Posts

I have a foreach() that I want to run and I want to check before I test it out if this is even possible or how I would do this correctly if it isn't. I have 3 arrays that all pull data from a database earlier in the script. These three all "match" so that they all belong to the same product, but they are from different queries. I want a foreach() to extract data from all 3 of them and insert that into a database until the arrays run out of data.

 

foreach (($titles_list as $title) && ($prices_list as $price) && ($services_list as $service_id)) {
mysql_query("INSERT INTO product_invoices (name, invoice_id, price, quantity, date_time, service_id) VALUES('$title', '$invoice_id', '$price', '$today_slashes', '$service_id')") or die(mysql_error());
}

 

Hopefully you see where I'm going with this.

Link to comment
Share on other sites

<?php
$titles_list = array(1,2,3);
$prices_list = array(11,22,33);
$services_list = array(111,222,333);
reset($titles_list);
reset($prices_list);
reset($services_list);
$title = current($titles_list);
$price = current($prices_list);
$service_id = current($services_list);
while ($title and $price and $service_id){
    mysql_query("INSERT INTO product_invoices (name, invoice_id, price, quantity, date_time, service_id) VALUES('$title', '$invoice_id', '$price', '$today_slashes', '$service_id')") or die(mysql_error());
    
    $title = next($titles_list);
    $price = next($prices_list);
    $service_id = next($services_list);
}
?>

Link to comment
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.