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
https://forums.phpfreaks.com/topic/211241-is-this-foreach-correctpossible/
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);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.