+ Post New Thread
Results 1 to 6 of 6
Coding Thread, Some PHP pagination help in Coding and Web Development; Hi, Im creating a PHP script which displays records from a database and displays a certain number of records on ...
  1. #1
    My220x's Avatar
    Join Date
    Feb 2008
    Location
    UK
    Posts
    257
    Thank Post
    9
    Thanked 3 Times in 3 Posts
    Blog Entries
    8
    Rep Power
    13

    Some PHP pagination help

    Hi,

    Im creating a PHP script which displays records from a database and displays a certain number of records on each page. My code is below:

    PHP Code:
    <?php
            
    if($_GET['cat'] && is_numeric($_GET['cat'])) {
            if(
    $_GET['convo'] && is_numeric($_GET['convo'])) {
            
    $query "SELECT * FROM conversations WHERE id = '{$_GET['convo']}' AND cat = '{$_GET['cat']}' AND display = 'yes'";
            
    $result mysql_query($query) or die("Error");
            if(
    mysql_num_rows($result) > 0) {
            while(
    $row mysql_fetch_array($result)) {
            
    $query "SELECT name FROM cats WHERE id = '{$row['cat']}'";
            
    $results mysql_query($query) or die("Error");
            
    $data mysql_fetch_array($results);
            echo 
    "<h2>{$row["title"]}</h2>
            <p><span class=\"bold\">Category: </span>" 
    htmlentities($data["name"]) . "</p>";
            echo 
    "<p><span class=\"bold\">Submitted On: </span>" date("j \of F Y"strtotime($row["submit"])) . "</p>";
            echo 
    "<p><span class=\"bold\">Author: </span>" htmlentities($row["author"]) . "</p>";
            echo 
    "<p><span class=\"bold\">Rating:</span> {$row["yes"]} like it and {$row["no"]} don't.</p>";
            echo 
    "<p><span class=\"bold\">Conversation: </span>" nl2br(wordwrap(htmlentities($row["text"]), 100)) . "</p>";
            }
            } else {
            echo 
    "That conversation does not exsist.";
            }
            } elseif(!
    $_GET['convo']) {
            
    $query "SELECT COUNT(*) FROM conversations WHERE cat = '{$_GET['cat']}'";
    $result mysql_query($query) or die("Query Error");
    $rows mysql_fetch_array($result);
    $r $rows[0];
    $rowsperpage 10;
    $totalpages ceil($r $rowsperpage);
    if (isset(
    $_GET['page']) && is_numeric($_GET['page'])) {
    $currentpage = (int) $_GET['page'];
    } else {
    $currentpage 1;
    }
    if (
    $currentpage $totalpages) {
    $currentpage $totalpages;
    }
    if (
    $currentpage 1) {
    $currentpage 1;
    }
    $offset = ($currentpage 1) * $rowsperpage;
           
    $query "SELECT * FROM conversations WHERE cat = '{$_GET['cat']}' AND display = 'yes' LIMIT $offset$rowsperpage";
    $result mysql_query($query) or die("Query Error");
            echo 
    "<ul>";
            if(
    mysql_num_rows($result) > 0) {
            while(
    $row mysql_fetch_array($result))
            {
            echo 
    "<li><a href=\"cats.php?cat={$_GET['cat']}&convo={$row['id']}\">{$row['title']}</a></li>";
            }
            
    $range 3;

    if (
    $currentpage 1) {
    echo 
    " <a href='cats.php?cat={$_GET['cat']}&page=1'><<</a> ";
    $prevpage $currentpage 1;
    echo 
    " <a href='cats.php?cat={$_GET['cat']}&page=$prevpage'><</a> ";
    }
    for (
    $x = ($currentpage $range); $x < (($currentpage $range) + 1); $x++) {
    if ((
    $x 0) && ($x <= $totalpages)) {
    if (
    $x == $currentpage) {
    echo 
    " [<b>$x</b>] ";
    } else {
    echo 
    " <a href='cats.php?cat={$_GET['cat']}&page=$x'>$x</a> ";
    }
    }
    }
    if (
    $currentpage != $totalpages) {
    $nextpage $currentpage 1;
    echo 
    " <a href='cats.php?cat={$_GET['cat']}&page=$nextpage'>></a> ";
    echo 
    " <a href='cats.php?cat={$_GET['cat']}&page=$totalpages'>>></a> ";
    }
            } else {
            echo 
    "This Conversation does not exsist.";
            }
            } else {
            echo 
    "This conversation does not exsist";
            } 
            } else {
            echo 
    "I am sorry but this category does not exsist";
            }
            
    ?>
    Now if you do click on the next page link it says "This conversation does not exsist" however I want it to go to the next page can someone help me fix it please.

  2. #2

    Join Date
    Feb 2009
    Posts
    45
    Thank Post
    1
    Thanked 5 Times in 5 Posts
    Rep Power
    11
    1st off - you have 2 phrases 'This conversation does not exsist'

    Change them to be better debug statements - or at least
    'This conversation does not exsist 1' &
    'This conversation does not exsist 2'

    That'll help debug which path through your code your going through

    If I've read your code correctly


    you reach the 2nd of these debug statements as a result of the final else clause from

    if($_GET['convo'] && is_numeric($_GET['convo'])){
    ....code
    }elseif(!$_GET['convo']){
    ....code
    } else {
    echo "This conversation does not exsist";
    }


    I suspect this might be where your problem lies

    The
    if($_GET['convo'] && is_numeric($_GET['convo'])){

    tests for a Non-Blank & numeric parameter named convo

    whereas
    }elseif(!$_GET['convo']){

    This tests if convo is Blank / zero length string

    and therefore the
    } else {
    echo "This conversation does not exsist";

    will catch convo not blank, not zero length, but not numeric

    I suspect that this
    }elseif(!$_GET['convo']){

    is not what you intended

  3. #3

    Join Date
    Feb 2009
    Location
    Norfolk, England
    Posts
    12
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    11
    The main chunk, after the elseif (!_$GET['convo']) is the bit that's actually doing the work of the paging.

    On the face of the error message is wrong, because convo presumably isn't set when you come back with page 2. It is more likely to be the capital 'c' version as a result of the mysql_fetch_array failing. Definitely enumerate the error messages so you know which one isn't failing.

    I notice that in the main SQL fetch (the one with Limit), it is unsorted. It's a while since I've written any PHP/MySQL but it struck be that this being unordered might not help, as SQL doesn't guarantee the order of any returned dataset if you don't specify it (I think !)

    Might also be worth outputting the main SQL fetch as a debug effort just so you can check it using the CLI MySQL utility to see if it works.

    I presume you've removed the mysql_connects for clarity

  4. #4
    My220x's Avatar
    Join Date
    Feb 2008
    Location
    UK
    Posts
    257
    Thank Post
    9
    Thanked 3 Times in 3 Posts
    Blog Entries
    8
    Rep Power
    13
    Im going to do a bit more reading to fix it.
    Last edited by My220x; 21st March 2009 at 10:35 PM.

  5. #5
    My220x's Avatar
    Join Date
    Feb 2008
    Location
    UK
    Posts
    257
    Thank Post
    9
    Thanked 3 Times in 3 Posts
    Blog Entries
    8
    Rep Power
    13
    Ok heres my version all 100% working:

    PHP Code:
    <?php
           
    if(isset($_GET["page"]) && is_numeric($_GET["page"])) {
            
    $page ceil($_GET["page"]);
            } else {
            
    $page 1;
            }
            
    $perpage 10;
            
    $querycount "SELECT COUNT(*) FROM conversations WHERE display = \"no\"";
            
    $resultcount mysql_query($querycount) or die(mysql_error());
            
    $fetch_row mysql_fetch_row($resultcount);
            
    $lastpage ceil($fetch_row["0"] / $perpage);
            if(
    $page 1) {
            
    $page 1;
            } elseif(
    $page $lastpage) {
            
    $page $lastpage;
            }
                    
    $startlimit = ($page-1)*$perpage;

          
    $query "SELECT * FROM conversations WHERE display = \"no\" ORDER BY \"submit\" ASC LIMIT $startlimit$perpage ";
          
    $result mysql_query($query) or die(mysql_error());
          if(
    mysql_num_rows($result) > 0) {
          
    ?>
          <table id="latest" border="1">
          <th>Title</th>
          <th>Conversation</th>
          <th>Category</th>
          <th>Submitted On</th>
          <th>Author</th>
          <th>Action</th>
          <?php
          
    while($row mysql_fetch_array($result)) {
          echo 
    "<tr>";
          echo 
    "<td>" htmlentities($row["1"]) . "</td>";
          echo 
    "<td>" htmlentities($row["2"]) . "</td>";
          
    $sql "SELECT * FROM cats WHERE id = {$row["cat"]}"// This is so I can show the categories name
          
    $catresult mysql_query($query) or die("Error");
          
    $catname mysql_fetch_array($catresult);
          echo 
    "<td>" $catname["1"] . "</td>";
          echo 
    "<td>" date("jS \of F Y"strtotime($row["6"])) . "</td>";
          echo 
    "<td>" htmlentities($row["8"]) . "</td>";
          echo 
    "<td><a href=\"manage.php?convo={$row["0"]}&action=accept\">Accept</a> / <a href=\"manage.php?convo={$row["0"]}&action=reject\">Reject</a></td>";      
          echo 
    "</tr>";
          }
          echo 
    "
          </table>
          </div>"
    ;
          if(
    $page == 1) {
            echo 
    "<<< ";
            echo 
    "<< ";
            } else {
            echo 
    "<a href=\"?page=1\"><<<</a> ";
            echo 
    "<a href=\"?page=" . ($page 1) . "\"><<</a> ";
            }
            
            for (
    $i=$page$i<=$page+2$i++) {
        if (
    $i>&& $i<=$lastpage) {
            if (
    $page == $i) {
                echo 
    "<b>$i</b> ";
            } else {
                echo 
    "<a href=\"?page=$i\">$i</a> ";
            }
        }
    }
            
            if(
    $page == $lastpage) {
            echo 
    ">>> ";
            echo 
    ">> ";
            } else {
            echo 
    "<a href=\"?page=". ($page 1) ."\">>></a> ";
            echo 
    "<a href=\"?page=" $lastpage "\">>>></a> ";
            }
          } else {
          echo 
    "<p>No Conversations waiting approval</p>";
          }
          }

  6. #6

    Join Date
    Feb 2009
    Posts
    45
    Thank Post
    1
    Thanked 5 Times in 5 Posts
    Rep Power
    11
    Quote Originally Posted by My220x View Post
    Ok heres my version all 100% working:

    [php]<?php
    if(isset($_GET["page"]) && is_numeric($_GET["page"])) {
    $page = ceil($_GET["page"]);
    } else {
    $page = 1;
    }
    $perpage = 10;
    $querycount = "SELECT COUNT(*) FROM conversations WHERE display = \"no\"";
    $resultcount = mysql_query($querycount) or die(mysql_error());
    $fetch_row = mysql_fetch_row($resultcount);
    $lastpage = ceil($fetch_row["0"] / $perpage);
    if($page < 1) {
    $page = 1;
    } elseif($page > $lastpage) {
    $page = $lastpage;
    }
    $startlimit = ($page-1)*$perpage;

    $query = "SELECT * FROM conversations WHERE display = \"no\" ORDER BY \"submit\" ASC LIMIT $startlimit, $perpage ";
    $result = mysql_query($query) or die(mysql_error());
    if(mysql_num_rows($result) > 0) {
    ?>
    <table id="latest" border="1">
    <th>Title</th>
    <th>Conversation</th>
    <th>Category</th>
    <th>Submitted On</th>
    <th>Author</th>
    <th>Action</th>
    <?php
    while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . htmlentities($row["1"]) . "</td>";
    echo "<td>" . htmlentities($row["2"]) . "</td>";


    // This is so I can show the categories name
    $sql = "SELECT * FROM cats WHERE id = {$row["cat"]}";
    $catresult = mysql_query($query) or die("Error");
    $catname = mysql_fetch_array($catresult);

    ????? Shouldn't this be ???????
    $catresult = mysql_query($sql) or die("Error");
    $catname = mysql_fetch_array($catresult);




    You need to check that your inner SQL query is what you intended

SHARE:
+ Post New Thread

Similar Threads

  1. PHP
    By faza in forum How do you do....it?
    Replies: 4
    Last Post: 3rd December 2008, 01:56 PM
  2. PHP ID
    By Jackd in forum Coding
    Replies: 4
    Last Post: 30th November 2008, 01:08 PM
  3. PHP Help!
    By Cravon in forum Coding
    Replies: 2
    Last Post: 20th November 2007, 05:05 PM
  4. Help with PHP
    By Gatt in forum Web Development
    Replies: 1
    Last Post: 14th November 2006, 09:01 PM
  5. php help
    By beeswax in forum Windows
    Replies: 33
    Last Post: 14th October 2005, 04:51 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •