Jump to content

paginating


Woodburn2006

Recommended Posts

i did this tutorial for paginating: http://www.phpfreaks.com/tutorials/43/0.php

but i cannot get the next and prev buttons to work, they appear and have 1 as a page number at the bottom but there is 3 pages. i can put ?page=3 onto the address and it moves me to page 3 but it is the actual next and prev buttons that do not work.

this is the code
[code]    if($page != 1){ 
        $pageprev = $page--;
       
        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); 
    }else{
        echo("PREV".$limit." ");
    }

    $numofpages = $totalrows / $limit; 
   
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
         
        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); 
    }else{
        echo("NEXT".$limit); 
    } [/code]

any ideas on this?
Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/
Share on other sites

Out put your variables.
I had to do that when I was working with my pagnation scripts.

I found that my $prevpage kept equalling my $page

So I changed $prevpage to be set up like
[code]
<?php
$prevpage = $page - 1;
?>
[/code]
Which is odd cause I thought $page--; would work but it didnt seem to for some reason???

Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/#findComment-72592
Share on other sites

Okay, so you're not getting your Prev and Next to show up as links, and you're not getting links to your pages printed out.

I tested your page by adding "?page=<pagenumber>" to the URL, but I'm not seeing the results change.

Can you post the code you are using to define your [b]$page[/b], [b]$totalrows[/b], and [b]$limit[/b] variables?
Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/#findComment-72599
Share on other sites

[code]  $limit  = 25;               
    $query_count    = "SELECT count(*) FROM gallery";   
    $result_count  = mysql_query($query_count);   
    $totalrows      = mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    } [/code]

the prev works now that i changed $pageprev to $page - 1;

but there are 3 pages in total and it only shows 1 page
Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/#findComment-72606
Share on other sites

Try these changes. I changed some [b]echo()[/b] formatting, added some page validation checking, and removed some redundant code.

[code=php:0]
    $numofpages = ceil($totalrows / $limit); 

    // Check that the page requested is a valid page number
    if($page < 1){
    $page = 1;
    }

    if($page > $numofpages){
    $page = $numofpages;
    }

    if($page > 1){ 
        $pageprev = $page--;
       
        echo ("<a href=\"$PHP_SELF&page=".$pageprev."\">PREV".$limit."</a> "); 
    }else{
        echo ("PREV".$limit." ");
    }
   
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=".$i."\">$i</a> ");
        }
    }

    if($page < $numofpages){
        $pagenext = $page++;
         
        echo("<a href=\"$PHP_SELF?page=".$pagenext."\">NEXT".$limit."</a>"); 
    }else{
        echo("NEXT".$limit); 
    }
[/code]

Let us know how this works.
Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/#findComment-72612
Share on other sites

[quote author=SharkBait link=topic=103739.msg413329#msg413329 date=1155229573]
Out put your variables.
I had to do that when I was working with my pagnation scripts.

I found that my $prevpage kept equalling my $page

So I changed $prevpage to be set up like
[code]
<?php
$prevpage = $page - 1;
?>
[/code]
Which is odd cause I thought $page--; would work but it didnt seem to for some reason???

[/quote]

The reason that doesn't work is because what it actually does is assigns the value in $page to $prevpage, and then decrements $page. So if you started with $page = 2, you would end up with $prevpage = 2 and $page = 1.  Not what you wanted.
Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/#findComment-72614
Share on other sites

still wont work.

when i changed the code you said all it would give me is the first page and there was no other pages when i typed ?page=2 or 3.

so i removed this code and i could get to the 2 next pages:

[code]    if($page < 1){
    $page = 1;
    }[/code]

but i still cant get it to work

this is the full code:

[code]<?php

    @mysql_connect(localhost, <USER>, <PASS>) or die("ERROR--CAN'T CONNECT TO SERVER");
    @mysql_select_db(<DB>) or die("ERROR--CAN'T CONNECT TO DB");

    $limit          = 25;               
    $query_count    = "SELECT count(*) FROM gallery";   
    $result_count  = mysql_query($query_count);   
    $totalrows      = mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    }
       

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM gallery LIMIT $limitvalue, $limit";       
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#E0E0E0"; // light gray

    echo("<table>");
   
    while($row = mysql_fetch_array($result)){
        if ($bgcolor == "#E0E0E0"){
            $bgcolor = "#FFFFFF";
        }else{
            $bgcolor = "#E0E0E0";
        }

    echo("<tr bgcolor=".$bgcolor."><td>");
    echo($row["name"]);
    echo("</td></tr>");
    }

    echo("</table>");

    $numofpages = ceil($totalrows / $limit); 

    // Check that the page requested is a valid page number


    if($page > $numofpages){
    $page = $numofpages;
    }

    if($page > 1){ 
        $pageprev = $page - 1;
       
        echo ("<a href=\"$PHP_SELF&page=".$pageprev."\">PREV".$limit."</a> "); 
    }else{
        echo ("PREV".$limit." ");
    }
   
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=".$i."\">$i</a> ");
        }
    }

    if($page < $numofpages){
        $pagenext = $page + 1;
         
        echo("<a href=\"$PHP_SELF?page=".$pagenext."\">NEXT".$limit."</a>"); 
    }else{
        echo("NEXT".$limit); 
    }

   
    mysql_free_result($result);

?>
[/code]

i removed the user and pass for security  ;)

Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/#findComment-72616
Share on other sites

here's the trouble

[code]
    $limit          = 25;               
    $query_count    = "SELECT count(*) FROM gallery";   
    $result_count  = mysql_query($query_count);   
    $totalrows      = mysql_num_rows($result_count);
[/code]
because you are selecting COUNT(*), the result of this query will only have one row.

do this-
[code]
$row=mysql_fetch_array($result_count, MYSQL_ASSOC);
$totalrows=$row['COUNT(*)'];
[/code]
Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/#findComment-72636
Share on other sites

The method posted by [b]bltesar[/b] will work if you make change to your query:

[code=php:0]             
$query_count    = "SELECT count(*) AS rowcount FROM gallery";   
[/code]

and change the [b]$totalrows[/b] to read this:

[code=php:0]
$totalrows=$row['rowcount'];
[/code]

so your query code would read like this:

[code=php:0]           
    $query_count = "SELECT count(*) AS rowcount FROM gallery";   
    $result_count = mysql_query($query_count);   
    $row = mysql_fetch_array($result_count, MYSQL_ASSOC);
    $totalrows = $row['rowcount'];
[/code]

also, your code for printing a [b]Prev[/b] link will not work, because you have a [b]&[/b] where you need a [b]?[/b]. Change this line:

[code=php:0]
        echo ("<a href=\"$PHP_SELF&page=".$pageprev."\">PREV".$limit."</a> ");
[/code]

to this:

[code=php:0]
        echo ("<a href=\"$PHP_SELF?page=".$pageprev."\">PREV".$limit."</a> ");
[/code]
Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/#findComment-72654
Share on other sites

What I originally wrote should work fine, except that these things are case sensitive. 

The query should be SELECT COUNT(*)...


Using that with totalrows=$row['COUNT(*)'] is preferred over just doing a SELECT *... because the latter approach will take up more resources because it actually gets all the contents of all the rows.
Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/#findComment-72657
Share on other sites

And then depending on how you want to sort them it can be either ASC (ascending) or DESC (descending)

[code=php:0]
$query = "SELECT * FROM gallery ORDER BY name DESC LIMIT $limitvlaue, $limit";

// OR

$query ="SELECT * FROM gallery ORDER BY name ASC LIMIT $limitvalue, $limit";
[/code]

=-)
Link to comment
https://forums.phpfreaks.com/topic/17150-paginating/#findComment-72752
Share on other sites

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.