Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination in PHP (paginate comments)

Tags:

php

pagination

I watched video tutorial about pagination in PHP. Author used 4th version of PHP in his tutorial. Now, I want to paginate my comments page. I used code which I learned in tutorial. Code like this:

$limit = 10;
$page = $_GET['page'];
if(($page='') or !is_numeric($page)){
    $page = 1;
}

$total_posts = mysql_num_rows(mysql_query("SELECT * FROM comments"));
$total_pages = ceil($total_posts / $limit);
$start = ($page-1)*$limit;

$query = mysql_query("SELECT * FROM comments ORDER BY likes DESC LIMIT $start,$limit");
while($write = mysql_fetch_array($query)){
    $id      = $write['id'];
    $comment = $write['comment'];
    $likes   = $write['likes'];

    echo $id . ' ' . $comment . ' ' . $likes . '<br>' ;
}

But, when I paginate pages, the content of page doesn't change. For example, the comments which in "comments.php?page=1" is the same as "comments.php?page=2".

Does it depend on version of PHP? (because, this code worked in video tutorial) Please, help me to find where is problem.

like image 377
John Avatar asked Jan 29 '26 06:01

John


1 Answers

or works much more differently from ||

Try using || instead

ALSO

You are setting $page = '' on every page load no matter what. so it is always page 1

if(($page='') or !is_numeric($page)){ //!!!ahhh
    $page   = 1;
}

Change that to:

if(($page == '') || !is_numeric($page)){
   $page = 1;
}
like image 163
Naftali Avatar answered Jan 30 '26 19:01

Naftali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!