On Sun, 18 Mar 2001, Tyler Longren wrote:
> Hello, this is how I'm searching a database now...is there an easier way
> (PHP)? Anything that's short would be nice. This is just really long.
>
> $searchgenre = "punk, hardcore, classical, acoustic";
> $formatted_query = str_replace(", ", ",", $searchgenre);
> $genre_array = split(",", $formatted_query);
> $result = count($genre_array);
> if ($result == "1") {
> $sql_text = "SELECT * FROM artists WHERE genre like '%$genre_array[0]%'
> ORDER BY artist ASC";
> }
>
>
>
> if ($result == "2") {
> $sql_text = "SELECT * FROM artists WHERE genre like '%$genre_array[0]%' OR
> genre like '%$genre_array[1]%' ORDER BY artist ASC";
> }
First of all: this is NOT a php list.
Second: just construct the sql query by iterrating though the argument list with a
foreach statement.
$searchgenre="punk, hardcore, ...";
if(!($genre_array=split(", ",$searchgenre))){
print "Error...";
}
//assumes you have at least one genre
$first_genre=array_shift($genre_array);
$query="select * from artists where genre like '%$first_genre%'";
foreach($genre_array as $another_genre){
$query.=" OR '%$another_genre%'";
}
$query.=" ORDER BY artist";
The only drawback could be that you must have php4 to have the foreach construct. But
even with php3 use a while loop to go through the genre list.
regards,
thalis
---------------------------------------------------------------------
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php