websites directory script
Last update on: 10-25-2008The goal from this lesson is not to build a websites directory but to show you how a search request works, so it will be easy and detailed, we will see later how to code a complex script.
Create the mysql table
# #structure of the directory table # CREATE TABLE directory_tbl ( id int NOT NULL auto_increment, date varchar(20) NOT NULL, url varchar (50) NOT NULL, email varchar(55) NOT NULL, description varchar(255)NOT NULL, keywords varchar (255) NOT NULL, PRIMARY KEY (id) );
So we chose the basic parameters, the website url, author's email, website description (255 Character max), website keywords, and we set the id as a primary key.
Create the form:
The html code of the form you can name it as you want: (form.html)
<table style="width:400px; margin-left:auto; margin-right:auto" >
<tr>
<td style="width:160px;height:17px"><strong>Website URL:</strong></td>
<td style="width240px; height:17px">
<input type="text" name="url" / >
</td>
</tr>
<tr>
<td style="width:160px"><strong>E-mail :</strong></td>
<td style="width:240px">
<input type="text" name="email" / >
</td>
</tr>
<tr>
<td style="width:160px"><strong>Website description:</strong></td>
<td style="width:240px">
<input type="text" name="description" maxlength="255" size="30" />
</td>
</tr>
<tr>
<td style="width:160px"><strong>Keywords</strong></td>
<td style="width:240px">
<input type="text" name="keywords" maxlength="255" size="30" />
</td>
</tr>
<tr>
<td style="width:160px"> </td>
<td style="width:240px">
<input type="submit" name="button" value="Add" />
</td>
</tr>
</table>
</form>
Now we have to create the add.php page that will add the data to the mysql table:
//----------------------------------------------------------------
//
// the adding data to mysql table page
//
// Page add.php
//
//----------------------------------------------------------------
$mysql_host = 'localhost'; // Host name
$mysql_user = 'your login'; // Your login
$mysql_password = 'password'; // Your password
$mysql_base = 'data base name'; // enter the data base name
//----------------------------------------------------------------
//Note, I will not show you again how to verify the form...
//we already seen this before, so do it by yourself.
//Connect to the data base server
$db = mysql_connect($mysql_host,$mysql_user,$mysql_password) or die ("connexion error");
//we select the mysql data base
mysql_select_db($mysql_base,$db) or die ("base connexion error");
//define the date
$date = date("m-d-Y");
// then We remove the special characters
$url = AddSlashes (htmlspecialchars($_POST['url']));
$email = AddSlashes (htmlspecialchars($_POST['email']));
$description = AddSlashes (htmlspecialchars($_POST['description']));
$keywords = AddSlashes (htmlspecialchars($_POST['keywords']));
//Insert data into the data base
mysql_query("INSERT Into directory_tbl (id,date,url,email,description,keywords) VALUES
('','$date','$url','$email','$description','$keywords') ")
or die ("request error".mysql_error());
//We close de conexion
mysql_close();
//We send the visitor to the thank you page, I will let you use your imagination to do it
header('location: thankyou.php');
Okey! we have inserted the data, removed the special characters, we almost done to be ready to use the directory. to the goal of this lesson. (searching)
The search form
The HTML code
<table style="width:400px; margin-left:auto ; margin-right:auto;">
<tr style="background:#000066">
<td colspan="2">
<div style="text-align:center; font-family:arial;font-size:12px;color:#fff;font-weight:bold">Directory Search</div>
</td>
</tr>
<tr>
<td style="width:164px"> </td>
<td style="width:236px"> </td>
</tr>
<tr>
<td style="width164px">Enter a Keyword</td>
<td style="width:236px">
<input type="text" name="keyword" / >
</td>
</tr>
<tr>
<td style="width:164px">
<input type="submit" name="Submit2" value="searchr" />
</td>
<td style="width:236px"> </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</form>
Now as we send the query from the form to the search.php page we have to code that search.php page:
//----------------------------------------------------------------
//
// The searshing page with the keywords as a search key
//
// search.php
//
//----------------------------------------------------------------
$mysql_host = 'localhost'; // Host name
$mysql_user = 'your login'; // Your login
$mysql_password = 'password'; // Your password
$mysql_base = 'data base name'; // enter the data base name
//----------------------------------------------------------------
//Again, I will not show you again how to verify the form...
//Connect to the data base server
$db = mysql_connect($mysql_host,$mysql_user,$mysql_password) or die ("connexion error");
//we select the mysql data base
mysql_select_db($mysql_base,$db) or die ("base connexion error");
//The search request
$req = mysql_query("SELECT url,description, email from directory_tbl where keywords LIKE '%$_POST[keyword]%' Order by url Asc ") or die ("request error ".mysql_error());
//We count the results
$res= mysql_numrows($req);
//We display the results found print 'For the keyword '.$keyword.' there is '.$res.' result <br />';
//----------------------------------------------------------------------
//we sort the results by alpha order (see the request order).
while( $res2 = mysql_fetch_array($req))
{
print '<strong>website Description:</strong><br / >';
print $res2['description'].'<br />';
print'<a href="'.$res2['url'].'">'.$res2['url'].'</a><br / >';
print '<hr />';
}//end of the loop
//we close the connexion
mysql_close();
I think that we done, so don't say that I forget something if you followed with me all the lessons, you will not meet any problem. And please do not copy and paste use your fingers to type and learn.
See you on the next lesson.
PHP and MySQL's lessons:
Introduction To PHPGet Started With PHP
PHP Variables
PHP Variables Of Environment
PHP Conditions
PHP Looping
PHP Cookies
PHP Working With Dates
PHP Working With Arrays
PHP Working With Files
PHP Play With Strings
PHP And Forms
Send Emails With PHP
The Include Statement
Get Started With MySQL
MySQL Update And Delete
The WHERE Clause
MySQL Functions
Guestbook Script
Websites Directory Script
Multiple Pages With PHP
Create Your Forum With Php

