pagination

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

pagination

Post by weasel »

ok... i found sompting talking about pagination and i would also like a thing to show 20 results per page... but i have no idea how to set the thing up...:(
can any one help me with this...

Code: Select all

<?php
include('config.php');

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

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

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM table 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.">n<td>"); 
    echo($row["users"]); 
    echo("</td>n<td>"); 
    echo($row["usersID"]); 
    echo("</td>n</tr>"); 
    } 

    echo("</table>"); 

    if($page != 1){ 
        $pageprev = $page--; 
         
        echo("<a href="$PHP_SELF&page=$pageprev">PREV".$limit."</a>&nbsp;"); 
    }else{ 
        echo("PREV".$limit."&nbsp;"); 
    } 

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


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

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

?>
help is appreciated, and it is named view.php, if that helps


and... i get this error....

Error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'table LIMIT 0, 20' at line 1
Last edited by weasel on Mon Aug 18, 2003 11:58 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Is `table` the name of your table? If so that's a reserved word so it won't work.

Mac

P.S. Could you use tags around your code - it makes it easier to read.
weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

Post by weasel »

the name of the table is maps
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

then replace `table` to `maps` :).
weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

Post by weasel »

i dont see where "table" is... ill search for it with my php editor...
weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

Post by weasel »

ohh.....
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

OK G!, this is the point where you also want to start considering the design and maintenance issues of this code. Everybody else helped you get it working, now consider the ramifications of what it is you've wrought.

What I'm really getting at is the fact that there is no abstraction in this code. The db calls are right out there in the open, as is your pagination solution. The reason this is bad is becuase it's harder to maintain. If you find a bug in how you are paginating, you touch all of that unrelated code by simple virtue of opening the file. Another problem is that if when you move on to another project and you need pagination, you have to rewrite the code. If it was an object or a small group of functions working against a collection of data, then you essentially write it only once.

I have an object and function/struct based approach to this that makes it easy relatively easy to move from project to project. But before you ask, I'm not going to tell you where they are or send them to you. I would suggest you try creating this on your own. It will make you a stronger coder. If not, check out phpclasses.org. I know of a couple of pagination classes over there that should help out.

Mine are also closely coupled with some things I'm doing for my site.

If you decided to write it as an object, keep in mind what that objects mission is: pagination. That said, there should'nt be anything else your pagination class should be responsible for. It shouldn't echo data, make db calls, make sandwiches, none of the above. What should it be responsible for? Figuring things like the limit clause for the query, number of pages, how many results to show on the last page, etc...

Cheers,
BDKR
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

BDKR is right, i myself have this as a function which i just copy to a new project when needed :D then all i have to do is to tell it the table name and any thing after the "where" part and volia 8).
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

qads can you give me this function?
Post Reply