Welcome to WebmasterWorld Guest from 54.167.111.180

Forum Moderators: phranque

Message Too Old, No Replies

query output not displaying properly

image count is wrong

     
4:04 am on Jul 31, 2007 (gmt 0)

New User

5+ Year Member

joined:July 8, 2007
posts:4
votes: 0


I have checked, and rechecked my databases to verify that there are no blank lines. Yet, all but the final page of the output is one image short!

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>

5:04 pm on July 31, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 19, 2003
posts:804
votes: 0


Try this for the display:


<?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]

5:11 pm on July 31, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
posts:2606
votes: 0


Try:

.......
<?php

$i = 0;
.......

The first loop through it should be 0 not 1.. I think this is why you are one short.... usually the first item in a list or record set is the [0] element...[1] is the second... it looks like you are starting with the second result.

5:19 pm on July 31, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 19, 2003
posts:804
votes: 0


His use of $i is strictly a counter and shouldn't really need to start at 0 although it can.
8:23 pm on July 31, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
posts:2606
votes: 0


Bear....

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]

12:43 am on Aug 1, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 19, 2003
posts:804
votes: 0


Actually that is my line and if the number of rows is indeed the number of rows that line will cause a </tr><tr> pair to be inserted on $i = 3, 6, 9, 12 ,15, etc the and says and $i not equal to $count say count is 10

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]

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members