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 Coder
    Join Date
    Mar 2003
    Location
    USA
    Posts
    99
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Display in random order

    I have a db of items with `id`s. And I'm currently displaying them with a loop in DESC order (5, 4, 3, 2, 1).

    PHP Code:
    $query mysql_query("SELECT * FROM `table` ORDER BY `id` DESC");
    while(
    $row mysql_fetch_array($query)) {
        echo 
    'This is item number' $row["id"] . '<br />';

    How can I modify this so it will display like this instead:

    Code:
    This is item number 3
    This is item number 1
    This is item number 2
    This is item number 4
    This is item number 5
    Randomly. Thanks!

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    PHP Code:
    $query mysql_query("SELECT * FROM `table` ORDER BY RAND()");
    while(
    $row mysql_fetch_array($query)) {
        echo 
    'This is item number' $row["id"] . '<br />';


  • Users who have thanked SKDevelopment for this post:

    Blatch (08-31-2009)

  • #3
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    $ids = array();

    $query mysql_query("SELECT * FROM `table` ORDER BY `id` DESC");
    while(
    $row mysql_fetch_array($query))
        
    $ids[] = $row['id'];

    shuffle($ids);

    foreach ( 
    $ids as $id )
        echo 
    'This is item number' $id '<br />';


  • Users who have thanked Mwnciau for this post:

    Blatch (08-31-2009)

  • #4
    New Coder
    Join Date
    Mar 2003
    Location
    USA
    Posts
    99
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thank you both!


  •  

    Posting Permissions

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