DeepakJ Posted July 31, 2007 Share Posted July 31, 2007 foreach ($invoicenumlist as $key => $value){ $query3 = "SELECT * FROM productid WHERE invoicenum='$value'"; $Result3=mysql_query($query3) or DIE("Could not query3"); while ($row3=mysql_fetch_array($Result3)){ echo "hello"; $productidlist[]= $row3['productid']; if ($row3['productid']!= ""){ $pidcounter=$pidcounter+1; } } } Can you see what is wrong with this loop? I tested partsof it and saw that it entered the foreach loop but not the while loop? Can someone please explain why? Quote Link to comment https://forums.phpfreaks.com/topic/62680-solved-loop-problem/ Share on other sites More sharing options...
Psycho Posted July 31, 2007 Share Posted July 31, 2007 Have you verified that the query result has records? The DIE clause will only tell you if the query failed, not if there were 0 records. Add this right before the while loop: echo "Records found: " . mysql_num_rows($Result3); Quote Link to comment https://forums.phpfreaks.com/topic/62680-solved-loop-problem/#findComment-312030 Share on other sites More sharing options...
DeepakJ Posted August 1, 2007 Author Share Posted August 1, 2007 It says 0. Wow your right. Quote Link to comment https://forums.phpfreaks.com/topic/62680-solved-loop-problem/#findComment-312878 Share on other sites More sharing options...
Psycho Posted August 1, 2007 Share Posted August 1, 2007 It says 0. Wow your you're right. Quote Link to comment https://forums.phpfreaks.com/topic/62680-solved-loop-problem/#findComment-312887 Share on other sites More sharing options...
DeepakJ Posted August 1, 2007 Author Share Posted August 1, 2007 I have another question. How could you define a multi dimensional array in another multidimensional array. $pidcounter = 0; foreach ($invoicenumlist as $key => $value){ foreach ($invoicenumlist[$key] as $key1 => $value1){ $query3 = "SELECT * FROM productid where invoicenum = '$value1'"; $Result3 = mysql_query($query3); while ($row3=mysql_fetch_array($Result3)){ $productidlist[$key][]=$row3['productid']; $pidcounter = $pidcounter+1; } } } Hopefully above will help illustrate what I am trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/62680-solved-loop-problem/#findComment-312900 Share on other sites More sharing options...
Psycho Posted August 1, 2007 Share Posted August 1, 2007 Not sure I follow. Are you wanting to define the array or iterrate through it? The code you have above is VERY bad form. You should never have nested loops to run queries such a that, it put a greal of unneccessary stress on the server. Also, this $invoicenumlist[$key] is the same as $value from the first loop. You do not need to have a variable for the key in a foreach loop and you should use meaningful names for the variables, not just $key and $value. Also, no need to use $pidcounter, just use count() any time you need to know how many records there are in an array. Here is a better way of accomplishing the above. It still has a looping query, but not two: foreach ($invoicenumlist as $invoiceKey => $invoicenums){ $query = "SELECT * FROM productid where invoicenum IN (" . implode(',', $invoicenums) . ")"; $Result = mysql_query($query); while ($row=mysql_fetch_array($Result)){ $productidlist[$key][]=$row3['productid']; } } Quote Link to comment https://forums.phpfreaks.com/topic/62680-solved-loop-problem/#findComment-313338 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.