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


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to