Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/184333-id-check-in-while-loop-bugged/
Share on other sites

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.

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.