PHP Pagination and Links

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

  1. #1
    Few days ago I posted my problem regarding PHP pagination. Thanks to some of the friends here, most of the problem has been resolved but there still some problems left. First have a look at the code:


    <body>
    <div id="main_content">

    <?php

    include("mysql_connect.php");
    mysql_select_db("dbName", $con_mysql_db);

    if (isset($_GET['dbName'])) {
    $pageno = $_GET['dbName'];
    } else {
    $pageno = 5;
    } // if
    $query = "SELECT count(*) FROM posts";
    $result = mysql_query($query, $con_mysql_db) or trigger_error("SQL", E_USER_ERROR);
    $query_data = mysql_fetch_row($result);
    $numrows = $query_data[0];


    $rows_per_page = 1;
    $lastpage = ceil($numrows/$rows_per_page);


    $pageno = (int)$pageno;
    if ($pageno > $lastpage) {
    $pageno = $lastpage;
    } // if
    if ($pageno < 1) {
    $pageno = 1;
    } // if


    $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

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

    $result = mysql_query($query, $con_mysql_db) or trigger_error("SQL", E_USER_ERROR);
    /* ... process contents of $result ... */


    if ($pageno == 1) {
    echo " FIRST PREV ";
    } else {
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
    $prevpage = $pageno-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
    } // if

    echo " ( Page $pageno of $lastpage ) ";


    if ($pageno == $lastpage)
    echo " NEXT LAST ";
    else {
    $nextpage = $pageno+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
    } // if

    ?>

    <!-- end #mainContent --></div>

    Now the problem is that FIRST and PREV links are not working at all i.e they are appearing as text but not as links.
    Secondly upon clicking the NEXT and LAST links, the URL is changing but the data or the page isn't. Which means that when I click NEXT link the url changes from saadi.000a.biz to saadi.000a.biz/index.php?pageno=2 but the data in the blog remains the same.
    Please have a look at the code and let me know that what's the problem in the code...!!!

    The link is of course saadi.000a.biz
     
    saadi123, Jul 26, 2010 IP
  2. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #2
    I just skimmed over your code and I am confused why on line 8 you have
    
    if (isset($_GET['dbName'])) {
    $pageno = $_GET['dbName'];
    ...
    
    Code (markup):
    dbName is not a variable you need to be pulling.
    It would make more sense to be
    
    if (isset($_GET['pageno'])) {
    $pageno = $_GET['pageno'];
    ...
    
    Code (markup):
     
    Narrator, Jul 27, 2010 IP
  3. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    @Narrator
    Yeah that worked. Thanks buddy!!!
     
    saadi123, Jul 27, 2010 IP
  4. sandy_joy

    sandy_joy Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi, thanks to all for sharing that code but I also Introduce Some another way after searching through search engine, I think it's too easy for understanding.
    see them Just Step:

    (Step 1)
    The first step is to include the ps_pagination.php file in your script.
    (Step Two)
    Create a ps_pagination object. The ps_pagination class constructor takes five parameters:
    a MySQL connection link
    b SQL query
    c Number of records to display per page. Defaults to 10
    d Number of pagination links to display. Defaults to 5
    e Your own custom parameters you want to be appended to pagination links
    (Step Three)
    Next, call the paginate() function. This function returns a paginated result set for the current page.
    (Step Four)
    The final step is to display the pagination links. You can use the renderFullNav() function to generate and display the links in one go or you can use individual function calls to display each link separately.
    Code we use here, I follow the following function,
    1. renderFirst – This function displays the link to the first page
    2. renderLast – This function displays the link to the last page
    3. renderNext – This function displays link to next page
    4. renderPrevious – This function displays link to previous page
    5. renderNav – Displays the page links
    6. renderFullNav – This function displays all the pagination links in one go

    for more info. visit: phpsense.com
     
    sandy_joy, Jul 27, 2010 IP
  5. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    Thanks for posting. I will see how your link can be useful to me. Perhaps the one with the "Truncate long string along word boundaries" title.
     
    saadi123, Jul 28, 2010 IP