Posted on November 23rd, 2007
57980 views
[MY_P2L_AD]
Hey Guys, This is my second php tutorial written in P2L.com. Ok so this time the tutorial is about How to make your Own Search Engine. Now, I should prelude this by saying that there are about two trillion ways of designing and implementing a search engine, but this is a quick easy one (in fact, you can basically copy/paste the source code if you don't feel like reading the tutorial). Okay, So this our fist Code with what we should begin just after <?php ....
This PHP code snippet should go in above the <head> of the document. The first line retrieves the "q" ("q" stands for query, in case you were wondering) from the search. The second line strips any white spaces. Next we're going to need to connect to the database... $user = "username"; // AN EDIT IS REQUIRED HERE The above variables need to be set (obviously) to whatever is applicable to your host/database. Okay, now we're going to talk about the database structure a little bit. Basically, what I've done is added a field to the dbase called "keywords". Then, I entered comma-separated values that I wanted to associate with that particular letter. Take a look at the code: $field_to_search = "field"; So, these four lines do this: 1) Set the field to search as a variable called $field_to_search 2) Query the database for the searched term 3) Return the results 4) Count the results (for aesthetic purposes later) Up to this point, all of this PHP code should be in or above the <head> section. To be 100% honest, it doesn't really matter if it is or isn't in the <head> section, but I like to keep my "backend" PHP up there because it's "out of the way" (so to speak). This first code snippet is NOT PHP. It shouldn't be put in the <? ?> tags. <form name="search" method="GET" action="<?=$PHP_SELF?>"> The above code snippet is the HTML form for querying the database. If you want, you can set up a snippet of code to hide the form when someone has already done a search, because the results will be displayed on the same page. You could do that with this code: if($q == true) Okay, that's just a side note... on to displaying the results! This is a lot of code, but it's pretty self-explanatory... Just follow the comments in the code, and you should have a good idea what's going on. if ($trimmed == "") Okay, I've chosen to interrupt the code here to explain the following while() loop. If you've worked with PHP/mySQL for any length of time at all, you've probably set up a loop just like this one, but I'll assume that you haven't. What you're going to want, so to set a variable, then put an = sign, then an $r[""];. in the quotes ("), you're going to add the name of the field that you want to associate with that variable. I'll show you some examples, but please now that you're going to need to change this to fit your database. while ($r= mysql_fetch_array($result)) So now you have your while() loop set up. The last line there is $count++;. Normally, you would end a while loop with the closing bracket (}), but we are going to "break out" of the while() loop here to format our results. By breaking out of the while loop to enter some good ol' HTML, you'll result the HTML every time the while loop passes, so, in other words, for every result of your search! Pretty clever, huh? Using the above example database, I'll show you what you could do... Keep in mind, this needs to be OUTSIDE of the <? ?> tags... rather, between them. <a href="http://www.YOURDOMAIN.com/archive/<? echo $year ?>/<? echo $id ?>.html"><? echo $title ?></a><br /><br /> Okay, so if your search returned 12 results, all your html would be written out for you with the link, the title, the description, and letting you know which result number you're seeing! Now, a very important piece of code: <? } ?> You have to close your while loop!!!! Many many frustrating hours I have spent debugging source code because I forgot those eight little characters! Ok so see next time in my furthur tutorials |