Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-26-2007, 04:32 PM
Junior Member
 
Join Date: Aug 2007
Posts: 3
abuapi is on a distinguished road
Creating A Simple Search Engine In PHP (ver. 1.0.0)

Introduction:

This is the first in what I hope will be a long list of tutorials that I like to call Practical Programming in PHP. Lets get down to business, today I'm going to show you how to create a simple search engine that you can use for your very own site or some other project that you may be working on. For this tutorial I will be using a MySQL database. The bulk of the code should be compatible with PHP 3 and 4.
Background:

Before we begin, I'll show you the table I'll be using in the examples:
First_Name Middle_Name Last_Name
Dana Johnson Smith
Jill Angel Petersburg
Jack Coner Mitchel

If you feel confused about any of the functions in the tutorial, have a look at my previous tutorial that deals with common database functions.
Down To Work:

Before we actually make the search engine, we need to create a basic webpage that will have an input field where the user can enter his or her search query. I'm going to keep mine simple; feel free to make an elaborate one with lots of bells and whistles. The code for my page is below:

<html>

<head>

<title>Simple Search Engine version 1.0</title>

</head>

<body>

<center>

Enter the first, last, or middle name of the person you are looking for:

<form action="search.php" method="post">

<input type="text" name="search_query" maxlength="25" size="15"><br>

<input type="reset" name="reset" value="Reset">

<input type="text" name="submit" value="Submit">

</form>

</center>

</body>

</html>

It's a pretty basic page so I'm not going to explain alot of it. Basically, the user will enter the first, middle, or last name of the person they are looking for and hit enter. The contents of the input field will be passed to a php script named “search.php” which will handle the rest.

Now that the page is out of the way, let's create the actual script. First, we need to connect to the database using mysql_pconnect() and select the table using mysql_select_db(). Next, we want to parse the value passed to the script to see if it contains any invalid input, such as numbers and funky characters like #&*^. You should always validate input, don't rely on things like JavaScript to do it for you, because once the user disables JavaScript all that fancy validation goes down the toilet. To check the input we are going to use a regular expression, they are a bit confusing and will be explained in a later tutorial. For now, all you need to know is that it will check to see if value passed is a string of characters. All right, enough chatter, here is the first part of the script:

<?php

mysql_pconnect("host", "username", "password") or die("Can't connect!");

mysql_select_db("Names") or die("Can't select database!");



if (!eregi("[[:alpha:]]", $search_query))

{

echo "Error: you have entered an invalid query, you can only use characters!<br>";

exit;

}


Now that we've done that, we will form the search query.

$query= mysql_query("SELECT * FROM some_table WHERE First_Name= '$search_query'

OR Middle_Name= '$search_query' OR Last_Name= '$search_query' ORDER BY Last_Name");


Look confusing? I'll explain, what's happening is, we're asking MySQL to search all the rows in First_Name, Middle_Name, and Last_Name for a match to the query entered by the user; then, take the results of that search, alphabetize the results by Last_Name.

The rest of the coding from now on is a breeze. We will get the results from the query using mysql_fetch_array( ), and check to see if there is a match using mysql_num_rows(). If there is a match, or matches, we will output it along with the number of matches found; if there isn't, we'll report to the user that we couldn't find anything.

$result= mysql_num_rows($query);



if ($result == 0)

{

echo "Sorry, I couldn't find any user that matches your query ($search_query)";

exit;

}

else if ($result == 1)

{

echo "I've found <b>1</b> match!<br>";

}

else {

echo "I've found <b>$result</b> matches! <br>";

}



while ($row= mysql_fetch_array($query))

{

$first_name= $row["First_Name"];

$middle_name = $row["Middle_Name"];

$last_name = $row["Last_Name"];



echo "The first name of the user is: $first_name.<br>";

echo "The middle name of the user is: $middle_name.<br>";

echo "The last name of the user is: $last_name.<br>";

}

?>

I added that extra if statement so that when we report how many users we've found, its output will be in proper English. If I we don't, the script will echo "I've found 1 matches" which obviously isn't good grammar :P The rest of the script loops through the results and prints them to a webpage. That's all, we've finished the script! The entire script is included below:

<html>

<head>

<title>Simple Search Engine version 1.0 - Results </title>

</head>

<body>

<?php

mysql_pconnect("host", "username", "password") or die("Can't connect!");

mysql_select_db("Names") or die("Can't select database!");



if (!eregi("[[:alpha:]]", $search_query))

{

echo "Error: you have entered an invalid query, you can only use characters!<br>";

exit; //No need to execute the rest of the script.

}



$query= mysql_query("SELECT * FROM some_table WHERE First_Name='$search_query'

OR Middle_Name= '$search_query' OR Last_Name='$search_query' ORDER BY Last_Name");



$result= mysql_numrows($query);



if ($result == 0)

{

echo "Sorry, I couldn't find any user that matches your query ($search_query)";

exit; //No results found, why bother executing the rest of the script?

}

else if ($result == 1)

{

echo "I've found <b>1</b> match!<br>";

}

else {

echo "I've found <b>$result</b> matches!<br>";

}



while ($row= mysql_fetch_array($query))

{

$first_name= $row["First_Name"];

$middle_name = $row["Middle_Name"];

$last_name = $row["Last_Name"];



echo "The first name of the user is: $first_name.<br>";

echo "The middle name of the user is: $middle_name.<br>";

echo "The last name of the user is: $last_name. <br>";

}

?>

</body>

</html>

Reply With Quote
  #2 (permalink)  
Old 09-04-2007, 04:40 PM
mmc mmc is offline
Member
 
Join Date: Mar 2007
Posts: 34
mmc is on a distinguished road
nice work...will try out
Reply With Quote
  #3 (permalink)  
Old 11-05-2007, 12:28 PM
Member
 
Join Date: Sep 2007
Posts: 50
pakistanboy is on a distinguished road
I do not have an idea about this.

Thank you,
Regards,
Pakistan Boy,
The Best Link Directory | 1 Pakistan Gifts
Reply With Quote
  #4 (permalink)  
Old 11-06-2007, 11:30 AM
Junior Member
 
Join Date: Sep 2007
Posts: 8
giggsmash is on a distinguished road
nice...

Nice one for the search page... simple, understandable code, perfect....
well done!!!, It help me lot.
Reply With Quote
  #5 (permalink)  
Old 06-19-2008, 12:58 AM
Junior Member
 
Join Date: Jan 2008
Posts: 1
triXtyle is on a distinguished road
This is useful if you know what you are searching for. But if I don`t know what was the name, Mitchel, or Mitchal (sounds weird, but for example ), what should I do?
I can suggest you to change the mysql query to this:

PHP Code:
$querymysql_query("SELECT * FROM some_table WHERE First_Name='%$search_query%' OR Middle_Name= '%$search_query%' OR Last_Name='%$search_query%' ORDER BY Last_Name"); 
so now you can search with parts of the name only

anyway, cheers and good tutorial indeed

Last edited by triXtyle; 06-19-2008 at 01:00 AM.
Reply With Quote
  #6 (permalink)  
Old 07-10-2008, 11:38 AM
Junior Member
 
Join Date: Jul 2008
Posts: 6
Azim is on a distinguished road
Nice tutorials, seems good enough.
Does anyone know how to make a search engine for a website in html.
Like to search the site for anything.

Thanks
Reply With Quote
  #7 (permalink)  
Old 07-17-2008, 05:17 AM
sdavis891's Avatar
Junior Member
 
Join Date: Jul 2008
Location: Canada
Posts: 28
sdavis891 is on a distinguished road
(Azim)

HTML sites are only ment for text. Personal Portfolios and simple company sites use HTML. Places like myspace.com and google.com use php/mysql. But I do believe a php code can work on a html site. If i'm wrong someone please correct me.

(TUTORIAL)

It looks simple enough, from what I see. I don't know much about PHP/MYSQL but I want to know if it is possible to put it on a side like a google search bar. And make it open up in a new window to a forum search?
Reply With Quote
  #8 (permalink)  
Old 07-18-2008, 12:22 AM
VIP Member
 
Join Date: Mar 2008
Location: United Kingdom
Posts: 163
Robbb is on a distinguished road
Send a message via MSN to Robbb Send a message via Skype™ to Robbb
sdavis891, that is very possible
For it opening in a new window, that is html coding, changing the target from blank to parent (i think thats the right ones, i always get them mixed up )
I dont understand your question about putting it on a side. Are you talking about the positioning of the search bar?
Reply With Quote
  #9 (permalink)  
Old 07-18-2008, 01:54 AM
sdavis891's Avatar
Junior Member
 
Join Date: Jul 2008
Location: Canada
Posts: 28
sdavis891 is on a distinguished road
yes. Having a search bar located on the top of a website. When someone enters there information and presses go or enter... It opens a new browser IE/FF3 or what ever... It comes up to a page with there information submitted and showing a list of results.

But what i want to know if its possible to plug it into a forum search.. What I was saying earlier was that if I made one and I clicked go. It would open up my forum search with my text displaied with results.

This is going from a Website -> Forum
Reply With Quote
  #10 (permalink)  
Old 07-18-2008, 10:40 PM
Junior Member
 
Join Date: Jul 2008
Posts: 6
Azim is on a distinguished road
Thanks for help guys,
And u r right i was talking about making a search engine in HTML site using php&Sql. it is possible, but i dodnt have tutorials. Thanks
And about ur searches maybe u r using the forum search engine which takes you there.
Did you direct the queries to the forum's database?
This is what could possibly be wrong.

Well Thanks again guys.

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 02:02 AM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com