What is wrong with this paging code?

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
terji
Forum Commoner
Posts: 37
Joined: Tue May 14, 2002 5:27 pm
Location: Denmark

What is wrong with this paging code?

Post by terji »

When loaded it seems alright, but when you press next it shows 6 results even if paging_beg and paging_end have the values 3 and 6... What is wrong here?!?!

Code: Select all

<?php
include('myCommon.php');

db_Connect();

$sql = "SELECT id FROM table";
$resultset = mysql_query($sql);
$row = mysql_fetch_row($resultset);
$total_records = $row&#1111;0];
mysql_free_result($resultset);
$paging_size = 3;

if(empty($pageid))
&#123;
	$pageid = 1;
&#125;

$paging_end = $pageid * $paging_size; 
$paging_beg = $paging_end - $paging_size;
$pageid++;

$sql = "SELECT id, heading FROM table ORDER BY start_date DESC LIMIT $paging_beg, $paging_end";
$resultset = mysql_query($sql);

while($myrow = mysql_fetch_array($resultset)) 
&#123;
	printf("%s %s <br>", $myrow&#1111;"id"], $myrow&#1111;"heading"]);
&#125;

mysql_free_result($resultset);

echo "<br> Total records: $total_records | Next pageid = $pageid | Paging start = $paging_beg | Paging end = $paging_end | -> <a href="test.php?pageid=$pageid">next</a>";
 
?>
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

well, paging end shouldnt change
change

Code: Select all

$paging_end = $pageid * $paging_size;
$paging_beg = $paging_end - $paging_size
to

Code: Select all

$paging_end = $pagingsize;
$paging_beg = ($pageid - 1) * $pagingsize
When loaded it seems alright, but when you press next it shows 6 results even if paging_beg and paging_end have the values 3 and 6... What is wrong here?!?!
well, the reson for that is, if paging_beg is 3 and paging_end is 6, what that does is it STARTS at the third record and shows 6 after that. get it?
Post Reply