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
Share on other sites

Are you sure the NEXT button doesn't work?  I found a typo for the PREV button:
[code]
echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); 
[/code]

should be:
[code]
echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV".$limit."</a> "); 
[/code]

notice the & has changed to a ?
Link to comment
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
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
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
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
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
Share on other sites

Good catch, [b]king arthur[/b].

Make these changes to the code I posted:

[code=php:0]
        $pageprev = $page--;
        $pagenext = $page++;
[/code]

to this:

[code=php:0]
        $pageprev = $page - 1;
        $pagenext = $page + 1;
[/code]

and you should be good to go... ;)
Link to comment
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.