...

View Full Version : Random Pick



Erindesign
03-25-2007, 05:54 AM
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.



$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?

iLLin
03-25-2007, 06:48 AM
Well your first one can be cleaned up like so:



$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:



$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...

Erindesign
03-25-2007, 04:30 PM
That one line will replace my 4???? Damn I need a few lessons :P

I get an error when $fl is set.


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. :))

guelphdad
03-26-2007, 03:38 AM
your error is you are quoting your column name. column names do not belong in quotes.

Inigoesdr
03-26-2007, 04:22 AM
Those are backticks, not quotes.

$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum