Jump to content

search script | need to query the database and tell me if there is no matches


dazzclub

Recommended Posts

Hi guys and girls,

 

I am having trouble finding out wether the query made no matches rather than just displaying the matches.

 

So what I would want is for the search term to look at the database, and if there's not matches then say, "No match for your search term"

 

function search_applications() {

global $dbc;

echo '<form method="post" action="" id="search">'. "\n";
echo '<label for="search_applications">Search for an application</label>'. "\n";
echo '<input type="text" name="search_applications" />'. "\n";
echo '<input type="submit" name="submit" />'. "\n";
echo '</form>'. "\n";


if(isset($_POST['submit'])){ 

$search_applications = $_POST['search_applications'];


if ($search_applications == "") 
{ 
echo "<p>You forgot to enter a search term";  
} 

//if (preg_match("/\b$search_applications\b/i")){ 
if(preg_match("/^[  a-zA-Z]+/", $search_applications)){ 


//for testing the variables
//echo $search_applications;

$sql="SELECT title, category, content FROM distributors_content WHERE  title LIKE '%" . $search_applications . "%' OR category LIKE '%" . $search_applications  ."%' OR content LIKE '%" . $search_applications  ."%'";
$result=mysqli_query($dbc, $sql); 
//-create  while loop and loop through result set 
while($row=mysqli_fetch_array($result)){ 
          $title  =$row['title']; 
       $category=$row['category']; 
       $content=$row['content']; 
       
//-display the result of the array 
echo "<ul>\n"; 
echo '<li> <a href="index.php?article='.$row['title'].'&&page=application" title=""> '.$title . ' ' . $category .  '</a></li>'."\n"; 
echo "</ul>";
}
}else{

echo "<p>Enter another search term</p>";
}
} 	 
} 

 

Any pointers would be good :)

 

Thank you

Thank you so much, it worked..

 

Here's the full code if anyone finds it usefull...

 

function search_applications() {

global $dbc;

echo '<form method="post" action="" id="search">'. "\n";
echo '<label for="search_applications">Search for an application</label>'. "\n";
echo '<input type="text" name="search_applications" />'. "\n";
echo '<input type="submit" name="submit" />'. "\n";
echo '</form>'. "\n";


if(isset($_POST['submit'])){ 

$search_applications = $_POST['search_applications'];


if ($search_applications == "") 
{ 
echo "<p>You forgot to enter a search term";  
} 

//if (preg_match("/\b$search_applications\b/i")){ 
if(preg_match("/^[  a-zA-Z]+/", $search_applications)){ 


//for testing the variables
//echo $search_applications;

$sql="SELECT title, category, content FROM distributors_content WHERE  title LIKE '%" . $search_applications . "%' OR category LIKE '%" . $search_applications  ."%' OR content LIKE '%" . $search_applications  ."%'";
$result=mysqli_query($dbc, $sql); 

if(mysqli_num_rows($result) == 0){
echo "There were no matches!";
}	 

//-create  while loop and loop through result set 
while($row=mysqli_fetch_array($result)){ 
          $title  =$row['title']; 
       $category=$row['category']; 
       $content=$row['content']; 
       
//-display the result of the array 
echo "<ul>\n"; 
echo '<li> <a href="index.php?article='.$row['title'].'&&page=application" title=""> '.$title . ' ' . $category .  '</a></li>'."\n"; 
echo "</ul>";
}
}
} 
}

 

Thanks again

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.