Quote:
Originally Posted by sarettah
Me, I would try something like this:
<?php
$searchstring='transvestite threesome tea party';
$terms=explode(' ',$searchstring);
$conditions=array();
for($i=0;$i<count($terms);$i++)
{
if(!empty($terms[$i]))
{
$conditions[$i]="title like '%" . trim($terms[$i]) . "%'";
}
}
$sql_str ="select url, title from fappageinfo where ";
for($i=0;$i<count($conditions);$i++)
{
if($i>0)
{
$sql_str .=" or ";
}
$sql_str .=$conditions[$i] . " ";
}
echo "sql=" . $sql_str . "<br>";
?>
|
I got pretty wordy in there because I usually try to make the code in here as readable as possible. A more concise implementation of that would be:
<?php
$searchstring='transvestite threesome tea party';
$terms=explode(' ',$searchstring);
$whereclause='';
foreach($terms as $term){$whereclause .=(!empty($term)?(!empty($whereclause)?"or ":'') . "title like '%" . trim($term) . "%' ":'');}
$sql_str="select url, title from fappageinfo " . (!empty($whereclause)?'where ' . $whereclause:'');;
echo "sql=" . $sql_str . "<br>\n";
?>
It is exactly the same, but different.
.