searls03 Posted June 15, 2012 Share Posted June 15, 2012 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]; Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 15, 2012 Share Posted June 15, 2012 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. Quote Link to comment Share on other sites More sharing options...
searls03 Posted June 15, 2012 Author Share Posted June 15, 2012 Eventually I will be adding an insert query for every time something is pulled. How do I make it so that they are arrays and not strings? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 15, 2012 Share Posted June 15, 2012 How do I make it so that they are arrays and not strings? Those variables $row['category'], $row['price'], etc. will be the values from those fields for the current record. How are you expecting them to be arrays? Quote Link to comment Share on other sites More sharing options...
searls03 Posted June 15, 2012 Author Share Posted June 15, 2012 it's as it loops through the database, an array will be formed with all the records. so lets say it queries column named numbers and it makes it into an array with values "1,2,3,4,5". does this make sense? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 16, 2012 Share Posted June 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
searls03 Posted June 16, 2012 Author Share Posted June 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
trq Posted June 16, 2012 Share Posted June 16, 2012 Because on each iteration you are resetting the array to empty. Read your code. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 16, 2012 Share Posted June 16, 2012 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()); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.