GreenSmurf Posted December 8, 2009 Share Posted December 8, 2009 while ($mycheck = mysql_fetch_array($searchresult2)){ $checkID = $mycheck["id"]; while ($mytest = mysql_fetch_array($result2)){ $testID = $mytest["id"]; if ($testID != $checkID){ $nume++; echo "<br />".$checkID." == ".$testID."<br /><br />"; } } } I have an interesting bug that seems to not want to go away no many how many times I recode this. The output looks something like this: 40 == 1600 40 == 1464 40 == 1332 40 == 1567 40 == 1364 40 == 1335 40 == 1484 40 == 1501 ... One part of the problem is that the '40' never changes but I am sure that is related to whatever I am overlooking. It is supposed to check if the IDs from the database match and if so then it is to add an interval to the $nume variable. The reason for the two while loops if because it is check two entirely different queries from a mySQL database. Any help would be appreciated. -GreenSmurf Quote Link to comment https://forums.phpfreaks.com/topic/184333-id-check-in-while-loop-bugged/ Share on other sites More sharing options...
cags Posted December 8, 2009 Share Posted December 8, 2009 The cause of this problem seems practically identical to the last thread I replied to :-\ with the code you've provided suggesting an accurate solution will be difficult, so instead I'll offer you the percieved reason for your problem. Upon entering the outter loop it will set $checkID then enter the inner loop. It will then run this loop untill mysql_fetch_array($result2) runs out of rows to return from the resultset. In theory if you are coding your queries correctly, the output would be correct for the first value of $checkID. For every other iteration of the outer loop the inner loop will not be entered because mysql_fetch_array($result2) will be returning FALSE due to the fact the pointer will be at the end of the resource. The solution I suggested to the previous user was to attempt to use mysql_data_seek to move the pointer back to the start of the dataset. I offer the same disclaimer as I did to them though, I've never used the function so I can't say 100% it will work, but it seems right. Quote Link to comment https://forums.phpfreaks.com/topic/184333-id-check-in-while-loop-bugged/#findComment-973172 Share on other sites More sharing options...
GreenSmurf Posted December 8, 2009 Author Share Posted December 8, 2009 Thank you. I am looking into it now. I am a little unsure how to institute it at this time but I am sure some reading and examples will help. -GreenSmurf Quote Link to comment https://forums.phpfreaks.com/topic/184333-id-check-in-while-loop-bugged/#findComment-973826 Share on other sites More sharing options...
premiso Posted December 8, 2009 Share Posted December 8, 2009 Show us your queries, it may be possible to do what you want just within a single query, unless the point of this is to do it with PHP. Quote Link to comment https://forums.phpfreaks.com/topic/184333-id-check-in-while-loop-bugged/#findComment-973827 Share on other sites More sharing options...
GreenSmurf Posted December 9, 2009 Author Share Posted December 9, 2009 This solved it. Thanks cags! while ($mycheck = mysql_fetch_array($searchresult2)){ $checkID = $mycheck["id"]; mysql_data_seek($result2,0); $addup = true; while ($mytest = mysql_fetch_array($result2)){ $testID = $mytest["id"]; if ($testID == $checkID){ $addUp = false; break; //echo "<br />".$checkID." != ".$testID."<br /><br />"; } } if ($addUp == true){ $nume++; } } The next thing I have been working on it to create a list of IDs from the table to compare and skip later in the display process but that seems to not be going well. Originally, I simply changed the code to have this code: if ($addUp == true){ $nume++; $skip[$x] = $checkID; $x++; } below the second loop. But I am having trouble with this. The idea is to run a loop later that will check IDs from the query that should be skipped. Since I seem to be completely array illiterate in PHP I have this code right now: while ($i < count($skip)){ $myrow = mysql_data_seek($result,$i); $id = $myrow["id"]; if ($skip[$i] != $id){ ... } } Any more help would be great. Thank you. -GreenSmurf Quote Link to comment https://forums.phpfreaks.com/topic/184333-id-check-in-while-loop-bugged/#findComment-973842 Share on other sites More sharing options...
GreenSmurf Posted December 11, 2009 Author Share Posted December 11, 2009 The next thing I have been working on it to create a list of IDs from the table to compare and skip later in the display process but that seems to not be going well. Originally, I simply changed the code to have this code: if ($addUp == true){ $nume++; $skip[$x] = $checkID; $x++; } below the second loop. But I am having trouble with this. The idea is to run a loop later that will check IDs from the query that should be skipped. Since I seem to be completely array illiterate in PHP I have this code right now: while ($i < count($skip)){ $myrow = mysql_data_seek($result,$i); $id = $myrow["id"]; if ($skip[$i] != $id){ ... } } Any more help would be great. Thank you. -GreenSmurf Some new code: if ($testID == $checkID){ $addUp = false; //echo "<br />".$checkID." == ".$testID."<br /><br />"; $skip[$x] = $checkID; $x++; break; } } if ($addUp == true){ $nume++; } and if ((sizeof($skip) == 0 || !in_array($id, $skip)) || $searchdisplay == false){ Fixed it. Must have been a very long day when I wrote that code. I have no idea what I was thinking. This new code solved it though. -GreenSmurf Quote Link to comment https://forums.phpfreaks.com/topic/184333-id-check-in-while-loop-bugged/#findComment-975590 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.