Total members 11895 |It is currently Fri Dec 06, 2024 12:38 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This article presents the concept of adding and developing a simple search module to your website based on using PHP programming language. The example presented in this article is about searching in a website for styles and CSS themes. The search page contains two selection boxes and two text fields. The first selection box is for choosing the CMSs type such as PHPBB, Joomla and WordPress. The second selection box is to choose the category of theme styles such as Business, education and Fashion. Now, about the two text fields, the first input text is for style name or a part of it. The second text field is for style number if you already know it. There many cases handle in the example presented in this article :

  • If the user pressed the search button without choosing any CMS or Style Category the results will be and error message."Please choose the type of your style".
  • If the user pressed the search button after only selecting the CMS type, the results will be all the styles for this CMS type.
  • If the user selected the CMS type and Category without entering any text fields they results will be all the styles in selected CMS type filtered by the Category selection.
  • If no results found after running the query this message appears "Sorry, No results found".
  • This is the happy scenario, happens when search query find matches and print it using the echo function.
Following is the main search page which includes the search form :

html code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>search form in php</title>
 
</head>
<body>
<center>
 <table  cellspacing="5">
        <tr>
   <td><form method="get" action="./search.php" name="searchform">
<input type="hidden" name="start" value="0">            
            <select name="template_type_id" class="searchform2">
                <option value="0"> - Type -</option>
          <option value='3' >joomla5</option><option value='2' >phpbb2</option>

<option value='1' >phpbb3</option><option value='4' >wordpress</option> 
           </select>
<select name="template_category_id" id="template_category_id" class="searchform2">

                <option value="0"> - Category -</option>

<option value=Animal>Animal</option><option value=Art>Art</option>

<option value=Books>Books</option><option value=Business>Business</option>

<option value=Cars>Cars</option><option value=Children>Children</option>
<option value=Communications>Communications</option>
<option value=Computing>Computing</option><option value=Dating>Dating</option>
<option value=Education>Education</option><option value=Electronics>Electronics</option>
<option value=Entertainment>Entertainment</option><option value=Fashion>Fashion</option>
<option value=Notebook>Notebook</option><option value=Flowers>Flowers</option>
<option value=Food>Food</option><option value=Full screen>Full screen</option>
<option value=Futuristic>Futuristic</option><option value=Gambling>Gambling</option>
<option value=General>General</option><option value=Health>Health</option>
<option value=Hobby>Hobby</option><option value=Hotel>Hotel</option>
<option value=Industry>Industry</option><option value=Interior>Interior</option>
<option value=Jewelry>Jewelry</option><option value=Law>Law</option>
<option value=Marina>Marina</option><option value=Media>Media</option>
<option value=Military>Military</option><option value=Music>Music</option>
<option value=Night club>Night club</option><option value=Personal>Personal</option>
<option value=Photography>Photography</option><option value=Real estate>Real estate</option>
<option value=Religion>Religion</option><option value=Science>Science</option>
<option value=Services>Services</option><option value=Shopping>Shopping</option>
<option value=Society>Society</option><option value=Spiritual>Spiritual</option>
<option value=Sports>Sports</option><option value=Travel>Travel</option>
<option value=Video Art>Video Art</option><option value=Web design>Web design</option>
<option value=Web hosting>Web hosting</option><option value=Wedding>Wedding</option>         
</select><input class="searchform" type="text" 
name="keywords" value=" - Keywords -" 
onFocus="javascript:if (this.value == ' - Keywords -') this.value='';" 
onBlur="javascript:if (this.value == '') this.value=' - Keywords -';" />
<input class="searchform" type="text" name="number" 
value=" - Number -" onFocus="javascript:if (this.value == ' - Number -') this.value='';" 
onBlur="javascript:if (this.value == '') this.value=' - Number -';" />
 </form> </td><td><img src='Template/Images/Buttons/search.png'  
 onclick='javascript:document.searchform.submit();' class='imglink' ></td>

        </tr>
        </table>
</center>
</body>
</html>
 

Attachment:
File comment: Search Input
search1.gif
search1.gif [ 2.32 KiB | Viewed 40649 times ]

You will notice that we have replaced the input submit button with an image just for better looking, and we applied submit action using JavaScript function. After the user submits the button the search page is called throw the search.php page :
php code
<?php
//require "config.php"; // All database details will be included here
$TYPE_ID=$_GET['template_type_id'];
$folderpath="stylesdata/";
switch($TYPE_ID)
{

case 1:
$html_title = "PhpBB2 Templates | PhpBB2 Themes | Templates Dragon";

break;

case 2:
$html_title = "PhpBB3 Templates | PhpBB3 Themes | Templates Dragon";
break;

default :
$html_title = "PhpBB Templates | PhpBB Themes | Templates Dragon";
break;


}

$STYLE_CATEGORY=$_GET['template_category_id'];
$keywords=$_GET['keywords'];
$pagelink="body/searchbody.php";
include 'Template.php' ;

?>

Which calls the search body page, you become wondering about the usefulness of template.php page and why it is used , actually this search module is part of a bigger project which we have used some design patterns in view in hope to making all the calls managed throw only one page which is template.php and in this page we call all the common sub pages such as headers and footers. We also open the connection to databases in header page ..etc. The search body page , it just simple and easy to understand, we get the parameters sent from the search form and we use them in the MYSQL Queries, then, we fetch all the results back and we present them in a HTML tables. The image below shows a search results example
Attachment:
File comment: Example of search results
search2.gif
search2.gif [ 7.99 KiB | Viewed 40649 times ]

Following is the search body php page:
php code
<div class='content'>
<?php

function GetTypeByID($ID)
{
$query="Select * from type where ID=$ID;";
$results=mysql_query($query);

echo mysql_error();
return $results;

}
@$template_type_id=$_GET['template_type_id'];
@$template_category_id=$_GET['template_category_id'];
@$keywords=$_GET['keywords'];
$subquery=" ";

if($template_type_id=="0"){
echo "<center> Please choose the type of your style </center>";
}
else
{



if($template_type_id!=0){
$Type=GetTypeByID($template_type_id);
$TypeName=mysql_result( $Type,0,'TYPE_NAME');
$folderpath="stylesdata/".$TypeName."/";
$subquery= $subquery."where ID = ".$template_type_id;

if(!$template_category_id==0){
$subquery= $subquery." and CATEGORY = '".$template_category_id."'";
}
if(!($keywords==""||$keywords==" - Keywords -")){
$subquery= $subquery." and STYLE_NAME like '%".$keywords."%'";
}
}else if(!$template_category_id==0){
$subquery= $subquery." where CATEGORY = '".$template_category_id."'";

if(!($keywords==""||$keywords==" - Keywords -")){
$subquery= $subquery." and STYLE_NAME like '%".$keywords."%'";
}
}else if(!($keywords==""||$keywords==" - Keywords -")){
$subquery= $subquery." where STYLE_NAME like '%".$keywords."%'";
}


$query=" SELECT * FROM styles ".$subquery;
//echo"$query";
$result=mysql_query($query);
echo mysql_error();
$num=mysql_num_rows($result);
if($num=="0")
{
echo "<br/><center><h4>Sorry, No results found</h4></center><br/>";
}
else
{
$i=0;
echo "<br/><h4>Search results:</h4><br/>";
echo"<br/><table border=0 cols=%100>";

while($noticia = mysql_fetch_array($result))
{


$image= $folderpath."images/".$noticia['STYLE_ID'].".PNG";
$file= $folderpath."uploads/".$noticia['STYLE_NAME']."_".$noticia['STYLE_ID'].".zip";
if(($i%3)==0)
echo"<tr>";
echo "<td>";

echo " <div class='styleroundcont'>
<div class='styleroundtop'>
<img src='Template/Images/c1.gif'
width='15' height='15' class='corner'
style='display: none' /></div> <center>";

echo "<a style='cursor: hand;' href='phpbb3stylesview.php?styleID=$noticia[STYLE_ID]'>
<strong>$noticia[STYLE_NAME]</strong><br />";
echo "<img name='$image' alt='' src='$image'
border='1'
style='border-color: 777777;'
id = 'tpl_$noticia[STYLE_ID]'/></a><br />";
echo" <script type='text/javascript'>
loadPre('tpl_$noticia[STYLE_ID]',
'Template $noticia[STYLE_ID]',
'http://localhost/mileTemplates2/$image',
430,
442 );
</script>";
echo "Viewed: $noticia[VIEWS]<br />Downloads: $noticia[DOWNLOADS]<br />";
echo " </center><div class='styleroundbottom'>
<img src='Template/Images/c4.gif'
width='15' height='15' class='corner'
style='display: none' /> </div></div>";
echo "</td>";

$i++;
if(($i%3)==0)
echo"</tr>";

}
echo"</tr>";
echo"</table>";
echo"<br />";
}
}
?>
<div>

The results is includes many thanks such as style snapshot, style name, style downloads number and views number. The parameter Number in search form in not implemented the queries if you are interested about it you modify the search body page or remove the input element from the HTML search form. The page include in this article includes the database SQL script , and all the need file to run the example. Note that to change the database connection configuration you need to open Config.php.




Attachments:
File comment: Full example package
PHPSearch.rar [914.96 KiB]
Downloaded 1704 times

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

I suggest you to use Zend_Search_Lucene. It is a fully implemented and fast PHP based full text search engine.


Author:
Newbie
User avatar Posts: 12
Have thanks: 1 time

never tried before? anyway in this article am talking about implementing your own custom search with example

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : How to do a simple search engine for website using PHP
 JSP Search Engine     -  
 Search engine optimization     -  
 A simple search page using Google AJAX Search API     -  
 Servlets/JSP Website search page example     -  
 AJAX Engine     -  
 binary search     -  
 Run query and search from ASP     -  
 Binary search tree C++     -  
 how we can search in filehandling without using structures a     -  
 search in a string and replace     -  



Topic Tags

PHP Database, PHP Forms
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com