November 23, 200718 yr Hello i have recently been learning Php, i have successfully created signup/login/logout/email submit forms using PHP but i am having real trouble trying to create or find a working example of a search engine that uses php! I have tried google but only appear to find rubbish, either has bit n bobs of code & no form, or has the form & no instructions that explain lamens terms! I love larning but i need examples to see how things are done & hopefullt say (Oh yer i no ow it works now) get me drift? Anyone please please please got a example of a search engine with a form so i can see how its done?? dont matter how small just enough so i can adapt from there and learn peace by peace. Cheers, Take Care All, Andy.
November 23, 200718 yr EDIT: I assumed you meant a search engine for the entire site, rather than say a list of products in a database, so take the following with that in mind. Depends Would the search engine be for a large site or a small site? Databased or non-databased? Will you be using a shared hosting account, or do you have control of the server? Do you need to handle the search yourself, or would Google Custom Search be enough? A small site means you could use something that goes through all of the pages of the site every time somebody searches. It's not very efficient, but you could get away with it if you've not got hundreds of pages and your site isn't incredibly popular. It's fairly easy to set up. If it's a databased site (i.e CMS, blog, forum, something you've put together), you could look at a MySQL full-text search. Be sure to stem your words first. If it's a large, non-databased site, you're probably going to have to write a crawler, and either put results into MySQL, or use something like Lucene. Anyway, as you can see, there's no easy answer Do you want something that just works, is easy to setup and get your head around, or do you want to go all out and build something immense?
November 23, 200718 yr Author <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>search</title> <style type="text/css"> <!-- #nav { margin: 1px; padding: 0; list-style: none; width: 200px; float:right; background-color: #CCFFCC } --> </style> </head> <body> <?php $link = mysql_connect("localhost","root","longstand1"); mysql_select_db("dbsignup", $link) or die("Unable to select database"); # $selectSql1 = "SELECT * FROM customer WHERE gender = '".mysql_real_escape_string($_POST['gender'])."' AND area = '".mysql_real_escape_string($_POST['area'])."'"; $result = mysql_query($selectSql1, $link) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { ?> <?php echo '<pre>'; print_r($row); echo '</pre>'; } mysql_close($link); ?> <form action="Search_engine.php" method="post"> <table width="323" height="250" border="5" cellpadding="0" cellspacing="0"> <tr> <td width="155"><div align="right">Gender:</div></td> <td width="154"><label> <select name="gender" id="gender"> <option>Male</option> <option>Female</option> </select> </label></td> </tr> <tr> <td><div align="right">Age:</div></td> <td><label> <select name="age" id="age"> <option>18</option> <option>19</option> <option>20</option> <option>21</option> <option>22</option> <option>23</option> <option>24</option> <option>25</option> <option>26</option> </select> </label></td> </tr> <tr> <td><div align="right">Height</div></td> <td><label> <select name="height_one" id="height_one"> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> </select> To <select name="height_two" id="height_two"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> <option>11</option> </select> </label></td> </tr> <tr> <td><div align="right">Hair Colour:</div></td> <td><label> <select name="hair_colour" id="hair_colour"> <option>Black</option> <option>Brown</option> <option>Blonde</option> <option>Red</option> <option>Auburn</option> <option>Other</option> </select> </label></td> </tr> <tr> <td><div align="right">Area:</div></td> <td><label> <select name="area" id="area"> <option>England-Bath Avon</option> <option>England-Bedforshire</option> <option>England-Berkshire</option> </select> </label></td> </tr> <tr> <td><div align="right">Work Interested:</div></td> <td><label> <select name="desired_work" id="desired_work"> <option>Look-A-Likes</option> <option>Models & Glamour</option> <option>Actors & TV Extras</option> <option>Singers & Vocalists</option> <option>Theatre & Musicals</option> <option>Producers&Aegents</option> </select> </label></td> </tr> </table> <input type="submit" /> </form> </body> </html> EDIT: I assumed you meant a search engine for the entire site, rather than say a list of products in a database, so take the following with that in mind. Depends Would the search engine be for a large site or a small site? Databased or non-databased? Will you be using a shared hosting account, or do you have control of the server? Do you need to handle the search yourself, or would Google Custom Search be enough? A small site means you could use something that goes through all of the pages of the site every time somebody searches. It's not very efficient, but you could get away with it if you've not got hundreds of pages and your site isn't incredibly popular. It's fairly easy to set up. If it's a databased site (i.e CMS, blog, forum, something you've put together), you could look at a MySQL full-text search. Be sure to stem your words first. If it's a large, non-databased site, you're probably going to have to write a crawler, and either put results into MySQL, or use something like Lucene. Anyway, as you can see, there's no easy answer Do you want something that just works, is easy to setup and get your head around, or do you want to go all out and build something immense? Thanks you very much for replying sir! My site is Php based, and i need the search engine to search the fields in my mysql database, i am still learning Php so i get confused real quick, would you be kind enough to send me a example?? I can send you my database & search engine that i put together but it displays there results in an array (Really looks unproffessional), i need the search engine to only display 4 ofthe resulst out of the database, (First name/Age/Area/Tallent. Really stuck on this one to the point of stress.. Could you help me fine tune it?? Here is my form & Php code: Here is my sql databse: CREATE DATABASE IF NOT EXISTS dbsignup; USE dbsignup; CREATE TABLE `customer` (`customer_id` INT( 3 ) NOT NULL AUTO_INCREMENT, `first_name` VARCHAR(30) NOT NULL , `last_name` VARCHAR(30) NOT NULL , `address` VARCHAR(70) NOT NULL , `city` VARCHAR(40) NOT NULL , `area` VARCHAR(50) NOT NULL , `email` VARCHAR(70) NOT NULL , `phone` VARCHAR(20) NOT NULL , `user_name` VARCHAR(30) NOT NULL , `password` VARCHAR(30) NOT NULL , `work_location` VARCHAR(60) NOT NULL , `agency_name` VARCHAR(70) NOT NULL , `agency_number` VARCHAR(30) NOT NULL , `skills` VARCHAR(500) NOT NULL , `about_me` VARCHAR(500) NOT NULL , `myspace_address` VARCHAR(50) NOT NULL , `hotmail_yahoo_address` VARCHAR(50) NOT NULL , `gender` VARCHAR(10) NOT NULL , `age` VARCHAR(3) NOT NULL , `height_one` VARCHAR(20) NOT NULL , `height_two` VARCHAR(20) NOT NULL , `build` VARCHAR(20) NOT NULL , `eye_colour` VARCHAR(20) NOT NULL , `hair_colour` VARCHAR(30) NOT NULL , `desired_work` VARCHAR(100) NOT NULL , `image_one` VARCHAR(10) NOT NULL , `image_two` VARCHAR(10) NOT NULL , `image_three` VARCHAR(10) NOT NULL , `image_four` VARCHAR(10) NOT NULL , UNIQUE ( `customer_id` ) ); Please help, as you can se am a tryer am just lost at the moment??
November 23, 200718 yr I haven't any examples I can give, I'm afraid, but I've had a quick go at your search form. Should be helpful - it's not complete, but it's something you can use as a base. By the way, I notice you're using VARCHAR everywhere in your database - this will make it difficult when searching through numeric fields. You should use INTs instead. <?php mysql_connect('localhost', 'root', ''); if (mysql_errno()) { trigger_error('Couldn\'t select database - '.mysql_error()); } mysql_select_db('dbsignup'); if (mysql_errno()) { trigger_error('Couldn\'t select database - '.mysql_error()); } $searchSubmitted = false; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $searchSubmitted = true; $criteria = array(); if (!empty($_POST['gender'])) { // Gender selected - gender is a string $criteria[] = 'gender = \''.mysql_real_escape_string($_POST['gender']).'\''; } if (!empty($_POST['age'])) { // Age selected - age can only be a number $criteria[] = 'age = '.((int) $_POST['age']); } if (!empty($_POST['height_one']) && !empty($_POST['height_two'])) { // Both height_one and height_two are selected $criteria[] = 'age BETWEEN '.((int) $_POST['height_one']).' AND '.$_POST['height_two']; } else if (!empty($_POST['height_one'])) { // Only height_one was selected $criteria[] = 'age >= '.((int) $_POST['height_one']); } else if (!empty($_POST['height_two'])) { // Only height_two was selected $criteria[] = 'age <= '.((int) $_POST['height_two']); } $sql = 'SELECT * FROM customer'; if (count($criteria) > 0) { // Only add a where clause if some criteria have been selected. // If no criteria are selected, show all results. $sql .= ' WHERE ('.implode(') AND (', $criteria).')'; } // This is for debugging - remove it when you're happy with how things work print '<pre>'.htmlspecialchars($sql).'</pre>'; $result = mysql_query($sql); if (mysql_errno()) { trigger_error('Query failed - '.mysql_error(), E_USER_ERROR); } $customers = array(); while ($customer = mysql_fetch_assoc($result)) { $customers[] = $customer; } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>Test</title> </head> <body> <form method="post"> <p> Gender: <select name="gender"> <option value="">(either)</option> <option>Male</option> <option>Female</option> </select> </p> <p> Age: <select name="age"> <option value="">(any)</option> <option>18</option> <option>19</option> <option>20</option> <option>21</option> <option>22</option> <option>23</option> <option>24</option> <option>25</option> <option>26</option> <option>27</option> <option>28</option> <option>29</option> </select> </p> <p> Height: <select name="height_one"> <option value="">(any)</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select> to <select name="height_two"> <option value="">(any)</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select> </p> <p> <input type="submit" value="Search" /> </p> </form> <?php if ($searchSubmitted): ?> <?php if (count($customers) == 0): ?> <p> Sorry, no customers were found for your search. Try altering your search options. </p> <?php else: ?> <table> <?php foreach ($customers as $customer): ?> <tr> <td><?php echo htmlspecialchars($customer['first_name']); ?> <td><?php echo htmlspecialchars($customer['age']); ?> </tr> <?php endforeach; ?> </table> <?php endif; ?> <?php endif; ?> </body> </head> </html>
December 7, 201015 yr Mmmm you really should be using wildcards insted like $selectSql1 = "SELECT * FROM customer WHERE gender LIKE %".mysql_real_escape_string($_POST['gender'])."% AND area LIKE %".mysql_real_escape_string($_POST['area'])."%";
Create an account or sign in to comment