Jump to content

foreach inside a while


searls03

Recommended Posts

THis isn't working right now, but how would I do a foreach loop inside a while loop.  so for every $product that is returned from the while, the foreach will run for it?

 

$sql = mysql_query("SELECT * FROM products WHERE city='$city' && package='Breaking'");

while($row = mysql_fetch_array($sql)){
$id=$row['id'];
$category = $row['category'];
$price = $row['price'];
$product = $row['product'];
$order = $row['orderable'];
foreach($product AS $key => $val) {
$product = $val;
$category1 = $category[$key];
$price1 = $price[$key];
$id1 = $id[$key];
$order1 = $order[$key];

Link to comment
Share on other sites

You simply put a foreach() within a while() loop. I think the problem is not the foreach()/while() but in what you are doing with the. The first part of the code in the while() loop is defining some variables. But you do nothing with them. Then you have a foreach() loop which also defines some variables - one of which is overwriting the previous variable "$product". But, what really makes no sense is how you are trying to define the variables in the foreach() loop. I really have no idea what you are trying to accomplish.

 

The code inside the foreach() loop is trying to define variables based upon an index of the variables defined at the beginning of the while loop

while($row = mysql_fetch_array($sql))
{
    $id=$row['id']; //This will be a string
    $category = $row['category'];  //This will be a string
    $price = $row['price']; //This will be a string
    $product = $row['product']; //This will be a string
    $order = $row['orderable']; //This will be a string
    foreach($product AS $key => $val)
    {
        $product = $val;
        $category1 = $category[$key]; //The $category var is a string, not an array
        $price1 = $price[$key]; //The $price var is a string, not an array
        $id1 = $id[$key]; //The $order var is a string, not an array
        $order1 = $order[$key]; //The $order var is a string, not an array
    }
}

 

But, those values at the beginning of the while() loop are apparently from a result set from a DB query. A record from a DB result is an array, but the values in that array will be strings. So, there is not index value for those items.

Link to comment
Share on other sites

I think I understand what you are trying to and it makes even less sense. If I understand you correctly you want to loop through the database records and create an array, e.g. $price, of all the price values from the database result set. Ok, there are a few things wrong with that:

 

1. You are not defining/setting an array. You are simply setting a single var. You should define the variable as an array $price = array(); THEN you would add the values as a new index in the array $price[] = $row['price'];

 

2. If you want to define arrays for each field in the result set then you would NOT run the foreach() inside the while() loop. You would want to run the while() loop to put all the records from the result set into arrays. Once you have all the arrays THEN you would exit the while loop and run the foreach() loop.

 

3. In the foreach() loop you are only reassigning values to those variable and not doing anything with the values and they are getting overwritten on each iteration - so the foreach() loop is pointless.

 

But, for the sake of argument, let's say you want to put the values from the DB result set into different array values and then do a foreach(0 to do something with those values. That's a waste of code. You are already iterating through the results in the while() loop. So, whatever you want to do with that data do it within the while loop. To use the while loop to dump the data into arrays only so you can do a foreach() loop is redundant.

Link to comment
Share on other sites

so something like:

$sql = mysql_query("SELECT * FROM products WHERE city='$city' && package='Breaking'");

while($row = mysql_fetch_array($sql)){
$id=array();
$category = array();
$price = array();
$product = array();
$order = array();
$id[]=$row['id'];
$category[] = $row['category'];
$price[] = $row['price'];
$product[] = $row['product'];
$order[] = $row['orderable'];
}
foreach($product AS $key => $val) {
$product = $val;
$category1 = $category[$key];
$price1 = $price[$key];
$id1 = $id[$key];
$order1 = $order[$key];

$sql = "INSERT INTO cart (price, product123, quantity, cart_id, product_id, academy, orderable, category) 





VALUES('$price1', '$product', '1', '".$_SESSION['complete']."', '$id1', '".$_SESSION['academy']."', '$order1', '$category1')";
$rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error());}

this only takes one query and inserts it.

Link to comment
Share on other sites

:facewall:

 

You know, if you were to tell us what you are trying to achieve you will save yourself and us a lot of time. You should NEVER run queries in loops. You can accomplish everything you want by running just a single query!

 

$query = "INSERT INTO cart
              (price, product123, quantity, cart_id, product_id, academy, orderable, category)
          SELECT `price`, `product`, '1', '{$_SESSION['complete']}', `id`, '{$_SESSION['academy']}', `orderable`, `category`
          FROM products
          WHERE city='{$city}' AND package='Breaking'";
$result = mysql_query($query) or die(mysql_error());

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.