SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    order random info mySQL - php

    i have an mySQL database with event information in it.

    Date, Location, Type

    there are 10 events in the database.

    how would i get 5 of those events from random, and then with those 5 order by date.

    i know how to get 5 random events, but i dont know how to then order the 5 i have.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    use your query to retrieve the 5 random rows, then sort the array in php

    (re)sorting 5 rows in php memory would be near instantaneous
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sorting mysql info

    i have a database of events on mySQL, i have retrived of of those events. i want to then fill and array with them.

    what is the best way to do this so the events can be ordered by date, location or type depending on what the user wants. would i need 3 arrays for each row?

  4. #4
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can use SQL to order it by how the user wants:

    Example:
    SELECT event FROM events ORDER BY date
    SELECT event FROM events ORDER BY location
    SELECT event FROM events ORDER BY type

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    see, wazzaday, this is what happens when you create multiple threads

    centeredeffect obviously did not see your other thread where i suggested you sort the 5 rows in php

    i've asked the moderators to merge your threads
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Threads merged.

    wazzaday, have a read of this thread (http://www.sitepoint.com/forums/mysq...on-727493.html), a number of ways of grabbing x number of records at random from a database are discussed and compared. It's probably a mute point if your only going to have 10 records but if the number of records is eventually going to be large, it's something worth considering.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


Bookmarks

Posting Permissions

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