The list of all PHP and Mysql posts

How to make a search page

Search boxes are importent part of a forum . User wants to find a topic very fast , he doesn't want to see all of the pages to find his favorite thing. The picture below shows a search box :



Put the following codes in "search.php" , they will show a form for search on the screen.

code:

<?php
include "auth.inc";  // we check that if user has loged in or not

include "funcs.php"; // the functions look like : connect (to connect to the database) and creatcode (for verification text) can be put in a file and then we include that file wherever it is required.

?>

<?php
include "auth.inc";
?>
<a href="logout.php">log out</a>
<br>

// the following html codes show a menu-bar on the page . they have been created by Frontpage . You can omit them.

<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title> search </title>
</head>
<body bgcolor="#99FF66" >
<table border="0" cellpadding="0" cellspacing="0" width="1055" height="625">
 <!-- MSTableType="layout" -->
 <tr>
  <td valign="top" colspan="3" height="148">
  <!-- MSCellType="ContentHead" -->
  <p align="center">
&nbsp;
<br>
<!--webbot bot="Navigation" S-Type="sequence" S-Orientation="horizontal" S-Rendering="graphics" S-Theme="poetic 0000" B-Include-Home="FALSE" B-Include-Up="FALSE" U-Page="sid:1001" --><a href="index.php"><img src="_derived/index.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="index.php" align="middle"></a> <a href="about.php"><img src="_derived/about.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="about.php" align="middle"></a> <a href="search.php"><img src="_derived/search.php_cmp_poetic000_hbtn.gif" width="154" height="37" border="0" alt="search.php" align="middle"></a><!--webbot bot="Navigation" i-checksum="38819" endspan --><br>
&nbsp;</p>
  <p><font size="6"><b> search </b></font></td>
 </tr>
 <tr>
  <td valign="top" width="300">
  <!-- MSCellType="NavBody" -->

<br>
<font size="3"><b> main titles </b></font>
<?php

// the following is just a list of forums

echo"
<br>
<br>
1.<a href=\"showforum.php?link=satire\">satire</a>
<br>
<br>
2.<a href=\"showforum.php?link=drama\">drama</a>
<br>
<br>
3.<a href=\"showforum.php?link=tragic\">tragic</a>
<br>
<br>
4.<a href=\"showforum.php?link=joke\">joke</a>
<br>
<br>
5.<a href=\"showforum.php?link=others\">others</a>
";
?>
  &nbsp;</td>
  <td valign="top" width="747">
  <!-- MSCellType="ContentBody" -->
  <p align="justify"><b><font size="4">
<br>
<br>
<br>
<?php
if($_GET['q'] == "" or !isset($_GET['q']))
{

// This condition checks that if the search box is empty or not . If no search has begun the search box and the search button are shown , unless the results of search are shown.

?>
<form method="get" action="search.php">

// the search form is sent to "search.php" again.

<p>
<input type="text" name="q" size="20">
<input type="submit" value="Search" name="searchBtn">
</p>
</form>
<?php
}else{

// if the search box is not empty , we do a search over the database by the entered word.

// some prosses over the entered word

$key = $_GET['q'];
$key = htmlspecialchars($key); // change html characters in to regular text , this is an anti-hack process

$key = trim($key); // trim omits "space" characters from two sides of a text


// omit the dangerous characters from the entery .

$key = str_replace("'", "", $key);
$key = str_replace("\\", "", $key);

if(ereg("[^a-zA-z0-9\ ]",$key)){

// here we check the word format that it contains only numbers and letters.

echo "incorrect format for search phrase, just use letters , numbers and space";
echo "<br> <a href = \"search.php\" >Search again</a> <br>";
exit;
}
connect();

// search the posts titles for the entered word.
// Notice to this query , '%" means whatever.

$sql = "SELECT * FROM topics WHERE title LIKE '%".$key ."%' ";
$SearchResult = mysql_query($sql);

$TotalResults = mysql_num_rows($SearchResult);
if($TotalResults == 0){

// if there's no result show the message "no match"

echo "no matches";
echo "<br> <a href = \"search.php\" >Search again</a> <br>";
exit;
}
for($i = $TotalResults - 1; $i != -1; $i-- )
{

// print the search results on the screen .

$TextTitle = mysql_result($SearchResult, $i, 3);
$Textauthor = mysql_result($SearchResult, $i, 0);
$Textlink = mysql_result($SearchResult, $i, 5);


echo "<a href=\"showtopic.php?Textlink=" .$Textlink. " \">" . $TextTitle . "</a>";
echo "<br> <br>";
}
}
?>
</td>
  <td valign="top" height="427" width="147">
  <!-- MSCellType="NavBody2" -->
  &nbsp;</td>
 </tr>
 <tr>
  <td valign="top" colspan="3" height="50">
  <!-- MSCellType="ContentFoot" -->
<br>
<br>
<p align="center">
  <?php

  echo get_pages_html(); // you can add page numbering to search results but I haven't done it here , this line is just a draft and isn't necessarry

  ?>
  &nbsp;</td>
 </tr>
</p>
</table>
</body>
</html>
<p>
<br>
</p>
<html dir="ltr">


There's enough comments between codes , so I don't explain more.
You can read the following related posts to understand codes here better:

http://webprogram4beginners.blogspot.com/2011/02/php-orders-to-work-with-database-and.html

http://webprogram4beginners.blogspot.com/2011/04/how-to-show-certain-post-by-user-demand.html

2 comments: