PHP 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
tip850
Forum Newbie
Posts: 2
Joined: Mon Jul 14, 2008 5:09 pm

PHP search engine

Post by tip850 »

Hi, this is my first thread and I am new to the whole web design business.

How could I make a pure PHP search engine to search the site that I am making? I don't want to use mysql just PHP, HTML and if I have to text files. I have tried to record information in text documents but I couldn't find a way to get the information.

I searched through php.net and all I found was a few string function that couldn't search the whole file, once it found the word I was looking for it stopped searching.

The first thing I need to know is how should I record the information without using mysql?

I have made the form that will gather the information I need and it sends it all to my email and the file it uploads gets sent to a folder named "assignments". There are nine fields, eight of them return text and the other file upload field returns what ever file is uploaded; there are no restrictions on the uploaded file and even if it is a text document I don't want the search engine to search it.

The second thing I need to know is how to search for the information?

I don't care if there needs to be a seperate file to store the information for each time something new is submitted. I have created a table using html and php which will be where the results go. Each part of the table echos a varible. If I can't put the code on the same page as the table which visitors will see then I also need to know how to get varibles from other files.

I want the most relivant information at the top of the table and to work out the relivance of the information I want the search engine to figure out what percentage of the document contains the words that the user searched for.

The last thing I need to know is what do I do when the search engine returns more results then I can fit on 1 page (there are 10 results per page)?

Thanks in advance to any one who answers my questions. This has been driving me nuts for the last week. I was tring to get the website live before the 20th of June but I didn't expect the search engine to be such a road block. It's the only thing left that I havn't finished. I have tried downloading search engines already made but they are not personilised enough, some of them contain foreign adverising and I can't edit them at all.

Thanks,

Tim
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: PHP search engine

Post by jaoudestudios »

For a simple search engine (php class) that is already built and orders the results by relevance...here is one without any advertisement etc...
http://www.forum.jaoudestudios.com/view ... ?f=13&t=12
However, it does use mysql and a database.

I know what you are thinking, I dont want to use mysql as it is another language to learn. It is not that difficult, plus this is what it is designed for, storing data, searching, ordering etc...

If you do it with text files only, it will be very slow and memory intensive, as you will have to open each text file, read the contents into a string or array, search it, order it and do a lot of messing about in between.

My advice, its worth using mysql. There are quite a few CMS systems out there with a lot of what you want built in. Have a look at Joomla...
http://www.joomla.org/
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: PHP search engine

Post by alex.barylski »

You would use an existing solution such as phpDig:

http://www.phpdig.net/

Alternatively you could use Zend_Search_Lucerne:

http://framework.zend.com/manual/en/zen ... ucene.html

Much easier than implementing anything yourself.
tip850
Forum Newbie
Posts: 2
Joined: Mon Jul 14, 2008 5:09 pm

Re: PHP search engine

Post by tip850 »

I took a lot of peoples advice and decided to try to learn mysql and so far I managed to put information into the database and retrieve it but I am stuck doing something with the information.

Here is the part where I send the information to the database

Code: Select all

mysql_connect("localhost","web177-tip850","peoples850");
mysql_select_db("web177-tip850");
mysql_query("INSERT INTO assignments(assignment_name, assignment_description, school, year, catagory, document, mark, year_marked, credits)VALUES('$name', '$description', '$school', '$year', '$catagory', '$file', '$mark', '$yearmarked', '$credits')") or die(mysql_error());
mysql_close();
That part of the code works.
Do I have to close the mysql connection or does it close itself?

This bit is the part where I try to retrieve the information and do something with it. For some reason the mysql_num_rows part doesn't work.

Code: Select all

$search_box = $_REQUEST['search_box'];
mysql_connect("localhost", "web177-tip850", "peoples850") or die(mysql_error());
mysql_select_db("web177-tip850");
$search = "SELECT*FROM assignments WHERE (assignment_name, assignment_description) LIKE $search_box ORDER BY assignment_name";
$query = mysql_query($search);
$no_rows = mysql_num_rows($query);
Why doesnt the mysql_num_rows work?

All I have to do now is put the results into a table but there will be more then 1 page of results with ten results per page. How do I make more pages but have the table on the second page continue on from the first and the third continue on from the second and so on? I used the mysql_num_rows to determine how many pages the script will need to make.

I think I will need to use mysql_result to put the information into the table but to use that I need to know what row of information I need. How do I figure out what row I need?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP search engine

Post by Benjamin »

It's not returning any rows because mysql_query did not return a result resource because the query has a syntax error. Run the query in phpMyAdmin to test it or use the mysql_error function to retrieve the last error.

Also, apply mysql_real_escape_string liberally and call me in the morning.
Post Reply