This is a discussion on elegant way of doing something else the last time through a loop? within the PHP General forums, part of the PHP Programming Forums category; HI list Is there an elegant way to know when the last time through the loop is going to be ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
HI list
Is there an elegant way to know when the last time through the loop is going to be and to do something else? I want to search through a table by "exploding" the search string and 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 array. Arguably I could first determine the amount of elements in the array and then use a for instead of a foreach, but I'm not sure if that will help ( will probably need a switch instead if you want to work with the sheer array elements), however, the if statement inside the loop is meant to "strip" out "the" and "and", meaning that it won't much help to use that approach anyway. 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". My attempts at "backtracking" the $sql string/array and start writing the end part of the string obviously fails. Any help with my "logic", ie, how do/would you guys do this? Thanks $table_name = "test"; if ($_POST[any_all] == "any") { $logic = "or"; } elseif ($_POST[any_all] == "all") { $logic = "and"; } $string = $_POST[text]; $phrases = explode(" ", $string); $sql = "select * from $table_name where "; foreach ($phrases as $key=>$val) { if (($val != "the") && ($val != "and")) { $sql.= "name like '%$val%' $logic"; } } $length = strlen($sql); $newlen = $length - 4; $sql[$newlen].= " order by name"; echo $sql; |
|
|||
|
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 |
|
|||
|
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 > |
|
|||
|
I always do it this way:
$condition=''; foreach ($phrases as $key=>$val) { if (($val != "the") && ($val != "and")) { $condition.= "name like '%$val%' $logic"; } } $condition=substr($condition, 0, strlen($condition) - strlen($logic)); $sql .= $condition; > > $length = strlen($sql); > $newlen = $length - 4; > $sql[$newlen].= " order by name"; > echo $sql; > > > |
|
|||
|
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); ?> |
|
|||
|
Following works nicely for me now, thanks all!
<?php include ("main_class.php"); $db = new my_db_class; $db ->connect("localhost","user","password","table"); $table_name = "main"; if ($_POST[any_all] == "any") { $logic = "or"; } elseif ($_POST[any_all] == "all") { $logic = "and"; } elseif ($_POST[any_all] == "exact") { $logic = "exact"; } $string = $_POST[text]; $phrases = explode(" ", $string); $sql = "select * from $table_name where "; if (($logic == "or") || ($logic == "and")) { $temp = ''; foreach ($phrases as $key=>$val) { if (($val != "the") && ($val != "and") && ($val != " ") && ($val != "")) { if (!empty($temp)) $temp .= ' '.$logic.' '; $temp.= "name like '%$val%' "; } } $sql.=$temp; } elseif ($logic == "exact") { $sql.="name like'%$string%'"; } $sql.= " order by name"; //echo $sql; $db->query($sql); $db->draw_table(); ?> On Mon, 2003-07-14 at 14:00, Marek Kilimajer wrote: > I always do it this way: > > $condition=''; > foreach ($phrases as $key=>$val) { > if (($val != "the") && ($val != "and")) { > $condition.= "name like '%$val%' $logic"; > } > } > $condition=substr($condition, 0, strlen($condition) - strlen($logic)); > $sql .= $condition; > > > > $length = strlen($sql); > > $newlen = $length - 4; > > $sql[$newlen].= " order by name"; > > echo $sql; > > > > > > > |
|
|||
|
Marek Kilimajer <kilimajer@webglobe.sk> wrote:
> I always do it this way: > > $condition=''; > foreach ($phrases as $key=>$val) { > if (($val != "the") && ($val != "and")) { > $condition.= "name like '%$val%' $logic"; > } > } > $condition=substr($condition, 0, strlen($condition) - strlen($logic)); > $sql .= $condition; another way to truncat the string: $logic_len = strlen($logic); $condition = substr($condition, 0, -($logic_len)); > > > >$length = strlen($sql); > >$newlen = $length - 4; > >$sql[$newlen].= " order by name"; > >echo $sql; > > > > > > > Curt -- |
|
|||
|
On Mon, Jul 14, 2003 at 01:11:11PM +0200, Petre Agenbag wrote:
> > HI list Hi Petre. > I want to search through a table by "exploding" the search string and > then compounding my own sql string by working through the array. I do alot of this. I have a solution which offloads the slight extra CPU onto the database server, while simplifying the PHP code a little. > the if statement inside the loop is > meant to "strip" out "the" and "and", meaning that it won't much help to > use that approach anyway. I've got a more flexible way of doing that too. How about this: $sql = "SELECT * FROM $table_name WHERE "; if ($_POST['any_all'] == 'ANY') { $logic = 'OR'; $sql .= '1=0'; } elseif ($_POST['any_all'] == 'ALL') { $logic = 'AND'; $sql .= '1=1'; } else die ('form hacking detected'); // protect from nasty user input $string = ereg_replace( '[^a-z0-9]+', ' ', strtolower($_POST['text']) ); $skip = array( 'the' => 1, 'and' => 1, 'a' => 1, ); foreach (explode(' ', $string) as $val) { if (!$skip[$val]) { $sql .= $logic . " name like '%$val%'"; } } $sql .= ' ORDER BY name'; -- Paul Chvostek <paul@it.ca> it.canada http://www.it.ca/ Free PHP web hosting! http://www.it.ca/web/ |