Re: elegant way of doing something else the last time through a loop?

From: Date: Mon, 14 Jul 2003 12:02:21 +0000
Subject: Re: elegant way of doing something else the last time through a loop?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-155070@lists.php.net to get a copy of this message
On 14 Jul 2003 13:11:11 +0200, you wrote: >Anyway, as you can see my problem lies with the SQl when the last >element is reached, then it should NOT add another "and" or "or". The short answer is "implode()". >Any help with my "logic", ie, how do/would you guys do this? if ($_POST[any_all] == "any") { any_all should be a string literal or a variable name. Try cranking up your warning levels. <? function build_sql_statement($table_name, $words, $glue) { $sql = "SELECT * FROM $table_name WHERE "; $newwords = array(); for ($i = 0; $i < sizeof($words); $i++) { if ($words[$i] != 'the' && $words[$i] != 'and') { $newwords[] = "name LIKE '%" . $words[$i] . "%'"; } } $sql .= join(" $glue ", $newwords); $sql .= " ORDER BY name"; return ($sql); } $table_name = "test"; $any_all = "any"; $string = "this is the test"; /* get our logic choice */ $logic = 'AND'; if (isset($any_all) && $any_all == 'any') { $logic = 'OR'; } /* break the incoming string into single words */ $phrases = explode(" ", $string); /* generate a SQL statement from the incoming words */ $sql = build_sql_statement($table_name, $phrases, $logic); echo ($sql); ?>

« previous php.general (#155070) next »