Jump to content
xisto Community
djXternal

Need Help Debugging

Recommended Posts

Ok guys I have a small loop for cycling through mysql entries and it is having problems somewhere that it will not get the last entry in the db.... What should happen during this section of the script is it should cathc 12 entries with P as the "Type" and then one entry at the end with a "Type" of M... But for some reason it only catches the 12 P's and not the one M

I cannot not find the problem so I'm hoping for someone with a little more experience can find the problem.

Thanks

$counter = 0;while($row = mysql_fetch_array($result))   {	  if ($counter != 4)		 {	if ($row["Type"] == P)				   {					  echo "<td><a href='" . $row["Location"] . "'><img src='http://fjor.homeip.net" . $row['Location'] . "' width='175px' border='0'></a></td>";				   }	else				   {		  echo "<td><a href='dispMOV.php?file=" . $row["Location"] . "'><img src='media/movie.jpg' width='175px' border='0'></a></td>";		   			}	$counter++;		 }	  else		 {	echo "</tr><tr>";				$counter = 0;		 }}

Edited by djXternal (see edit history)

Share this post


Link to post
Share on other sites

Let's translate it to pseudocode:

counter=0While (result = query to database) is true then	if counter not equal to 4 then		if P display location of P, else display location of M		counter=counter+1;	else 		display separator for html table.		counter=0	end ifend while

what i see from this pseudocode is that the last of each five queries won't display, else the code for the 5th lap of the while loop will set the counter to 0.

For displaying the whole content of the database i'd do:

While (result = query to database) is true then		if P display location of P, else display location of M		if  remainder(counter/4) is 3 display separators. 								counter=counter+1;end while

I'll program in another manner:
For counter=0 while (result=query to database) is true step 1 each time		if P display location of P, else display location of M		if  remainder(counter/4) is 3 display separators. end for

And this last one, translated to your script:
for($counter=0;$row = mysql_fetch_array($result));$counter++) {	if ($row["Type"] == P)	echo "<td><a href='" . $row["Location"] . "'><img src='http://fjor.homeip.net" . $row['Location'] . "' width='175px' border='0'></a></td>";	else   echo "<td><a href='dispMOV.php?file=" . $row["Location"] . "'><img src='media/movie.jpg' width='175px' border='0'></a></td>";		if($counter%4==3)  echo "</tr><tr>";		 }

Edited by DrK3055A (see edit history)

Share this post


Link to post
Share on other sites

Let's translate it to pseudocode:

counter=0While (result = query to database) is true then	if counter not equal to 4 then		if P display location of P, else display location of M		counter=counter+1;	else 		display separator for html table.		counter=0	end ifend while

what i see from this pseudocode is that the last of each five queries won't display, else the code for the 5th lap of the while loop will set the counter to 0.

For displaying the whole content of the database i'd do:

While (result = query to database) is true then		if P display location of P, else display location of M		if  remainder(counter/4) is 3 display separators. 								counter=counter+1;end while

I'll program in another manner:
For counter=0 while (result=query to database) is true step 1 each time		if P display location of P, else display location of M		if  remainder(counter/4) is 3 display separators. end for

And this last one, translated to your script:
for($counter=0;$row = mysql_fetch_array($result));$counter++) {	if ($row["Type"] == P)	echo "<td><a href='" . $row["Location"] . "'><img src='http://fjor.homeip.net" . $row['Location'] . "' width='175px' border='0'></a></td>";	else   echo "<td><a href='dispMOV.php?file=" . $row["Location"] . "'><img src='media/movie.jpg' width='175px' border='0'></a></td>";		if($counter%4==3)  echo "</tr><tr>";		 }


Thank you so much for the help, I just couldnt see the problem, possibly because I've been coding all day

But thanks again

Share this post


Link to post
Share on other sites

Ok guys I have a small loop for cycling through mysql entries and it is having problems somewhere that it will not get the last entry in the db.... What should happen during this section of the script is it should cathc 12 entries with P as the "Type" and then one entry at the end with a "Type" of M... But for some reason it only catches the 12 P's and not the one M
I cannot not find the problem so I'm hoping for someone with a little more experience can find the problem.

Thanks

$counter = 0;while($row = mysql_fetch_array($result))   {	  if ($counter != 4)		 {	if ($row["Type"] == P)				   {					  echo "<td><a href='" . $row["Location"] . "'><img src='http://fjor.homeip.net" . $row['Location'] . "' width='175px' border='0'></a></td>";				   }	else				   {		  echo "<td><a href='dispMOV.php?file=" . $row["Location"] . "'><img src='media/movie.jpg' width='175px' border='0'></a></td>";		   			}	$counter++;		 }	  else		 {	echo "</tr><tr>";				$counter = 0;		 }}
I dont completely understand your task, but, why you dont do 2 selects for this, the first one select the last 12 records of type P and then select the last record of type M, something like this:
// obtain the last 12 records of type P$result=mysql_query("select * from table where table.type='P' order by table.id DESC limit 12") or die(mysql_error());// rest of your code// obtain the last record of type M$result=mysql_query("select * from table where table.type='M' order by table.id DESC limit 1") or die(mysql_error());// rest of your code
Best regards,

Share this post


Link to post
Share on other sites

I dont completely understand your task, but, why you dont do 2 selects for this, the first one select the last 12 records of type P and then select the last record of type M, something like this:

// obtain the last 12 records of type P$result=mysql_query("select * from table where table.type='P' order by table.id DESC limit 12") or die(mysql_error());// rest of your code// obtain the last record of type M$result=mysql_query("select * from table where table.type='M' order by table.id DESC limit 1") or die(mysql_error());// rest of your code
Best regards,


Basically the script is for an picture/video album, so the numbers are not always the same, the code from DrK fixed my problem I was just too tired to see it skipping every 5

Share this post


Link to post
Share on other sites

Basically the script is for an picture/video album, so the numbers are not always the same, the code from DrK fixed my problem I was just too tired to see it skipping every 5

Ok, no problem.

Best regards,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.