PHP and Mysql search engine

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theclay7
Forum Commoner
Posts: 50
Joined: Wed Feb 19, 2003 3:17 am

PHP and Mysql search engine

Post by theclay7 »

i am going to write a search engine for my website in PHP and Mysql...anyone know a good start for me to delve into? The search will be like the one in this forum...
hurkle
Forum Newbie
Posts: 15
Joined: Tue Feb 11, 2003 1:21 pm
Location: Minnesota, USA

Post by hurkle »

For a search engine like you described, there's a few things you need. First of all, you need to figure out what's going to be searchable, i.e. keywords or descriptions of pages/content in your site, or the full text of pages in your site. I would suggest the latter. As a starting point, I would create a table call tbl_content, with fields like.. fld_content_id, fld_content_text, fld_content_url. Then, figure out some way to get the content from your pages into your table. It should be possible to write a script that points at your web folder, opens each file , grabs the file name and the contents, and inserts the contents into the fld_content_text field. Once you have that, you need an html form to enter search criteria. Instructions, a TEXT input, and a submit button. Then you need a script to process the search. Grab the contents of the text input, (I'd name it something like txt_search), check if something was entered, if not send them back to that form. If they entered something, split the string entered into multiple values seperated by spaces, to grab each word individually. Once you've got your search terms in an array, step through that array and build a mySql query that looks something like this..

$str_sql = "SELECT fld_content_url FROM tbl_content WHERE ";
$str_sql .= "(fld_content_text LIKE '%" . $arr_terms['1'] . "%' AND fld_content_text LIKE '%" . $arr_terms['2'] . "%' ) ";

that's just a rough idea, not literal code or the actual query, but you should get the idea. Once you've got results, if any, step thru them and for each row, draw a hyperlink whose target address is the fld_content_url. If you want to get fancy, use php code get the text of each page into your result set as well, and count the number of occurrences of your search terms to provide a score, then display in highest scoring order.

That's an outline of one approach, I hope that helps. If you have other content other than pages, that change, i.e. bulletin boards, you'd have to find a way to get the content of new posts stored in your searchable table. Let us know what you come up with.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Post Reply