next page bug..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zacky
Forum Newbie
Posts: 19
Joined: Fri Nov 29, 2002 6:08 am

next page bug..

Post by zacky »

I have a next page problem..
if there is 10, 20, 30(rows) and so on..
it makes one extra blank page.. this is a small but a tricky bug.. please help.. here is the code:

Code: Select all

$limit = "10";
if (!isset($sites) || $sites==0) {
$sql = "SELECT * FROM $table WHERE topic_id=$topicid ORDER BY reply_date LIMIT $limit";
}
else {
$l_sites = $sites*$limit;
$sql = "SELECT * FROM $table WHERE topic_id=$topicid ORDER BY reply_date LIMIT $l_sites,$limit";
}

Code: Select all

$num_result = mysql_query("SELECT * FROM $table WHERE topic_id= '" . $topicid . "'") or die (mysql_error());
$num_s = mysql_num_rows($num_result) or die (mysql_error());
@mysql_free_result($num_result);
$sitez = $num_s/$limit;
if(gettype($sitez) == "double"){
settype( $sitez, "integer");
}
if ($sitez >= "1") {
echo "Goto page: ";
for($x_sites = 0;$x_sites <= $sitez; $x_sites++) &#123;
$y_sites = $x_sites;
if($sites == $x_sites) &#123;
$y_sites++;
echo "<font color="red" size=4>$y_sites</font>\n"; &#125;
else &#123;
$y_sites++;
echo "<a href="list.php?sites=$x_sites">$y_sites</a>\n"; &#125;
&#125;
&#125;
:roll:
Last edited by zacky on Sun Jan 12, 2003 6:56 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$sitez = $num_s/$limit;
if(gettype($sitez) == "double"){
settype( $sitez, "integer");
}
can be replaced by

Code: Select all

$sitez = (int)($num_s/$limit);
then test

Code: Select all

if ($sitez >= 1) { // or just $sitez>0
without the quotes around 1, since it's a number.
The blank page probably is generated because of
for($x_sites = 0;$x_sites <= $sitez; $x_sites++) {
try

Code: Select all

for($x_sites = 0;$x_sites < $sitez; $x_sites++) {
instead.
laserlight
Forum Commoner
Posts: 28
Joined: Wed Jan 01, 2003 6:41 am

Post by laserlight »

I suspect it is a problem with the looping.

For example, on the

Code: Select all

for($x_sites = 0;$x_sites <= $sitez; $x_sites++)
part, if we test with 10 rows and a $limit = 10, then we have
$sitez = 10/10 => $sitez = 1
so

Code: Select all

for($x_sites = 0;$x_sites <= 1; $x_sites++)
this means the loop is run twice, where you would want it to run only once.
A possible solution is to use ceil() instead of typecasting to an integer, then checking for $x_sites < $sitez
While ceil() isnt required (and actually returns a double anyway), when we test again we will get

Code: Select all

for($x_sites = 0;$x_sites < 1; $x_sites++)
which means that it executes only once

at the same time, if the there was only 9 rows, we would still have it execute only once.
zacky
Forum Newbie
Posts: 19
Joined: Fri Nov 29, 2002 6:08 am

Post by zacky »

I'ts not working..

laserlight:
your code is not working.. It will only show 1 page, even when it is 37rows 8O
but I agree it is a looping problem.. :roll:

volka:
It will show one page less when the rows is over 10.. :cry:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I was certain it works properly, And still I am :D

Code: Select all

$num_result = mysql_query("SELECT count(*) FROM $table WHERE topic_id='$topicid'") or die (mysql_error()); // if $topicid is numerical remove the '
$num_s = array_pop(mysql_fetch_row($num_result)); // faster than querrying all records
$numPages = (int)($num_s/$limit);
$currentPage = $sites; // only for clearity
if ($numPages > 0)
{
	echo 'Goto page: ';
	$page = 0;
	while($page < $numPages)
	{
		if (++$page == $currentPage)
			echo '<font color="red" size="4">', $page, '</font>';
		else
			echo '<a href="list.php?sites=',$page,'">',$page,'</a>';
	}
}
zacky
Forum Newbie
Posts: 19
Joined: Fri Nov 29, 2002 6:08 am

Post by zacky »

It's not working :cry:
the last page is still blank.. It's just the same as it was before..
the only thing that is new is that the code is different :oops:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe the code for displaying the pages is wrong.
Your parameter sites is of [1..n]. Therefor if you query

Code: Select all

$sql = "SELECT * FROM $table WHERE topic_id=$topicid ORDER BY reply_date LIMIT $l_sites,$limit";
without decrementing sites by 1 you get all results shifted by one page, e.g. sites = 2 --> SELECT * FROM $table WHERE topic_id=$topicid ORDER BY reply_date LIMIT 20,10 although you wanted the content of page 2 which is LIMIT 10,10 ;)
Post Reply