Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem with order and limit

    I'm kinda new to PHP & MySQL and am having a small problem with a SELECT query. Can anyone point me in the right direction on how to resolve this?

    I have a table, 'discussion', with the fields 'postid', 'parent', and several others. I am trying to query 'discussion' for a range of rows with a limit of ten. Using the 'postid' of the last post, I create a link to view the next set of results after the last post.

    I have no problem doing this. I have 24 rows in the table and am able to view them in sets of 10 at a time. But when I try to order the results descending, I am unable to. The first set of ten (#24-#15) display, but when I try to view the next ten, the query returns the results for rows #9-#1.

    my trimmed down code:

    Code:
    if (!$more) {$more = 0;} # starting post
    $query = "SELECT * FROM `disc` WHERE `parent` = 0 ORDER BY postid LIMIT $more, 10";
    $result = $db->query($query);
    $num_results = $result->num_rows;
    $lastPost = 0;
    for ($i=0; $i < $num_results; $i++) {
        ... fetch row from result and process    
        $lastPost = $pid;
    }
    
    print "<a href='?more=$lastPost' title='Next'>Next</a>";
    This will display the results in ascending order and allow me to page through the results, but when I change my query to:
    Code:
    $query = "SELECT * FROM `disc` WHERE `parent` = 0 ORDER BY postid DESC LIMIT $more, 10";
    the paging does not work.

    Can anyone point out what I'm doing wrong or what I should look into to achieve this. Thanks.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,324 Times in 4,290 Posts
    Well, not to ask a dumb question, but have you debugged?

    WHat is the value of $more on that second page???

    How about simply echoing the $query on each page?

    FWIW, doing DESC LIMIT start,count works fine for me.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I think I understand where I am confused and what is tripping me up.


  •  

    Posting Permissions

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