Jump to content


Photo

paginating


  • Please log in to reply
17 replies to this topic

#1 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 10 August 2006 - 04:34 PM

i did this tutorial for paginating: http://www.phpfreaks...orials/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
    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);  
    }

any ideas on this?

#2 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 10 August 2006 - 04:51 PM

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

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

notice the & has changed to a ?

#3 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 10 August 2006 - 04:57 PM

http://www.dancingle...o.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

#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 10 August 2006 - 05:06 PM

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
<?php
$prevpage = $page - 1;
?>
Which is odd cause I thought $page--; would work but it didnt seem to for some reason???



#5 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 10 August 2006 - 05:11 PM

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 $page, $totalrows, and $limit variables?


#6 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 10 August 2006 - 05:14 PM

   $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; 
    }

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

but there are 3 pages in total and it only shows 1 page

#7 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 10 August 2006 - 05:21 PM

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

    $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);  
    }

Let us know how this works.

#8 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 10 August 2006 - 05:24 PM

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

<?php
$prevpage = $page - 1;
?>
Which is odd cause I thought $page--; would work but it didnt seem to for some reason???


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.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#9 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 10 August 2006 - 05:30 PM

Good catch, king arthur.

Make these changes to the code I posted:

        $pageprev = $page--; 
        $pagenext = $page++;

to this:

        $pageprev = $page - 1; 
        $pagenext = $page + 1;

and you should be good to go... ;)

#10 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 10 August 2006 - 05:34 PM

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:

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

but i still cant get it to work

this is the full 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); 

?> 

i removed the user and pass for security  ;)



#11 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 10 August 2006 - 06:19 PM

$prevpage=$page--; is equivalent to
$prevpage=$page;
$page=$page-1;

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


#12 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 10 August 2006 - 06:24 PM

here's the trouble

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

do this-
$row=mysql_fetch_array($result_count, MYSQL_ASSOC);
$totalrows=$row['COUNT(*)'];


#13 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 10 August 2006 - 06:33 PM

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

#14 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 10 August 2006 - 06:48 PM

The method posted by bltesar will work if you make change to your query:

               
$query_count    = "SELECT count(*) AS rowcount FROM gallery";     

and change the $totalrows to read this:

$totalrows=$row['rowcount'];

so your query code would read like this:

             
    $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'];

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

        echo ("<a href=\"$PHP_SELF&page=".$pageprev."\">PREV".$limit."</a> "); 

to this:

        echo ("<a href=\"$PHP_SELF?page=".$pageprev."\">PREV".$limit."</a> ");


#15 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 10 August 2006 - 06:55 PM

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.

#16 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 10 August 2006 - 07:12 PM

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

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


#17 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 10 August 2006 - 07:16 PM

$query  = "SELECT * FROM gallery ORDER BY name LIMIT $limitvalue, $limit";


#18 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 10 August 2006 - 10:40 PM

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

$query = "SELECT * FROM gallery ORDER BY name DESC LIMIT $limitvlaue, $limit";

// OR

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

=-)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users