I am trying to figure out the best way to configure my search bar. It will be searching my website for posts members have posted.

I tried this but this does not work. Here is the PHP file that I use as the form action. Do you know how or a good tutorial that will show me to display the username who posted it and the content in a table?

This is search.php:

< ?php 
//This will get keywords I think
$keyword=trim($_POST["keyword"]);
 //check if the keyword is empty
 if($keyword==""){ echo"No posts with these keywords"; exit; } ?>

In my html I have the text field where you enter the keywords and I have the input name="keyword" . What do I need to do, any suggestions or good tutorials : )

Recommended Answers

All 2 Replies

Member Avatar for diafol

get an array of keywords from the textbox -

$raw = $_POST['keyword'];
$clean = trim(mysql_real_escape_string($raw));
$k_array = explode(" ", $clean); //assuming space is the separator

//this needs more tests - but gives you an idea
$clause = "WHERE ";
$i = 0;
foreach($k_array as $word){
  if($i > 0)$clause .= " OR ";
  $clause .= "field1 LIKE '%$word%'";
  $i = $i + 1;
}

$query = mysql_query("SELECT field1,field2 FROM table1 $clause");

Top of my head, not tested, sure there's a more efficient way too.

commented: Ardav gives helpful responses : ) +1

Thanks I will try this : )

get an array of keywords from the textbox -

$raw = $_POST['keyword'];
$clean = trim(mysql_real_escape_string($raw));
$k_array = explode(" ", $clean); //assuming space is the separator

//this needs more tests - but gives you an idea
$clause = "WHERE ";
$i = 0;
foreach($k_array as $word){
  if($i > 0)$clause .= " OR ";
  $clause .= "field1 LIKE '%$word%'";
  $i = $i + 1;
}

$query = mysql_query("SELECT field1,field2 FROM table1 $clause");

Top of my head, not tested, sure there's a more efficient way too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.