Forum Moderators: phranque
The missing image is in the last position on every page. I've looked at this so much that I'm nuts over it. ANY suggestions would be very welcome at this point!
Everything else is working well. Below is the chunk of script that is supposed to run the display.
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
$max_results = 30;
$array= $_SESSION['category'];
$result1=($page*$max_results);
$from =($result1-$max_results);
$result= mysql_query("SELECT DISTINCT
products.company,
products.id,
pix.image,
category.id,
COUNT(products.id) AS NUM
FROM category, pix, products
WHERE category.cat_number = '$array'
AND category.id = products.id
AND category.id = pix.id
GROUP BY category.id
ORDER BY products.id ASC
LIMIT ".$from.",".$result1) or die ('query problem');
$count = mysql_num_rows($result) or die ('Sorry, your search did not return any results. Please try another item.');
$rows = mysql_fetch_array($result) or die ('$rows problem');
?>
<table border="0" cellpadding="10">
<tr>
<?php
$i = 1;
while($rows = mysql_fetch_array($result)){
?>
<td><a href="singlePage.php?id=<?php echo $rows['id'];?>"><img src="<?php echo $rows['image'];?>"width="150"></td>
<?php
if ($i%3) {
} else {
if ($i >= $count) {
} else {
echo "</tr><tr>";
}
}
$i++;
}
?>
</tr>
</table>
<?php$i = 1;
while($rows = mysql_fetch_array($result))
{
?>
<td><a href="singlePage.php?id=<?php echo $rows['id'];?>"><img src="<?php echo $rows['image'];?>"width="150"></td>
<?php
if (($i%3 == 0) && ($i!= $count))
{
echo "</tr><tr>";
}
$i++;
}
?>
</tr>
</table>
<edit> To make the modulus operation comparison explicit (just in case my old habits bite).
[edited by: theBear at 5:10 pm (utc) on July 31, 2007]
It actually matters greatly on this line here.
if (($i%3 == 0) && ($i!= $count))
He is asking if dividing $i by 3 gives him a remainder of 0 and is checking the counter against the count of rows. With the value of $i being 1 before the start of the first loop.... then that means for the last loop it is 1 behind and so there is 1 result left because the $i will equal count one loop early.
[edited by: Demaestro at 8:25 pm (utc) on July 31, 2007]
1
2
3
</tr><tr> 3%3 == 0 and 3!= 10
4
5
6
</tr><tr) 6%3 == 0 and 6!= 10
7
8
9
</tr><tr> 9%3 == 0 and 9!= 10
10
exit while loop
</tr>
</table>
Now for giggles let us try $count = 3
1
2
3 3%3 == 0 (true) and 3!= 3 (false)
exit while loop
<tr>
</table>
<edit> We will let the owner of the question show the solution. Off by one errors are always a pita.</edit>
[edited by: theBear at 12:50 am (utc) on Aug. 1, 2007]