help with str_replace in select statement
help with str_replace in select statement
am 06.09.2007 14:03:22 von 4sgcv5hy6d0r
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;
Re: help with str_replace in select statement
am 06.09.2007 14:19:14 von luiheidsgoeroe
On Thu, 06 Sep 2007 14:03:22 +0200, 4sgcv5hy6d0r =
wrote:
> Hi group!
> Why str_replace does not work in this case?
> Thanks in advance!
>
> if ($cat0 !=3D ''){ $query_cat0 =3D "cat0 LIKE '$cat0%'"; }
> if ($cat1 !=3D ''){ $query_cat1 =3D "AND cat1 LIKE '$cat1%'"; }
> if ($cat2 !=3D ''){ $query_cat2 =3D "AND cat2 LIKE '$cat2%'"; }
>
> $mysql_query =3D "SELECT * FROM mytable WHERE ".$query_cat0." ".
> $query_cat1." ".$query_cat2;
> $what =3D 'WHERE AND';
> $with =3D 'WHERE';
>
> $mysql_query =3D 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 =3D array();
if ($cat0 !=3D ''){ $where_clauses[] =3D "cat0 LIKE '$cat0%'"; }
if ($cat1 !=3D ''){ $where_clauses[] =3D "cat1 LIKE '$cat1%'"; }
if ($cat2 !=3D ''){ $where_clauses[] =3D "cat2 LIKE '$cat2%'"; }
$mysql_query =3D "SELECT * FROM mytable";
if(!empty($where_clauses)) $mysql_query .=3D ' WHERE '.implode(' AND =
',$where_clauses);
echo $mysql_query;
-- =
Rik Wasmus
Re: help with str_replace in select statement
am 06.09.2007 14:31:44 von 4sgcv5hy6d0r
On 6 Sep., 14:19, "Rik Wasmus" wrote:
> On Thu, 06 Sep 2007 14:03:22 +0200, 4sgcv5hy6d0r
>
>
>
> wrote:
> > 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
Thank you very much! Browser shows only one space. Now I know it
again ;)
Re: help with str_replace in select statement
am 06.09.2007 14:38:18 von Sandy.Pittendrigh
On Sep 6, 6:03 am, 4sgcv5hy6d0r
wrote:
> 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;
It doesn't work?
If you put 'AND ' in front
of cat0 you could mix the where conditions in arbitrary
runtime combinations and order sequences.
.....keep two buffers and a table-name hash:
$select = 'select '; // append column titles according to menu
clicks
$from = ''; // foreach column title in select, insert
matching table name to a hash
where = ' where ' // foreach column title, take a condition from
menu values
.....put it all together and (as you suggest) turn 'where and' into
'where'