#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    224
    Rep Power
    13

    Problem with Pagination


    Hi all,

    I have the following pagination script, when it gets to the 10th comment you should then go to the next page to display the next 10 and so on. The problem is that in my testing I have 12 results currently, and it is displaying all 12 on the page. When I click on 'Next' it simply refreshes the same page.

    I know I need the issue of only displaying 10 results per page, but I also feel that I don't have the pagination working either, ie that the next 10 link may not work - happy to be told otherwise. If someone could help with the first part that would be great!Here is the script:

    PHP Code:
      ////Get data from users
        
    $user_id=$_SESSION['user_id'];
        
    $query1="SELECT oauth_uid, oauth_provider FROM users where userid='" $user_id "'";
    $result1 $mysqli->query($query1) or die($mysqli->error.__LINE__);


    // GOING THROUGH THE DATA
     
    if($result1->num_rows 0) {
         
    $row $result1->fetch_assoc();
         
    $oauth_uid=$row['oauth_uid'];
         
    $oauth_provider=$row['oauth_provider'];
     }  
     
     
    /////////////////////////////////////////////
     ///////////Get data from comments, the item id comes from the page, which is obtained earlier in the script
        
        
    $query="SELECT name, comment, anonymous, userid, item_id, comment_date from comments where item_id='$item_id'";
    $result2 $mysqli->query($query) or die($mysqli->error.__LINE__);

    // GOING THROUGH THE DATA
     
    if($result2->num_rows 0) {
      
       
    $limit=10;  
         if (empty(
    $s)) {
      
    $s=0;
      }

    // get results

      
    $query1 .= " limit $s,$limit";
     

    // begin to show results set
      
    echo "<br></br>";
    echo 
    "<h3>Comments for this article:</h3>";
    $count $s ;

    // now you can display the results returned   
    while ($rowmysqli_fetch_array ($result2)) {
        
      
    $anonymous=$row['anonymous'];
      
    $name=$row['name'];
     
    // echo "<p>anonymous=$anonymous</p>";
      
    $comment $row['comment'];
      
    $oauth_provider=$row['oauth_provider'];
      
      
    //Get the number of roes from the query:
      
    $numrows $result2->num_rows;
      

    if ((
    $anonymous==true)&& ($oauth_provider="facebook")){

    echo 
    "<p>$count.) ".$comment."<br/><sub><b>Comment by: </b>'Anonymous Poster'</sub><hr /><p>";
      }
      else{ 
     echo 
    "<p>$count.) ".$comment."<br/><sub><b>Comment by: </b>".$name."</sub><hr /><p>";     
       }  

      
    $count++ ;
      }

    $currPage = (($s/$limit) + 1);

    //break before paging
      
    echo "<br />";

      
    // next we need to do the links to other results
      
    if ($s>=1) { // bypass PREV link if s is 0
      
    $prevs=($s-$limit);
    print 
    "&nbsp;<a href='$PHP_SELF?s=$prevs'>&lt;&lt> Prev 10</a>&nbsp&nbsp;";
      
    Prev 10</a>&nbsp&nbsp;";
      }
      
      
      
    // calculate number of pages needing links
      
    $pages=intval($numrows/$limit);

    // 
    $pages now contains int of pages needed unless there is a remainder from division

      if (
    $numrows%$limit) {
      // has remainder so add one page
      
    $pages++;
      }

    // check to see if last page
      if (!(((
    $s+$limit)/$limit)==$pages) && $pages!=1) {

      // not last page so give NEXT link
      
    $news=$s+$limit;

      echo "
    &nbsp;<a href='$PHP_SELF?s=$news'>Next 10 &gt;&gt;</a>";
      }

    $a = $s + ($limit) ;
      if (
    $a > $numrows) { $a = $numrows ; }
      
    $b = $s + 1 ;
      echo "
    <p>Showing results $b to $a of $numrows</p>"; 
    Thanks,


    G
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    77
    To get this to work you need to add
    Code:
    ORDER BY
    followed by the id then either
    Code:
    ASC
    or
    Code:
    DESC
    then
    Code:
    LIMIT ".$end.",".$start."
    where end would be page(#) times the limit - the limit
    Code:
    $end = $page*$limit-($limit)
    start would be = to limit and limit would = 10
    An example of the page variable would be
    Code:
    $page = (!$_GET['page'] || $_GET['page'] < 0) ? "1" : $_GET['page'];
    $page = ceil($page);
    so it would look like this:
    PHP Code:

    $query
    ="SELECT name,comment,anonymous,userid,item_id,comment_date from comments where item_id='$item_id' ORDER BY `id` ASC LIMIT ".$end.",".$start.""// or DESC 
    cheers
    Last edited by jack13580; March 15th, 2013 at 11:42 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    224
    Rep Power
    13
    Thanks, I'll work this through - looks like I am getting somewhere..
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    77
    If you need to see a complete working example I can post one here if you want

IMN logo majestic logo threadwatch logo seochat tools logo