Sheer genius my man!
Thanks!
On Mon, 2003-07-14 at 13:38, Tom Rogers wrote:
> Hi,
>
> Monday, July 14, 2003, 9:11:11 PM, you wrote:
> PA> HI list
>
> PA> Is there an elegant way to know when the last time through the loop is
> PA> going to be and to do something else?
>
>
> PA> I want to search through a table by "exploding" the search string and
> PA> then compounding my own sql string by working through the array.
>
> >>From my example below, you can see I use a foreach to loop through the
> PA> array. Arguably I could first determine the amount of elements in the
> PA> array and then use a for instead of a foreach, but I'm not sure if that
> PA> will help ( will probably need a switch instead if you want to work with
> PA> the sheer array elements), however, the if statement inside the loop is
> PA> meant to "strip" out "the" and "and", meaning that it won't much help to
> PA> use that approach anyway.
>
> PA> Anyway, as you can see my problem lies with the SQl when the last
> PA> element is reached, then it should NOT add another "and" or "or".
>
> PA> My attempts at "backtracking" the $sql string/array and start writing
> PA> the end part of the string obviously fails.
>
> PA> Any help with my "logic", ie, how do/would you guys do this?
>
> PA> Thanks
>
>
> PA> $table_name = "test";
> PA> if ($_POST[any_all] == "any") {
> PA> $logic = "or";
> PA> }
> PA> elseif ($_POST[any_all] == "all") {
> PA> $logic = "and";
> PA> }
>
> PA> $string = $_POST[text];
> PA> $phrases = explode(" ", $string);
>
> PA> $sql = "select * from $table_name where ";
>
> PA> foreach ($phrases as $key=>$val) {
> PA> if (($val != "the") && ($val != "and")) {
> PA> $sql.= "name like '%$val%' $logic";
> PA> }
> PA> }
>
> PA> $length = strlen($sql);
> PA> $newlen = $length - 4;
> PA> $sql[$newlen].= " order by name";
> PA> echo $sql;
>
> I would do this
>
> $like = '';
> foreach ($phrases as $key=>$val) {
> if (($val != "the") && ($val != "and")) {
> if(!empty($like)) $like .= ' '.$logic.' ';
> $like.= "name like '%$val%'";
> }
> }
> $sql .= $like;
> --
> regards,
> Tom
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php