djXternal 0 Report post Posted November 1, 2006 (edited) 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 MI 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 November 1, 2006 by djXternal (see edit history) Share this post Link to post Share on other sites
DrK3055A 0 Report post Posted November 1, 2006 (edited) 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 November 1, 2006 by DrK3055A (see edit history) Share this post Link to post Share on other sites
djXternal 0 Report post Posted November 1, 2006 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 dayBut thanks again Share this post Link to post Share on other sites
TavoxPeru 0 Report post Posted November 4, 2006 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 MI 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 codeBest regards, Share this post Link to post Share on other sites
djXternal 0 Report post Posted November 4, 2006 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 codeBest 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
TavoxPeru 0 Report post Posted November 7, 2006 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 5Ok, no problem.Best regards, Share this post Link to post Share on other sites