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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2008
    Posts
    133
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Post random population of select menu

    Hey,
    I have this code:

    PHP Code:
    <?php 
                    
    echo '<select name="user_banks" class="grey_tbox">';
                    
    $sql = ("SELECT * FROM ".TBL_USERPROPBANKS."");
                    
    $query $database->query($sql);
                    while(
    $bank mysql_fetch_array($query)){
                    echo 
    '<option value="'.$bank['prop_id'].'">'.$bank['prop_name'].' ('.$bank['prop_t_rate'].'%)</option>';            
                    }
                    echo 
    '</select>';
                    
    ?>
    It gets data from my MySQL table and populates the select menu.
    However, I want it to populate the position of each row in the select menu randomly.
    e.g. the top select row isn't always value =1


    I'm a bit stumped on how to actually achieve this. Any ideas?

    regards, Callum.

  • #2
    Regular Coder
    Join Date
    May 2009
    Posts
    425
    Thanks
    3
    Thanked 62 Times in 61 Posts
    Maybe adding ORDER BY RAND to your SQL string will do what you want. Give that a shot.

  • #3
    Regular Coder
    Join Date
    Dec 2008
    Posts
    133
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tfburges View Post
    Maybe adding ORDER BY RAND to your SQL string will do what you want. Give that a shot.
    PHP Code:
        $sql = ("SELECT * FROM ".TBL_USERPROPBANKS." ORDER BY RAND()"); 
    Thanks, this works fine but, i've heard its really quite server usage intensive: http://www.titov.net/2005/09/21/do-n...ws-from-table/

    So would you suggest i use one of those workarounds?
    I'll probably only have 5 rows in the table though, would a LIMIT 5 make it faster?

  • #4
    Regular Coder
    Join Date
    May 2009
    Posts
    425
    Thanks
    3
    Thanked 62 Times in 61 Posts
    I'm really not sure... I haven't used SQL in years and I prefer not to...

    Try these PHP functions:
    http://www.php.net/manual/en/function.shuffle.php
    http://php.net/manual/en/function.array-rand.php


  • #5
    Regular Coder
    Join Date
    Dec 2008
    Posts
    133
    Thanks
    15
    Thanked 0 Times in 0 Posts
    thank you!


  •  

    Posting Permissions

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