简体   繁体   中英

Pagination script last page

I created php script following the tutorial, but it has a mistake. It displays in the last page information which is in the previous page - it's because $perpage. How can I display only data which wasn't display yet.

EXAMPLE - If I set $perpage to 3 and I have 7 records (named 1,2,3,4,5,6,7) on page one is 1,2,3 on page two is 4,5,6 and on the last page is 5,6,7 (I want to display only record 7)

$query = mysql_query("SELECT ID FROM clanek"); 
$count = mysql_num_rows($query);

$perpage = 3; // řádků na stránku
$pages_count  = ceil($count / $perpage); //celkem stránek zaokrohleno
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$is_first = $page == 1; //první stránka
$is_last  = $page == $pages_count; 
$prev = max(1, $page - 1); 
$next = min($pages_count , $page + 1); /

$data = mysql_query("SELECT DATE_FORMAT(datum_pridani_c,'%d.%m.%Y %H:%i:%s')as datumcas,nazev,kratky_popis,ID FROM clanek ORDER BY datum_pridani_c DESC LIMIT ".($page - 1).", ".$perpage);  /

while ($zaznam = mysql_fetch_array($data)) {

//some info her
}

if($pages_count>0) {
if(!$is_first) { 
echo '<a class="predchozistranka" href="index.php?page='.$prev.'">Předchozí</a>';
}
echo '<span class="stranka">Stránka '.$page.' / '.$pages_count.'</span>';
if(!$is_last) { 
echo '<a class="dalsistranka" href="index.php?page='.$next.'">Další</a>';
}
}
$data = mysql_query("SELECT DATE_FORMAT(datum_pridani_c,'%d.%m.%Y %H:%i:%s')as datumcas,nazev,kratky_popis,ID FROM clanek ORDER BY datum_pridani_c DESC LIMIT ".($page - 1).", ".$perpage); 

It has been a while since I have used MySQL but I fired up my Workbench just now and I see that the syntax for LIMIT is LIMIT offset,rowcount. The doc says so too http://dev.mysql.com/doc/refman/5.0/en/select.html

For your query to work then, instead of ($page - 1), $perpage , it should be ($page - 1)*$perpage, $perpage

$query = mysql_query("SELECT ID FROM clanek"); 
$count = mysql_num_rows($query);

Irrelevant but the above code is highly inefficient for getting the total number of rows. It would be better if you use SELECT count(id) FROM clanek

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM