|
 |
|
ขอความช่วยเหลือดวนครับ จะทำ autocomplete จากโค๊ดตัวนี้เราสามารถทำให้ ค้นหาจาก 2 field ได้ไหมครับ |
|
 |
|
|
 |
 |
|
จากโค๊ดตัวนี้เราสามารถทำให้ ค้นหาจาก 2 field ได้ไหมครับ
Code (PHP)
<?php
// เชื่อมต่อฐานข้อมูล
$link=mysql_connect("localhost","root","123456") or die("error".mysql_error());
mysql_select_db("database_name",$link);
mysql_query("set character set utf8");
$q = $_GET["q"];
$pagesize = 50; // จำนวนรายการที่ต้องการแสดง
$table_db="article"; // ตารางที่ต้องการค้นหา
$find_field="arti_topic"; // ฟิลที่ต้องการค้นหา
$sql = "select * from $table_db where locate('$q', $find_field) > 0 order by locate('$q', $find_field), $find_field limit $pagesize";
$results = mysql_query($sql);
while ($row = mysql_fetch_array( $results )) {
$id = $row["arti_id"]; // ฟิลที่ต้องการส่งค่ากลับ
$name = ucwords( strtolower( $row["arti_topic"] ) ); // ฟิลที่ต้องการแสดงค่า
// ป้องกันเครื่องหมาย '
$name = str_replace("'", "'", $name);
// กำหนดตัวหนาให้กับคำที่มีการพิมพ์
$display_name = preg_replace("/(" . $q . ")/i", "<b>$1</b>", $name);
echo "<li onselect=\"this.setText('$name').setValue('$id');\">$display_name</li>";
}
mysql_close();
?>
Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2009-11-24 23:10:15 |
By :
jiebspy |
View :
975 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|
|

|
Load balance : Server 04
|