|
Posted by pangea33 on 12/05/06 07:37
naixn wrote:
> Moot wrote :
> >
> > Personally, I don't like building the where clause for a query all in
> > one go like this. I also don't even like building the clause with if's
> > and string concatenation. To get the cleanest, easiest where clause
> > construction, I typically use an array and implode it at the end.
> > Since I usually need to juggle a half-dozen or more conditions which
> > may or may not be used in a particular query, this method lets me very
> > easily build up which conditions I need set.
> >
> > So I would do your example in a manner like this:
> >
> > $where = array();
> > $where[] = "manufacturers.manid = hottubs.manid";
> > $where[] = "hottubs.type = '%s'";
> > $where[] = "hottubs.dimlength <= '%s'";
> > $where[] = "hottubs.dimwidth <= '%s'";
> > $where[] = "hottubs.dimhight <= '%s'";
> > $where[] = "hottubs.seatsto <= '%s'";
> > IF ($_GET['shape'] != 'any' ) $where[] = "hottubs.shape = '%s'";
> >
> > $strWhere = '';
> > if (count($where) > 0) $strWhere = " WHERE " . implode(' AND ',
> > $where);
> >
> > $qry = "SELECT *
> > FROM hottubs, manufacturers
> > " . $strWhere . "
> > ORDER BY " . $thesearchtype_search;
> >
> >
> > - Moot
> >
>
> In fact, that is cleaner. But the use of implode is what annoys me. When
> you get a really big request, it may be, for sure, the cleanest, but surely
> not the fastest... Not that it would be a great deal, but well.
>
> But it is clean. I admit, approve, and I'll test and maybe adopt. :p
>
> Thx.
>
> --
> Naixn
> http://fma-fr.net
Agreed, Moot's code is very sleek. Although, one wonders what happens
in the event of an "or" condition. Just teasing cause you got mad
style. ;-)
[Back to original message]
|