Debug This Code

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

  1. #1
    I recently posted a thread requiring some PHP pagination help. I manage to grab some code however some error is displaying every time I try to incorporate this code.
    The php coding is as.
    <body>
    <div id="main_content">

    <?php

    include("mysql_connect.php");
    mysql_select_db("databaseName", $con_mysql_db); //Selecting database to connect
    $result = mysql_query("SELECT post_title, post_body From posts ORDER BY post_title DESC"); // Selecting post title and post content
    while($row = mysql_fetch_array($result))
    {
    $title = $row[0]; //Post title as a variable $title
    $content = $row[1]; //Post content as a variable $content
    echo("<h3>" .$title ."</h3><hr style='color: brown'><br/> .$content" ."<hr style='width: 80%; align: center; color: brown'><br/><br/><br/>");
    };
    if (isset($_GET['databaseName'])) {
    $pageno = $_GET['databAseName'];
    } else {
    $pageno = 1;
    } // 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 = 15;
    $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 table $limit";
    $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>

    The error is as that

    1) No pagination in the form of numbers or link is appearing. So I guess the code isn't working at all, However:

    2) When I try to run this code, my side bar and footer divs disappear. (I have 3 divs in my layout a) Main Content b)Side Bar and c)Footer )

    Please help me in the troubleshooting.
     
    saadi123, Jul 21, 2010 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    Could you post the error? I've checked it for syntax errors, and there are none, I couldn't debug it more, because I don't have the whole database structure.

    Btw, is "databaseName" the correct name for the database you use?
     
    ssmm987, Jul 22, 2010 IP
  3. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    1) By error I mean that the code isn't doing what it is supposed to do. However instead, my two out of three divs disappear when I place the code in any one div. you can visit saadi.000a.biz to see. Only the main_content div is appearing while the side_bar and footer divs have vanished.

    2) The database name is a000b_5538273_blog_posts. I have replaced it with "databaseName". Everything's fine regarding this.

    3) If no syntax error then are you sure that there's no logic error in the code? additionally have I placed the codes with the right sequence? I mean exactly where they are supposed to be?
     
    saadi123, Jul 22, 2010 IP
  4. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    Hey I figured it out!!!!

    Check below where I have declared variable $query. The line just below the line where I have declared a variable $limit.


    <?php

    include("mysql_connect.php");
    mysql_select_db("databaseName", $con_mysql_db); //Selecting database to connect
    $result = mysql_query("SELECT post_title, post_body From posts ORDER BY post_title DESC"); // Selecting post title and post content
    while($row = mysql_fetch_array($result))
    {
    $title = $row[0]; //Post title as a variable $title
    $content = $row[1]; //Post content as a variable $content
    echo("<h3>" .$title ."</h3><hr style='color: brown'><br/> .$content" ."<hr style='width: 80%; align: center; color: brown'><br/><br/><br/>");
    };
    if (isset($_GET['databaseName'])) {
    $pageno = $_GET['databAseName'];
    } else {
    $pageno = 1;
    } // 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 = 15;
    $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 table $limit"; // instead of table, I should have used posts (my table name) :)
    $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>

    One error still left. The NEXT, PREV keywords are not appearing as links but simple texts. So any help you can provide?


    It's a famous saying in URDU (my native language) that:

    "Nakal K Liye Bhi Akal Ki Zaroorat Hoti Hai" which means:

    "You need to be intelligent even when you copy/imitate"

    After this ridiculous error I must say that they said this for me

    HAHAHAHAHA...........
     
    Last edited: Jul 22, 2010
    saadi123, Jul 22, 2010 IP
  5. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I've spotted a few errors without running it. First of all, why do you display ALL the posts at the very start if this is a paging script?
     
    Deacalion, Jul 23, 2010 IP
  6. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    Do you mean to say this part of code?

    $result = mysql_query("SELECT post_title, post_body From posts ORDER BY post_title DESC"); // Selecting post title and post content

    If yes then what my logic is that first make all the rows available to the server and then group them according to the number of posts per page.

    Is there any problem with this logic???
     
    saadi123, Jul 23, 2010 IP
  7. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You should use the SQL query to select only the posts you want to display on the page.
    So if the user is on page 8 and it's 20 posts per page then you only want to pull out posts 140 to 160.
     
    Deacalion, Jul 23, 2010 IP
  8. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #8
    Yeah that really helped. Thanks.
    But there are still some problems. First let me show you 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 = 1;
    } // 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>

    The problem is that links like FIRST and PREV are not working at all.
    Secondly the links NEXT and LAST are though working but they are not effective. I mean that on clicking the NEXT link, the URL changes from
    http://saadi.000a.biz/ to http://saadi.000a.biz/index.php?pageno=2 and on clicking the LAST link it changes to http://saadi.000a.biz/index.php?pageno=9 but the problem is that the data in the main content never changes and it remains the same as if its the first page. You can visit the link to see what I am talking about which is saadi.000a.biz
    So please help me out.
     
    saadi123, Jul 23, 2010 IP