ถามเกี่ยวกับการ search แบบหลายๆ key

ไม่ทราบว่าเราจะ  ค้นหา ทีละหลายๆ คำแบบ google ยังไงครับ
ปกติจะใช้แค่  select * from MAIN where $table Like '%$search%'
  มันก้อจะได้แค่คำที่เราต้องการ เช่น "ต้นไม้ ใบหญ้า" 

   ก็จะเป็น บ้านเรามีต้นไม้ ใบหญ้าท้องทุงเขียวขจี

  ถ้าอยากหาคำว่า ต้นไม้และใบหญ้า ในฟิวส์นั้นทั้งหมด

เราจะทำยังไงครับ แบบที่เราพิมพ์ goolge ในช่อง search ว่า ต้นไม้+ใบหญ้า+เขียว

ท่านใดพอจะ share ความรู้ให้ได้มั่งครับ
06 พ.ย. 2551 10 5,414

ขออนุญาติแลกเปลี่ยนครับ
  
    ไม่ทราบว่าผมอ่านความต้องการของเจ้าของกระทู้ถูกหรือเปล่า

 จึงได้แนวคิดมาดังนี้

1. ตัดข้อความที่ค้นหาออกเป็นคำ ๆ โดยหาอะไรบางอย่างในการแบ่งคำออกจากกันเก็บใน array ง่าย ๆ ก็เช่น ช่องว่าง 
    เช่น $arrWord = split($strinput,' ');

2. เอาแต่ละคำจากข้อ 1 มาทำการ เติมคำสั่ง sql  เช่น  
    $strsql = "SELECT * FROM table_name WHERE " ;
  
   for($i=0; $i<count($arrWord);$i++){ 
     $strsql .= " column_name LIKE '%".$arrWord[$i]."%'  OR";
   }
   $strsql=substr($strsql,0,strlen($strsql)-2)  // ตัด OR ตัวสุดท้ายออก
     
 

ผิดถูกต้องขออภัย  ข้าน้อยด้อยปัญญาจริง ๆ

#1

ใช่เลยครับ ขอบพระคุณอย่างยิ่ง 

ปล. มาบรอดนี้ทีไรงานไหลลื่นตลอดครับ ^^
#2

1.สำหรับแยกตัวแปร search ออกเป็น array ที่โดยทั่วไปมักส่งมาโดยคั่นระหว่างตัวอักษร ด้วย ช่องว่าง
<?php
$searchs = explode( ' ' , $search  );
?>

2.รวมค่าที่ search ได้เข้ากับ query ซี่งโดยทั่วไปมักใช้ร่วมกับ OR ซึ่งหมายถึงมีคำใดคำหนึ่งที่ระบุ หรือ AND ใช้สำหรับ การค้นหาที่ต้องการผลลัพท์ที่มีคำทั้งหมด
<?php
foreach ( explode( ' ' , $search  ) as $value )
{
  $searchs[] = "col1 LIKE '%$value%' OR col2 LIKE '%$value%'"; // ค้นหาจาก 2 ฟิลด์ ใช้ OR
};
$sql = "SELECT * FROM table WHERE ".implode( " OR " , $searchs).";"; // อาจใช้ AND ถ้าต้องการผลลัพท์ทุกคำ
?>
#3

$arrWord = split(' ',$strinput);

   $strsql = "SELECT * FROM table_name WHERE " ;
  
   for($i=0; $i<count($arrWord);$i++){ 
     $strsql .= " column_name LIKE '%".$arrWord[$i]."%'  OR";
   }
   $strsql=substr($strsql,0,strlen($strsql)-2) ;
#4

ขอบคุณอาจารย์ ครับ ที่มาช่วย ทำให้ได้ข้อมูลที่แม่นยำขึ้น
#5

ขอบพระคุณอาจารย์ครับ

   ได้ความรู้เพิ่มขึ้นครับ... ยังมีคำสั่งให้ใช้งานต่าง ๆ อีกมากมายจริง ๆ
 โค๊ดข้างต้นของผมนั้นผิดพลาดอย่างแรงทีเดียว ต้องเร่งศึกษาให้มากขึ้น

Definition and Usage

The explode() function breaks a string into an array.

Syntax

 explode(separator,string,limit)

Parameter Description
separator Required. Specifies where to break the string
string Required. The string to split
limit Optional. Specifies the maximum number of array elements to return


Tips and Notes

Note: Separator cannot be an empty string.


Definition and Usage

The implode() function returns a string from the elements of an array.

Syntax

 implode(separator,array)

Parameter Description
separator Optional. Specifies what to put between the array elements. Default is "" (an empty string)
array Required. The array to join to a string


Tips and Notes

Note: The implode() function accept its parameters in either order. However, for consistency with explode(), you should use the documented order of arguments.

Note: The separator parameter of implode() is optional. However, it is recommended to always use two parameters for backwards compatibility.


---------------------------------------------------------------
array explode(string separator,string str[,int limit]);
 
string separator = ตัวอักษรที่ใช้แบ่งระหว่างสมาชิกของ array
string str = ข้อความที่ต้องการตัดแบ่งให้เป็น array
int limit  = ตัวเลขกำหนดจำนวน array สูงสุดที่จะคืนกลับไป


string implode([string seperator,]array arr);
string seperator= ตัวอักษรที่ใช้คั่นระหว่างสมาชิกของ array
array arr = object array ที่ต้องการรวมเป็นข้อความหนึ่งเดียว

ผิดถูกอย่างไร ต้องขออภัยด้วยครับ


อ้างอิง: http://www.w3schools.com/...c_string_implode.asp
           http://www.w3schools.com/...c_string_explode.asp

#6

อีกนิดได้ไหมครับ ถ้าเราจะทำ ไฮไลท์ ทั้ง หมดที่เราหาออกมาได้จะทำยังไงครับ

เดิมใช้แค่ str_replace("$search","<u>$search</u>", odbc_result($fdn,3));

มันไฮไลท์ได้แค่อันเดียว
#7

วนลูป highlight ทีละคำ เหมือนตอน query ครับ
#8

ขอบคุณครับ
#9

ไม่ทราบว่าวนแบบไหนครับ มัน ทำไฮไลท์ แต่อันสุดท้ายอ่าครับ ขอยกตัวอย่างได้ไหมครับ
#10
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^