MichelDS Posted May 1, 2012 Share Posted May 1, 2012 A WHILE loop in a FOR loop problem Script about links and sublinks from 1 table. In fact 2 questions about that. 1) In the FOR loop I have for example to do the loop 3 times. (needs to find 3 results withing the loop with if and else) When first time, he's gonna search within the WHILE loop that holds (for example) 10 records of the DB in a variable. He needs to search for rowid number 5, he found it He goes out of the WHILE loop and goes searching again but now for rowid number 2 Now it seem that the WHILE loop start searching for number 2 but won't start from his first row in the variable (10 rows from the db ). He start at row number 6 instead of beginning all over again !!! -> so the order of the numbers of rows MUST be in an ascending order, ortherwise he won't find them all ! But the numbers I have are NOT in an ascending order !!! Why doesn't the WHILE loop begin again from his row 1 in the variable ? FOR loop { WHILE loop { content here, break; } } 2) even when I get all the result in an ascending order, he won't go doing a second search but with a different rowid number that he gets from the FOR loop. It is giving me back indeed the right new rowid number, but the WHILE loop is doing nothing. I have put many echo "..."; and other stuff for checking but can't find the cause ! Here's the code : // $ResultShow = 10 rows from mysql db if(isset($ResultShow)){ if (mysql_num_rows($ResultShow) >= 1){ //$linksuborder = "2,3,4"; $array1 = explode(",", $linksuborder); sort($array1); for ($n = 0; $n < count($array1); $n++){ // searchin for the right row id in the variable $ResultShow where we find the numbers for next loop $r1 = each($array1); While($Row2 = mysql_fetch_array($ResultShow)) { if($Row2["linkid"] == $r1['value']) // found it, now look for the numbers that we'll put in an array { echo "---here the content---"; // linksuborder is where numbers from row id's are stored like 5,2,8 //now put them in an array $array2 = explode(",", $Row2["linksuborder"]); sort($array2); for ($n2 = 0; $n2 < count($array2); $n2++){ $r2 = each($array2); ////// here searching for the sublinks ////// While ($Row3 = mysql_fetch_array($ResultShow)) { if($Row3["linkid"] == $r2['value']) // search for the right row id within the variable $ResultShow { echo "---here the content---"; break; // found it, so no need to do the rest of the loop } // end if } //end while ////// end searching for sublinks ////// } // end for } // end if else { echo "--- content here ---"; } } // end while } // end for } //end if } // end if Table : linkid linksuborder linktitle 1 2,3,4 2 9,7,8 mainlink1 3 10 mainlink2 4 mainlink3 5 6 7 sublink3 8 sublink2 9 sublink1 10 sublink4 -> linksuborder 2,3,4 are the row id's for the mainlink -> than we put the numbers 9,8,7 (also row id's) mentioned in row linkid 2 also in a new array -> now we can search for the sublinks -> get the details from row linkid 9, than 7, than 8 -> First mainlink is compleet, now go to row linkid 3 to do it all over again for the next mainlink... RESULTS from an sorted array -> sort($linksuborder); ------------------------------------------------------------- mainlink1 : sublink1, sublink2, sublink3 mainlink2 : **no result back but it should * mainlink 3 : **no result back but it should * Quote Link to comment https://forums.phpfreaks.com/topic/261889-a-while-loop-in-a-for-loop-problem/ Share on other sites More sharing options...
MichelDS Posted May 1, 2012 Author Share Posted May 1, 2012 Table : linkid linksuborder linktitle articleid 1 2,3,4 2 9,7,8 mainlink1 3 10 mainlink2 4 mainlink3 24 5 6 7 sublink1 3 8 sublink2 12 9 sublink3 5 10 sublink4 so the result is : ---------------------- linksuborder : 2 -> a test to see if it recognize the number and yes it does linkid: 2 ->Mainlink1 (linkid: 7 ) sublink1 -> articleid: 3 (linkid: 8 ) sublink2 -> articleid: 12 (linkid: 9 ) sublink3 -> articleid: 5 linksuborder : 3 -> also recognized but no result, it just don't want to do the WHILE loop -> look for the mainlink and subliniks linksuborder : 4 -> also recognized but no result, it just don't want to do the WHILE loop -> look for the mainlink and subliniks But the question remains, why don't start the WHILE loop not to read it records from the first record in the variable everytile the loop is activated ? Why does he remember the last record where he did a BREAK; to continue from there ??? Perhaps an answer for this could solves the mystery, who klnows ! Quote Link to comment https://forums.phpfreaks.com/topic/261889-a-while-loop-in-a-for-loop-problem/#findComment-1341954 Share on other sites More sharing options...
MichelDS Posted May 2, 2012 Author Share Posted May 2, 2012 ALLRIGHT !! I found the answer myself ! And I want to share it with you ! Never knew this. Why the variable with the result from the query didn't wanted to do a second time the loop : A MySQL result resource has an internal pointer, much like a PHP array, and when you have run through it once, the pointer is at the end. You can reset the data pointer using mysql_data_seek(): THUS : mysql_data_seek($query, 0); while ($row = mysql_fetch_array($query)) { Problem solved ! Sorry guys but nobody has tried here. I found the answer at http://stackoverflow.com/questions/9698944/whilerow-mysql-fetch-arrayquery-doesnt-work-in-second-time. They deserve the credits ! Quote Link to comment https://forums.phpfreaks.com/topic/261889-a-while-loop-in-a-for-loop-problem/#findComment-1342215 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.