Help with PHP pagination

Discussion in 'PHP' started by saadi123, Jul 19, 2010.

  1. #1
    I uploaded my blog using PHP and MYSQL. The link to it is
    http://saadi.000a.biz

    It's in its very raw form so I still have to work a lot on it.
    I require some help regarding pagination. I want to paginate my blog with 3 posts per page. The source code of my blog is as follow.

    <?php
    include("mysql_connect.php"); // File establishing connection with database

    mysql_select_db("db_name", $con_mysql_db); // Selecting Database

    $result = mysql_query("SELECT post_title, post_body From posts ORDER BY post_title DESC");
    while($row = mysql_fetch_array($result))
    {
    echo("<h3>" .$row[0] ."</h3><hr style='color: brown'><br/> .$row[1]" ."<hr style='width: 80%; align: center; color: brown'><br/><br/><br/>");
    }
    ?>

    I hope everything is clear. So please tell me about the PHP code for pagination so that I do not have to amend this code greatly. I tried a code on it but it is not working. The code I used is as follow:

    mysql_connect("database address", "username", "password") or die(mysql_error());
    mysql_select_db("databse name") or die(mysql_error());

    if (!(isset($pagenum)))
    {
    $pagenum = 1;
    }
    $data = mysql_query("SELECT post_title, post_body From posts ORDER BY post_title DESC") or
    die(mysql_error());
    $rows = mysql_num_rows($data);
    $page_rows = 4;
    $last = ceil($rows/$page_rows);

    if ($pagenum < 1)
    {
    $pagenum = 1;
    }
    elseif ($pagenum > $last)
    {
    $pagenum = $last;
    }

    $max = 'limit' .($pagenum -1) * $page_rows .',' .$page_rows;
    ?>

    When I run this code, A blank page is appearing, which definitely means an error in the code. So please tell me what's the point that I am missing!!!???
     
    saadi123, Jul 19, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    $row is usually contains associative arrays - the name are the name of columns. So try this:
    
    <?php 
    include("mysql_connect.php"); // File establishing connection with database 
    
    mysql_select_db("db_name", $con_mysql_db); // Selecting Database
    
    $result = mysql_query("SELECT post_title, post_body From posts ORDER BY post_title DESC");
    while($row = mysql_fetch_array($result)) {
    $title = $row['post_title'];
    $body  = $row['post_body'];
    echo("<h3>$title</h3><hr style='color: brown'><br/>$body<hr style='width: 80%; align: center; color: brown'><br/><br/><br/>");
    }
    ?>
    
    PHP:
     
    Rainulf, Jul 20, 2010 IP
  3. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Yeah that's cool. But it still does not serve my purpose. I'm looking for pagination code.
    Anyway thanks for this code as well. :)
     
    saadi123, Jul 20, 2010 IP
  4. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #4
    Is that all the PHP code you have? It doesn't make any sense, here's my own version, I kind of rushed doing it but you should get the general idea of if you read it over.
    
    <?php
    $link = mysql_connect("database address", "username", "password") or die(mysql_error());
    mysqli_set_charset($link, "utf8") or  die(mysql_error());
    mysql_select_db("db_name", $link) or die(mysql_error()); // Selecting Database
    
    /////////
    function CalculateOffset ($pgNum, $rowsPerPage) {
       $tempOffset = $pgNum * $rowsPerPage;
       return ($tempOffset > $rowsPerPage) ? ceil($tempOffset-$rowsPerPage) : 0;
    }
       
    $pagenum = isset($_GET['p']) ? $_GET['p'] : 1;
    
    $data = mysql_query("SELECT * From posts") or die(mysql_error());
    $rows = mysql_num_rows($data);
    
    $page_rows = 4; // rows per page
    $offset = CalculateOffset ($pagenum, $page_rows);
    
    $data = mysql_query("SELECT * From posts ORDER BY post_title DESC LIMIT $offset, $page_rows") or die(mysql_error());
    //////////////
    
    while($row = mysql_fetch_array($result)) {
    $title = $row['post_title'];
    $body  = $row['post_body'];
    echo("<h3>$title</h3><hr style='color: brown'><br/>$body<hr style='width: 80%; align: center; color: brown'><br/><br/><br/>");
    }
    
    mysqli_close($link);
    ?>
    
    PHP:
    Visit blablabla.com/thatscript.php?p=2 .. etc
     
    Rainulf, Jul 20, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    Although this may not answer the OP's initial question, I'd like to further add to Rainulf's code, and remind to intval($_GET['p']) or (int) $_GET['p'] as an attempt to ensure its numeric and to prevent potential sql injection.
     
    danx10, Jul 20, 2010 IP