By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,919 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,919 IT Pros & Developers. It's quick & easy.

how to built a search engine

P: 34
I am try to using PHP to built a search engine. My problem how to make a search engine that user not only can insert the keyword in text field but also can minimize the searching area by clicking at the option i provide.The most inportant is how to make the relationship between the option user choose and the keyword at test field
Sep 22 '06 #1
Share this Question
Share on Google+
4 Replies


P: 16
it all depends on the type of search you want to do. are you having customers search a product, users, or are you trying to take on google?

say you are searching a product, your SQL would look something like:

$product = 'web dev book'

SELECT prodId, prodPrice,
prodDesc, prodTitle
FROM products
WHERE prodTitle LIKE '%$product%'

this is the most basic kind of search. to break it down, you are selecting the fields you want from the table 'products,' then you are matching $product to prodTitle using LIKE, which allows wildcards (%), so any title that has $product in it anywhere. the downside to this is if $product == 'a', it will return any title with 'a' anywhere in it.

if you want to have user selected query options to narrow down their selection, you could do:

$product = 'web dev book'

if ( $priceSwitch ) {
$where .= "AND prodPrice = '$prodSwitch'";
}
if ( $hardcoverSwitch ) {
$where .= "AND prodCover = 'hardcover'";
}
SELECT prodId, prodPrice,
prodDesc, prodTitle
FROM products
WHERE prodTitle LIKE '%$product%'
$where

this will append your $where to the end of the mysql query, adding the extra switches you want. you could also use OR instead of AND if it fits, and this same concept can be used elsewhere in the script to make it more dynamic.

if you are daring, you can use REGEX:

$product = '^web'

SELECT *
FROM products
WHERE prodTitle REGEX '$product'

this allows regex characters, so in this case it would look for any title that starts with 'web', as ^ is regex for the start of a line. there is some danger in this, as starting with a ? will draw and error, or having a [ without a ], so youll need to parse those. but this should at least get you off to a start.

let me know if you have more questions.

-tim
Sep 22 '06 #2

P: 34
Below is my search engine code. Now i want to add on two radio option so that the searching can be minimize. What should i do?



<?php session_start();?>
<h2>Search</h2>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Search for:
<input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="Operasi">Alat Operasi</option>
<Option VALUE="Balai">Alat Balai</option>
<Option VALUE="Komunikasi">Alat Komunikasi</option>


</Select>

<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?
//This is only displayed if they have submitted the form

if ($searching =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Otherwise we connect to our Database
mysql_select_db("inventory") or die(mysql_error());

// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM listofitem WHERE upper($field) LIKE'%$find%'");


echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>NamaPeralatan</th><th>Motor</th><th>Alat Operasi</th><th>Alat Balai</th><th>Alat Komunikasi</th> </tr>";

//And we display the results
while($result = mysql_fetch_array( $data ))
{


echo "<tr><td>";
echo $result['ID'];
echo "</td><td>";
echo $result['NamaPeralatan'];
echo "</td><td>";
echo $result['Motor'];
echo "</td><td>";
echo $result['Operasi'];
echo "</td><td>";
}
echo "</table>";



//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
Sep 23 '06 #3

P: 16
2 things i need to know.

1) what is this a search for, if i understand the language i can help more

2) what do you want the radio buttons to do?

let me know this and i can tell you how to do it
Sep 23 '06 #4

P: 34
2 things i need to know.

1) what is this a search for, if i understand the language i can help more

2) what do you want the radio buttons to do?

let me know this and i can tell you how to do it
In the column of Motor it will be catagorize to WithMotor and UnMotor tools.Thus the function of radio is let user can choose either the tools are type of WithMotor or UnMotor.

When the user key in the keywords in the textfield, the engine will search either the term is from WithMotor or UnMotor, then what categories it come from whether from OperationTool, OfficeTools or ComunicaionTools


CREATE TABLE `listofitem` `ID` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`NameOfTool` VARCHAR( 30 ) NOT NULL ,
`Motor` VARCHAR( 10 ) NULL DEFAULT NULL ,
`Operation` VARCHAR( 30 ) NULL DEFAULT NULL ,
`Office` VARCHAR( 30 ) NULL DEFAULT NULL ,
`Comunication` VARCHAR( 30 ) NULL DEFAULT NULL

<?php session_start();?>
<h2>Search</h2>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Search for:
<input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="Operation">OperationTool</option>
<Option VALUE="Office">OfficeTools</option>
<Option VALUE="Comunication">ComunicationTools</option>


</Select>

<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?
//This is only displayed if they have submitted the form

if ($searching="yes")

echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Otherwise we connect to our Database
mysql_select_db("inventory") or die(mysql_error());

// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM listofitem WHERE upper($field) LIKE'%$find%'");


echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>NameOfTools</th><th>Motor</th><th>Alat Opeation</th><th>OfficeTools</th><th>ComunicationTools</th> </tr>";

//And we display the results
while($result = mysql_fetch_array( $data ))
{


echo "<tr><td>";
echo $result['ID'];
echo "</td><td>";
echo $result['NameOfTools'];
echo "</td><td>";
echo $result['Motor'];
echo "</td><td>";
echo $result['Operation'];
echo "</td><td>";
echo $result['Comunication'];
echo "</td><td>";
}
echo "</table>";



//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;

?>
Sep 23 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.