Hi,
I need to make a search page for book using PHP and MySQL. I've already have the script that I've got from one site.
It run very well. I can type what I need to search then click which category to be search using textbox such as Title or Author.
I want to modify it so I can search the Title with different name, search the Author with different name.
For example:tick Title with input Visual and tick Author with input Larry.
So, the output will be every Title that have word Visual and/or Author that have Word Larry.
Right now, how I'm gonna start this modify things. I'm still newbie and need a lot of guidance.Thanks.
Type the input here, but I need to add another input also:
<input name="search" type="text" value="<?php echo isset($searchTerms)?$searchTerms:''; ?>" />
These are textboxes:
<tr>
<th scope="row">Carian di :</th>
<td width="74">Tajuk:
<input type="checkbox" name="Title" value="on" <?php echo isset($_GET['Title'])?"checked":''; ?> /></td>
<td width="100">Kategori:
<input type="checkbox" name="Category" value="on" <?php echo isset($_GET['Category'])?"checked":''; ?> /></td>
<td width="89">Penulis:
<input type="checkbox" name="Author" value="on" <?php echo isset($_GET['Author'])?"checked":''; ?> /></td>
</tr>
These are the coding that I need a lot of modify I think:
<?php
if (isset($_GET['search'])) {
$searchTerms = trim($_GET['search']);
$searchTerms = strip_tags($searchTerms); // remove any html/javascript.
if (strlen($searchTerms) < 3) {
$error[] = "Carian perlu melebihi 2 huruf."; //need to be more than 2 alphabets
}else {
$searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
}
// If there are no errors, lets get the search going.
if (count($error) < 1) {
$searchSQL = "SELECT Title, Category, Author, Barcode FROM doc WHERE ";
// grab the search types.
$types = array();
$types[] = isset($_GET['Title'])?"`Title` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['Category'])?"`Category` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['Author'])?"`Author` LIKE '%{$searchTermDB}%'":'';
$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
if (count($types) < 1)
$types[] = "`Title` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked
$andOr = isset($_GET['matchall'])?'AND':'OR';
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `Title`"; // order by title.
$searchResult = mysql_query($searchSQL) or die("The was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
if (mysql_num_rows($searchResult) < 1) {
$error[] = "Tiada paparan untuk carian <span id=\"term\">{$searchTerms}</span>.";//when there is no search found
}else {
//$results = array(); // the result array
$i = 1;
echo "Carian anda ialah <span id=\"term\">{$searchTerms}</span> memaparkan: <br/><br/>"; //Your search .... show:
echo "<table align=\"center\" border=\"1\ cellpadding=\"2\" cellspacing=\"0\">\n";
print"<tr>";
echo "<th bgcolor=\"#FF6600\">Bil</th>\n"; // No.
echo "<th bgcolor=\"#FF6600\">Tajuk;Penulis</th>\n"; //Title and Author
echo "</tr>\n";
while ($row = mysql_fetch_assoc($searchResult)) {
print"<tr>";
print "<td rowspan=\"2\"><div align= \"center\">$i</div></td>\n";
print "<td><p>$row[Title]</p></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td><p>$row[Author]</p></td>\n";
print"</tr>\n";
$i++;
}}}}
?>