On Thu, 06 Sep 2007 14:03:22 +0200, 4sgcv5hy6d0r
<fj*************@temporaryinbox.comwrote:
Hi group!
Why str_replace does not work in this case?
Thanks in advance!
if ($cat0 != ''){ $query_cat0 = "cat0 LIKE '$cat0%'"; }
if ($cat1 != ''){ $query_cat1 = "AND cat1 LIKE '$cat1%'"; }
if ($cat2 != ''){ $query_cat2 = "AND cat2 LIKE '$cat2%'"; }
$mysql_query = "SELECT * FROM mytable WHERE ".$query_cat0." ".
$query_cat1." ".$query_cat2;
$what = 'WHERE AND';
$with = 'WHERE';
$mysql_query = str_replace($what, $with, $mysql_query);
echo $mysql_query;
Because the string is 'WHERE AND', not 'WHERE AND', notice the double
space. You could use a regex matching whitespace, clearly overkill. I'd do
it like this:
$where_clauses = array();
if ($cat0 != ''){ $where_clauses[] = "cat0 LIKE '$cat0%'"; }
if ($cat1 != ''){ $where_clauses[] = "cat1 LIKE '$cat1%'"; }
if ($cat2 != ''){ $where_clauses[] = "cat2 LIKE '$cat2%'"; }
$mysql_query = "SELECT * FROM mytable";
if(!empty($where_clauses)) $mysql_query .= ' WHERE '.implode(' AND
',$where_clauses);
echo $mysql_query;
--
Rik Wasmus