dont_be_hasty Posted March 6, 2009 Share Posted March 6, 2009 Hi, I have two mysql queries which save the results into arrays. I then need to use a for loop/loops (or a loop of some kind) to move through both these arrays. Basically i need to get the 1st result from 'array1' and run a mysql query using that result and the 1st result in 'array2', then still using the 1st result from 'array1' run the same query between it and 2nd result from 'array2' and so on till 'array2' runs out of results, then go back round the for loop and take the 2nd result from 'array1' and do the same with it. Im not very good with arrays and loops - theyre one of my reason for hating programming. Ill show you's what ive tried and failed at: Here is the code/queries i have been using to get my info from the database $query1="SELECT term_id FROM association, gene_product, term WHERE association.term_id=term.id AND association.gene_product_id=gene_product.id AND symbol='$gene1'";} $result1=mysql_query($query1) or die(mysql_error()); $row1 = mysql_fetch_array ($result1); $query2="SELECT term_id FROM association, gene_product, term WHERE association.term_id=term.id AND association.gene_product_id=gene_product.id AND symbol='$gene2'";} $result2=mysql_query($query2) or die(mysql_error()); $row2 = mysql_fetch_array ($result2); Here is the for loops i attempted: for ($i=1; $i<=$num1; $i++) { for ($i=1; $i<=$num2; $i++) { $query_d="SELECT distance FROM graph_path WHERE term1_id = '$row9[term1_id]' AND term2_id = '$row10[term2_id]'"; $result_d=mysql_query($query_d) or die(mysql_error()); $row_d = mysql_fetch_array($result_d); } } any one wondering - $num1 and $num2 is the number of results in the two arrays. When i tried to display the results in 'row_d' nothing was being displayed so ive obviously gone wrong somewhere. But hopefully you's can see what im trying to do. If anyone can help or even suggest another way of doing this it will be very much appreciated. Ive been struggling for ages now with this. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/148247-for-loops-help/ Share on other sites More sharing options...
Adam Posted March 6, 2009 Share Posted March 6, 2009 At the moment you're only saving the first result into an array. Try.. while ($row = mysql_fetch_array($result1)) { $set1[] = $row['term_id']; } And.. while ($row = mysql_fetch_array($result2)) { $set2[] = $row['term_id']; } Then to loop through them in the way you said... foreach ($set1 as $x) { foreach ($set2 as $y) { $query_d = "SELECT distance FROM graph_path WHERE term1_id = '".$set1[$x]."' AND term2_id = '".$set2[$y]."'"; $result_d = mysql_query($query_d) or die(mysql_error()); while ($row_d = mysql_fetch_array($result_d)) { $set3[] = $row_d['distance']; } } } That will store all the returned results from the query in a new array called '$set3', which you could loop through like: foreach ($set3 as $val) { print $val . '<br />'; } Hope this helps! I've not tested the code by the way! Adam Quote Link to comment https://forums.phpfreaks.com/topic/148247-for-loops-help/#findComment-778321 Share on other sites More sharing options...
dont_be_hasty Posted March 8, 2009 Author Share Posted March 8, 2009 Thanks so much for the help. Thats gotten me a good bit further. However there is a problem with displaying the 3rd array: foreach ($set3 as $val) { print $val . '<br />'; } It gave me the following error: Warning: Invalid argument supplied for foreach(). I tired changing it round to use a while loop instead and it displayed nothing. Any ideas on what is wrong with the 3rd array? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/148247-for-loops-help/#findComment-779822 Share on other sites More sharing options...
Adam Posted March 8, 2009 Share Posted March 8, 2009 It could be that there are no results returned and so $set3 isn't an array. Try adding: $set3 = array(); Just before: foreach ($set1 as $x) { foreach ($set2 as $y) { $query_d = "SELECT distance FROM graph_path WHERE term1_id = '".$set1[$x]."' AND term2_id = '".$set2[$y]."'"; $result_d = mysql_query($query_d) or die(mysql_error()); while ($row_d = mysql_fetch_array($result_d)) { $set3[] = $row_d['distance']; } } } Adam Quote Link to comment https://forums.phpfreaks.com/topic/148247-for-loops-help/#findComment-779833 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.