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

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-14-2003
Petre Agenbag
 
Posts: n/a
Default elegant way of doing something else the last time through a loop?

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;


Reply With Quote
  #2 (permalink)  
Old 07-14-2003
Tom Rogers
 
Posts: n/a
Default Re: [PHP] elegant way of doing something else the last time through a loop?

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

Reply With Quote
  #3 (permalink)  
Old 07-14-2003
Petre Agenbag
 
Posts: n/a
Default Re: [PHP] elegant way of doing something else the last timethrough a loop?

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
>


Reply With Quote
  #4 (permalink)  
Old 07-14-2003
Marek Kilimajer
 
Posts: n/a
Default Re: [PHP] elegant way of doing something else the last time througha loop?

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;
>
>
>


Reply With Quote
  #5 (permalink)  
Old 07-14-2003
David Otton
 
Posts: n/a
Default Re: [PHP] elegant way of doing something else the last time through a loop?

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);

?>

Reply With Quote
  #6 (permalink)  
Old 07-14-2003
Petre Agenbag
 
Posts: n/a
Default Re: [PHP] elegant way of doing something else the last timethrough a loop? SOLVED

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;
> >
> >
> >

>


Reply With Quote
  #7 (permalink)  
Old 07-14-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] elegant way of doing something else the last time through a loop?

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
--


Reply With Quote
  #8 (permalink)  
Old 07-15-2003
Paul Chvostek
 
Posts: n/a
Default Re: elegant way of doing something else the last time through a loop?

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/

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:46 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0