Jump to content

Func for pagination doesn't work


sharke

Recommended Posts

Hi guys, i created basic function for pagination but i get incorect result.

i mean this method must be outside ( $start = ($currentPage-1)*$count; )

for correct result (ORDER BY id DESC LIMIT ".$start.",".$res_per_page.")

better solution  :confused:

<?php
if (!isset($_GET['strana']) || !isnum($_GET['strana'])) { $_GET['strana'] = 0; }
$res_per_page = 1;

$result = mysql_query("SELECT COUNT(id) FROM comments");
$rows = mysql_result($result, 0);

$result2 = mysql_query("SELECT * FROM comments ORDER BY id DESC LIMIT ".$_GET['strana'].",".$res_per_page."");

if ($rows > 1) { echo pagenav($_GET['strana'], $res_per_page, $rows); }

function pagenav($start, $count, $total, $link = "") {

global $PHP_SELF;
if ($link == "") { $link = $PHP_SELF."?strana="; }

//Determine total pages
  $totalPages = ceil($total/$count);
  
  $currentPage = $start;
  if ($currentPage < 1) {
    $currentPage = 1;
  } else if ($currentPage > $totalPages) {
    $currentPage = $totalPages;
  }

$start = ($currentPage-1)*$count;

//Create page navigation section
  $pageList = array();;

  if ($currentPage > 1) {
    $pageList[] = "<a href='".$link."1'><<</a>";
    $pageList[] = "<a href='".$link.($currentPage-1)."'>< Previous Page</a>";
  }

  for($page=1; $page<=$totalPages; $page++) {
    $pageList[] = ($currentPage==$page) ? "<b>[{$page}]</b>" : "<a href='".$link.$page."'>{$page}</a>";
  }

  if ($currentPage < $totalPages) {
    $pageList[] = "<a href='".$link.($currentPage+1)."'>Next Page ></a>";
    $pageList[] = "<a href='".$link.$totalPages."'><<</a>";
  }

  $pageNav = implode(' ', $pageList);
  return "<div class='nav'>".$pageNav."</div>";

}

Link to comment
https://forums.phpfreaks.com/topic/183662-func-for-pagination-doesnt-work/
Share on other sites

I think your getting incorrect results due to this line

 

if ($rows > 1) { echo pagenav($_GET['strana'], $res_per_page, $rows); }

 

should be

 

if ($rows > 1) { echo pagenav($_GET['strana'], $res_per_page, mysql_num_rows($result)); }

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.