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

Thread: Random Pick

  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    231
    Thanks
    9
    Thanked 1 Time in 1 Post

    Random Pick

    I've gota music script (most of u are tired of it. asking 2many questions :P).

    When an artist isn't picked, there is a random artist picked. Now I have it setup so it counts the rows, and picks a random number from 1-nr of rows. This number is then used as the id and gotten a mysql query from this id.

    PHP Code:
    $query mysql_query("SELECT * FROM `artists`");
    $total mysql_num_rows($query);
    $random=rand(1,$total);
    $right mysql_query("SELECT * FROM `artists` WHERE `id`=$random"); 
    Now, I want something a bit different. I also have ABC....Z, by the first letterof Artist Name. If the first letter is picked, but no artist, I want the random artist to be start with this letter. I wanted to use something like above but the id won't be in order. it could be like 1, 20, 32, etc etc.

    There has to be something with arrays... I just can't think of it atm. Anyone has any idea?

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well your first one can be cleaned up like so:

    PHP Code:
    $query mysql_query("SELECT * FROM artists ORDER BY rand() LIMIT 1"); 
    Now you can put in an if statement if they choose a letter and further your search like so:

    PHP Code:
    $letter $_POST['letter'];  //or incomplete artist name
    $query mysql_query("SELECT * FROM artists WHERE artist_name LIKE '".$letter."%' ORDER BY rand() LIMIT 1"); 
    What that will do is search for all names that "start" with w/e the user puts in. So if they just put in 'A' then they will return everything that starts with A and then randomize it. If they put in App, then it will return everything that begins with App like apple, appliance, applause... u get the idea. And the LIMIT 1 just limits your results to 1...
    Last edited by iLLin; 03-25-2007 at 05:51 AM.

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Posts
    231
    Thanks
    9
    Thanked 1 Time in 1 Post
    That one line will replace my 4???? Damn I need a few lessons :P

    I get an error when $fl is set.
    PHP Code:
        if(isset($fl)){
            
    $right mysql_query("SELECT * FROM artists WHERE `firstletter`=$fl ORDER BY rand() LIMIT 1");
        }
        else{
            
    $right mysql_query("SELECT * FROM artists ORDER BY rand() LIMIT 1");  
        } 
    Says invalid mysql query...
    (Yes, firstletter exists in the table as a column. I made it especially for this. So, when a person clicks the letter I can search easier. )

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,633
    Thanks
    4
    Thanked 148 Times in 139 Posts
    your error is you are quoting your column name. column names do not belong in quotes.

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Those are backticks, not quotes.
    PHP Code:
    $right mysql_query("SELECT * FROM artists WHERE `firstletter`=$fl ORDER BY rand() LIMIT 1") or die(mysql_error()); 
    Will tell you the error. Incidentally, you should be able to use the query above with LIKE in it to find rows with only the first letter, so you don't have to store it for each row.


  •  

    Posting Permissions

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