PHP, Page Number and MYSQL Result

Discussion in 'PHP' started by saadi123, Aug 1, 2010.

  1. #1
    I have a problem is that the main URL i.e saadi.000a.biz starts from page number 2. What I mean to say is that the default website address of mine displays page number 2 as its default page. I want to show page number 1 as my default page as I am showing my database result in chronological order. Please help.
    The coding is as follow:

    <?php
    
    include("mysql_connect.php");
    mysql_select_db("a000b_5538273_blog_posts", $con_mysql_db);
    
    if (isset($_GET['pageno'])) {
    $pageno = $_GET['pageno'];
    } else {
    $pageno = 5;
    } // if
    $query = "SELECT count(*) FROM posts";
    $result = mysql_query($query, $con_mysql_db) or trigger_error("SQL", mysql_error);
    $query_data = mysql_fetch_row($result);
    $numrows = $query_data[0];
    
    
    $rows_per_page = 5;
    $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;
    
    
    $result = mysql_query("SELECT post_title, post_body From posts ORDER BY post_no 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
    
    ?>
    PHP:

     
    Last edited: Aug 1, 2010
    saadi123, Aug 1, 2010 IP
  2. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #2
    Any suggestions please? Me still waiting...!!!
     
    saadi123, Aug 1, 2010 IP
  3. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #3
    hmmm... try this:
    if (isset($_GET['pageno'])) {
    $pageno = $_GET['pageno'];
    } else {
    $pageno = 1;
    }
    instead of:
    if (isset($_GET['pageno'])) {
    $pageno = $_GET['pageno'];
    } else {
    $pageno = 5;
    }


    but still not sure if it will work
     
    arpit13, Aug 2, 2010 IP
  4. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    Actually it worked. Thanks Buddy...!!!

    :)
     
    saadi123, Aug 2, 2010 IP
  5. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #5
    lol, great :)

    i was lazy and didn;t read ur whole post :p just few coding lines.

    anyways i am happy that i helped
     
    arpit13, Aug 2, 2010 IP