Paginate a Table in Multiple Databases (PHP/MySQL)

Discussion in 'PHP' started by FishSword, Nov 5, 2012.

  1. #1
    I have 10 MySQL databases that feature the same table structures/names in each.
    The table I am partically interested in is the Users

    I need a way to display/paginate the results from each of the Users tables, and sort the data by category_colour

    The table fields are:


    • id: INT(11)
    • name: varchar(255) - e.g. Dave, Lisa, Steve
    • category_colour: char(1) - Can be G = Green, O = Orange, B = Blue, O = Orange.

    If I was to do this without paginate, there would be too many results, and the server would take an age to display them all.
    I need the results to be categorized and in order of category_colour (1st: Red, 2nd: Green, 3rd: Blue, 4th: Orange) - Displaying the users database id

    OUTPUT EXAMPLE:

    Category Colour: Red


    • Dave - 1
    • Steve - 5
    • Fred - 4
    • etc

    Category Colour: Green



    • Richard - 6
    • Kelly - 8
    • etc.

    Category Colour: Blue



    • Emma - 3
    • Scott - 2
    • Louise - 7
    • etc.
     
    FishSword, Nov 5, 2012 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    First pull a count, to see how many there are, then use the SQL LIMIT command to set how many to pull and from what offset in the result list.

    SELECT COUNT(id) FROM users

    tells you how many... to determine the number of pages you just do:
    $pageCount = floor( ($count-1) / $numberToShowPerPage ) + 1;

    where $count is the result from the counting query and $numberToShowPerPage is, well... you can figure that out. (It's AMAZING how often people screw up that simple calculation! -- oh, and don't forget to special-case handle 0 results)

    SELECT * FROM users LIMIT :start, :count

    would pull up a list of :count users starting at :count into what the result would be. (assumes you're using prepared queries and bindparam like a good little doobie). :start would be the page * $numberToShowPerPage, and :count would probably BE $numberToShowPerPage.
     
    deathshadow, Nov 6, 2012 IP
  3. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #3
    may I add that you probably want to order by color like this
    
    SELECT * FROM users ORDER BY category_color LIMIT :start, :count
    
    Code (markup):
     
    stephan2307, Nov 6, 2012 IP
  4. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    Its not as easy as this. I have many Users tables. They are all in different databases. I need to combine them all, sort, then paginate and display the results similar to the example above.
     
    FishSword, Nov 6, 2012 IP
  5. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #5
    OK maybe you can show us then what tables you have and what their structures are.

    Also in my head it doesn't make sense to have multiple user tables.

    anyway show us what you have then we can help you better. otherwise it is just guessing.
     
    stephan2307, Nov 7, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    You probably designed your database structure very badly. Think about normalizing your data into a single database.
     
    Rukbat, Nov 7, 2012 IP