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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Getting sql results from an array

    Hey all, I'm hoping someone can help me with a bit of a problem I've run into. Basically I have an array of numbers that correspond to an 'ID' in a database. I can run a loop to get all the results, however I need to order all of the results by the 'date' field, rather than the 'ID' field.

    Simplified, if I have an array with 1,3,17,9, I need to get all events with these ID's, and order them by their corresponding dates.

    Can anyone point me in the right direction?

  • #2
    New Coder
    Join Date
    Nov 2009
    Location
    Australia
    Posts
    78
    Thanks
    0
    Thanked 7 Times in 7 Posts
    rather than looping through the array of ids, maybe append the ids to the SQL query ?

    soemthing like

    PHP Code:
    foreach($ids as $i){
       
    $sql .= " id = '$i' OR ";
    }
    $sql rtrim($sql"OR ");

    $query "SELECT * FROM `users` WHERE ($sql) ORDER BY `date`"

  • Users who have thanked GoodBanners.com for this post:

    hookedupphat (12-01-2009)

  • #3
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    that worked beautifully, thank you

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $query 'SELECT * FROM `users` WHERE `id` IN ( ' implode', '$ids ) . ' ) ORDER BY `date`';

    echo 
    $query
    Output:
    Code:
    SELECT * FROM `users` WHERE `id` IN ( 1, 3, 17, 9 ) ORDER BY `date`


  •  

    Tags for this Thread

    Posting Permissions

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