Web Develop Forum: Pagination - Web Develop Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Pagination Rate Topic: ***-- 1 Votes

#1 User is offline   paulOr Icon

  • Member
  • PipPip
  • Group: Members
  • Posts: 20
  • Joined: 23-October 08
  • Location:Edinburgh, Scotland

Posted 23 October 2008 - 08:44 PM

Heres some code i made to do really easy pagination. Have fun!

CODE
<?php
    
    ## This is a very basic & simple to intergrate pagination script
    ## which will split your news / comments etc up into different pages,
    ## so that you dont have a million entries on the one page.
    
    ## written by paul fraser @ paulOr.net

    $maxitems = 5;                     // how many items shall we show at once?
    $page_num = $_GET['page_num'];     // get the current page number.
    
    if(empty($page_num)) {
        $page_num = 1;             // if we cant find the page number, lets make it #1 :)
    }
    
    $limits = ($page_num - 1) * $maxitems;
    
    // Grab everything we are needing out of the database and count everything up. yum!
    $sql = mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT ".$limits.",".$maxitems."") or die(mysql_error());
    $totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM `news` ORDER BY `id` DESC"),0);    
    $totalpages = ceil($totalres / $maxitems);
    
    // Now lets loop through it and print it all out.
    while($r = mysql_fetch_array($sql)) {
        echo "<p>".$r['title']."</p>";
    }
    
    // Create prev and next links.
    $lastpage = $page_num - 1;
    $nextpage = $page_num + 1;
    
    echo "<ul id=\"pagination\">";
    
    // if this is the first page, then kill the back page link, else lets keep it
    if($page_num == 1) {
        echo "<li><</li>";
    } else {
        echo "<li><a href=\"/news/page/$lastpage/\"><</a></li>";
    }
    
    // list all of the pages.
    for($i = 1; $i <= $totalpages; $i++) {
        if($page_num == $i) {
            // kill the link to the CURRENT page
            // and we can style it different so people know they are here.
            echo "<li class=\"active\">$i</li>";
        } else {
            echo "<li><a href='/news/page/$i/'>$i</a></li>";
        }
    }
        
    // if this is the last page, then kill the next page link, else lets keep this too.
    if($page_num == $totalpages) {
        echo "<li>></li>";
    } else {
        echo "<li><a href=\"/news/page/$nextpage/\">></a></li>";
    }
    
    echo "</ul>";

?>


More @ paulOr.net
0

#2 User is offline   Jamie Icon

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 79
  • Joined: 18-January 09
  • Gender:Male

Posted 21 January 2009 - 10:13 AM

Thank you Paul this is very helpful.

Jamie
<a href="http://www.learntodesign.net" target="_blank">Learn to design</a> - Photoshop and other webdevelopment tutorials.
<a href="http://www.lucypinderwallpapers.com" target="_blank">Lucy Pinder Wallpapers</a> - Lucy Pinder Gallery
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users