Re: elegant way of doing something else the last time through a loop? SOLVED
| From: | Petre Agenbag | Date: | Mon, 14 Jul 2003 12:35:28 +0000 |
| Subject: | Re: elegant way of doing something else the last time through a loop? SOLVED | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-155078@lists.php.net to get a copy of this message | ||
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;
> >
> >
> >
>