+ Reply to Thread
Results 1 to 7 of 7

Thread: PHP Pagination

  1. #1
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    PHP Pagination

    Code:
    SELECT  *  FROM  `textbook` WHERE  `class`=:class ORDER  BY `textbook`.`ch`
    
    + ---- + ----- + -- + ------ + ------ +
    | .id. | class | ch | sTitle | fTitle |
    + ---- + ----- + -- + ------ + ------ +
    | 6... | CK2.. | 1. | ...... | ...... |
    | 7... | CK2.. | 3. | ...... | ...... |
    | 8... | CK2.. | 6. | ...... | ...... |
    | 115. | CK2.. | 9. | ...... | ...... |
    | 116. | CK2.. | 11 | ...... | ...... |
    | 9... | CK2.. | 13 | ...... | ...... |
    | 10.. | CK2.. | 15 | ...... | ...... |
    | 11.. | CK2.. | 17 | ...... | ...... |
    | 12.. | CK2.. | 20 | ...... | ...... |
    | 13.. | CK2.. | 22 | ...... | ...... |
    + ---- + ----- + -- + ------ + ------ +
    Let's say i bring up the chapter for 11.
    Code:
    SELECT  *  FROM  `textbook` WHERE  `class`=:class AND `ch`=:ch"
    
    + ---- + ----- + -- + ------ + ------ +
    | .id. | class | ch | sTitle | fTitle |
    + ---- + ----- + -- + ------ + ------ +
    | 116. | CK2.. | 11 | ...... | ...... |
    + ---- + ----- + -- + ------ + ------ +
    I also want to have previous and next buttons. But the design flaw in the database is that I can't just do a `ch`+1 or a `ch`-1 because they are not all in sequencial order.
    I figured that I can make another column `order`, but I do not want to add unneccessary columns to the database.
    Another way I was thinking would to have all the values of `ch` in an array and try to sort it somehow.

    I could be overlooking a very simple solution.

    Note:
    ~I am using PDO.

  2. #2
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: PHP Pagination

    Are you saying if a request comes in to display ch 11, you want to retrieve the information for ch 9 and for ch 13 (using your above data)?

  3. #3
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: PHP Pagination

    Quote Originally Posted by dlukin View Post
    Are you saying if a request comes in to display ch 11, you want to retrieve the information for ch 9 and for ch 13 (using your above data)?
    yes sir.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Pagination

    Use the LIMIT clause.
    Code:
    SELECT <columns> 
      FROM `textbook` 
      WHERE `class`=:class AND `ch` > :ch 
      ORDER BY `ch` ASC
      LIMIT 1
    Index column ch for best performance. An index on `textbook` (`class`, `ch`) will work.
    Last edited by misson; 06-11-2010 at 06:22 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: PHP Pagination

    so I would use this query to find the previous chapters?
    Code:
    SELECT <columns> 
      FROM `textbook` 
      WHERE `class`=:class AND `ch` < :ch 
      ORDER BY `ch` DESC
      LIMIT 1
    also, what kind of check could I run to see if there is no more previous/next chapters?

  6. #6
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP Pagination

    That query should work.

    If result set is empty, then no previous/next chapter (depending on the query).
    Nothing is always absolutely so.

  7. #7
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: PHP Pagination

    thanks guys that did the trick. now im gonna go back to my absence. ill be back soon.

+ Reply to Thread

Similar Threads

  1. PHP Pagination, can someone help please?
    By chibib0 in forum Programming Help
    Replies: 2
    Last Post: 05-03-2008, 09:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers