Where do i start off for a php search script? [duplicate]
Possible Duplicate:
How do i get my search bar to actually work?
Hi guys, I've got a search bar that looks great but i now need to get started on the php for the search results and so that it searches the whole site... i'm not very good at php so could you help me to get started on the simple building blocks of the php script? Thanks in advance... Oh, and here's my html script for the search bar if it helps in any way...
<form class="search2" method="get" action="default.html" />
<input class="search2" type="text" name="serach_bar" size="31" maxlength="255"
value="" style="left: 396px; top: 153px; width: 293px; height: 26px;" />
<input class="search1" type="submit" name="submition" value="Search" style=" padding-
botto开发者_StackOverflowm:20px; left: 691px; top: 153px; height: 23px" />
<input class="search2" type="hidden" name="sitesearch" value="default.html" />
If are you using php, probably your database is mysql, so you can use mysql full text search.
You can find great informations about it here: http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html
And an exemple how to use here:
http://devzone.zend.com/article/1304
First off, you will need a database to hold your data. MySQL is a very popular one (which most hosts provide by default).
A sample SQL database structure would look like this
MyTable
Id
Title
Content
From there, you need to use PHP to connect to the database and run a query which will execute and return results
<?php
// This is how you connect to MySQL using PHP
mysql_connect("localhost","user","pass");
mysql_select_db("your_db_name");
// Collect your input from your form using $_GET, but make sure you clean it first
$keyword = $_GET['keyword'];
// Note that % is wildcard in a MySQL query
$q = mysql_query("SELECT * FROM MyTable WHERE Content LIKE '%".$keyword."%' OR Title LIKE '%".$keyword."%'");
// Then iterate through your results and print them out
mysql_close();
?>
For more PHP/MySQL stuff this is a great reference (in fact, many PHP problems can easily be solved using that reference).
Regards,
Dennis M.
If the sites contents are located in a database, look into using LIKE in MySQL.
If the contents hard coded, I'd recommend using google's site search since you yourself said you aren't very familiar with PHP.
Theres also an in-between option to Matts suggestion... I've recently been looking at the Bing search API, its pretty much a free version of what Google offer (well I'm pretty sure its free).
You can query it and display results how you like, but its using the Bing search database, which means it'd be pretty good.
Another thing that you can do if you want to search pages on your site that may or may not have content contained in MySQL database tables (for example, if some of your content is hard-coded into your site statically) is to actually run a cron job that indexes your site regularly and stuffs all of the content into a database table, on which you can then do a full-text search. In most cases it's not necessary, but certainly an option in some situations.
You would need to do some work with a regular expression in your script or have a custom function (which would be faster) that would avoid retrieving HTML tags that may or may not match a search query, though.
精彩评论