Jump to content

Archived

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

Woodburn2006

paginating

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?

Share this post


Link to post
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 ?

Share this post


Link to post
Share on other sites
http://www.dancingledgegallery.co.uk/tester.php

that is where it is, there is about 53 entries in the DB. if you put ?page=2 or 3 at the end of the address you will see how it gets the records fine but the next prev etc dont work

thanks

Share this post


Link to post
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???

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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... ;)

Share this post


Link to post
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  ;)

Share this post


Link to post
Share on other sites
$prevpage=$page--; is equivalent to
$prevpage=$page;
$page=$page-1;

which is why you need to use:
$prevpage=$page-1;

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
finally got there, what you just said to do did not seem to work either so i just removed the count(*) and just used * and it works fine

thanks for all your help everyone

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
cool thanks

1 last thing, i want to order my results by the name field, how do i do this? i have tried this but get errors

[code]$query  = "SELECT * FROM gallery (LIMIT $limitvalue, $limit) order by 'name'"; [/code]

Share this post


Link to post
Share on other sites
[code=php:0]
$query  = "SELECT * FROM gallery ORDER BY name LIMIT $limitvalue, $limit";
[/code]

Share this post


Link to post
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]

=-)

Share this post


Link to post
Share on other sites

×

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.