سلام دوستان،
میشه لطف نموده این کلاس جستجو را انعطاف پذیر درست نمایند، یعنی بشه برای چند جدول کار نماید و هم چنان فیلد قسمت رابطه ...

چون فعلا برای یک جدول کار مینماید

نقل قول نوشته شده توسط hidensoft مشاهده تاپیک
ساخت یک موتور جستو جو گر سریع و کارآمد یکی از دق دقه های یک برنامه نویسه ، من حتی قبل از اینکه یک پروژه رو شروع کنم روس سرچ فکر می کنم . .. امروز یک کلاس بسازیم که به سریع ترین وجه ممکن و به بهترین نحوه به ما جواب مورد نظرمون رو بده ، از همه مهم تر اصولی باشه و قابل ارتقاع و سفارشی باشه ..

class.search.php

<?php

class search_engine
{
function search_engine($mysql)
{
# set database connection
$this->host = $mysql[0];
$this->username = $mysql[1];
$this->password = $mysql[2];
$this->database = $mysql[3];
$this->link = mysql_connect($this->host,$this->username,$this->password) or die(mysql_error());
$this->db_selected = mysql_select_db($this->database,$this->link) or die(mysql_error());
$this->found = array();
}
function set_table($table)
{
# set table
$this->table = $table;
}
function set_keyword($keyword)
{
# set keywords
$this->keyword = explode(" ", $keyword);
}
function set_primarykey($key)
{
# set primary key
$this->key = $key;
}
function set_fields($field)
{
# set fieldnames to search
$this->field =$field;
}
function set_dump()
{
# var dump objects
echo '<pre>';
var_dump($this->found);
echo '</pre>';
}
function set_total()
{
# total results found
return sizeof($this->found);
}
function set_result()
{
# find occurence of inputted keywords
$key = $this->key;
for ($n=0; $n<sizeof($this->field); $n++)
{
for($i =0; $i<sizeof($this->keyword); $i++)
{
$pattern = trim($this->keyword[$i]);
$sql = "SELECT * FROM ".$this->table." WHERE `".$this->field[$n]."` LIKE '%".$pattern."%'";
$result = mysql_query($sql);
while ($row = mysql_fetch_object($result) AND !empty($pattern))
{
$this->found[] = $row->$key;
}
}
}
$this->found = array_unique($this->found);
return $this->found;
}
}
?>
process.php

<?php

require_once 'class.search.php';

$config = array('localhost','root','','database');
$table = 'bizmain';
$key = 'biz_id';
$fields = array('biz_name','biz_address','biz_cat');

$keyword = $_POST['keyword'].' '.$_POST['location'];

$found = new search_engine($config);
$found->set_table($table);
$found->set_primarykey($key);
$found->set_keyword($keyword);
$found->set_fields($fields);

$result = $found->set_result();
print_r($result);

?>
form.php

<form method=post action=process.php>
<table border=1>
<tr>
<td>Search by name, category or keywords</td>
<td><input name=keyword></td>
</tr>
<td>&nbsp;</td>
<td><input type=submit value=Submit></td>
</table>
</form>
sql

CREATE TABLE `bizmain` (
`biz_id` mediumint(8) unsigned NOT NULL auto_increment,
`biz_name` varchar(100) default NULL,
`biz_address` varchar(255) default NULL,
`biz_phone` varchar(100) default NULL,
`biz_email` varchar(100) default NULL,
`biz_url` varchar(100) default NULL,
`biz_cat` varchar(100) default NULL,
PRIMARY KEY (`biz_id`)
) ;

INSERT INTO `bizmain` (`biz_id`, `biz_name`, `biz_address`, `biz_phone`, `biz_email`, `biz_url`, `biz_cat`) VALUES (1, 'LocalFilipino.com', 'Makati, Philippines', '(632) 848-0886', 'info@localfilipino.com', 'http://www.localfilipino.com', 'Advertising'),
(2, 'Liberty Realty', '2451 S. Buffalo Drive, Suite 145, Las Vegas, NV 89117', '(702) 248-8899', 'info@libertyrealty.com', 'http://www.libertyrealty.com', 'Real Estate, Realty'),
(3, 'FRS Philippine Freight', '5960 Spring Mtn Rd, 3-D<br> Las Vegas, NV 89146', '(702) 253-7555', 'info@frsphilippines.com', 'http://www.frsphilippines.com', 'Freight, Cargo');

Output

output.jpg