The manual Page
Version française
   
index | glossary | news | downloads | links ]
  PHP home page
pages
guest book
news
print button
 
news
glossary
links
downloads
 
credits
contact
 
 
search
 
last update
19/02/2003
Valid HTML 4.0!
Valid CSS!
Hit-Parade
Mesurez votre audience


  TmP's guest book

Principle

The guest book is not very complicated. It is mainly a matter of accessing a MySQL database.

The SQL table associated to the guest book is called guest_book. It has 5 columns: Date, Name, Mail, Message and ID. ID is autoincremented, it is therefore useless to put it in the insert query.

The interest of this script is that it is "self-refered": the form we use to add a message calls the page itself. This avoids using an intermediate page to display a thanks message. Moreover, the person who has written a new message can see it has been immediately added.

The only possible difficulty of this script is the way we display messages on several pages. A variable (page) passed as an argument in the URL of the page is used to guess the number of the page we want to see. This variable is set to 0 for the first page, 1 for the second page... If no page argument is passed in the URL or if its value is less than 0, we set page to 0, and if we ask for a page that does not exist yet, we display the last possible page. See how to display messages.

Implementing the script

The script can be splitted into 2 parts: a first part that treats a possible message to add, and a second part that deals with the messages to display. However, for both parts, we are going to use the same connection to the MySQL server.

New message

We use a form with the POST method. The advantage of of this is that we can add long messages and the URL bar in Web agent remains clear. There is also another advantage: the PHP script will get directly variables initialized with the data provided in the form.

The first thing to do consists of checking if all data we need to add a new message are present (at least a message and a name):

// Is there a message to add?
if ($message != "") {
	$message = htmlentities($message);
// Is there a name?
	if ($nom != "") {
		...
	}
}

htmlentities is used to transform the possible HTML code of the message in a special format so that it will not be interpreted as HTML code when it is displaid. If something is missing, we show a message. If not, we add the message in the table:

// Query to add the message
$query  = "INSERT INTO guest_book (Date, Name, Mail, Message)";
$query .= " VALUES ($date, \"$name\", \"$mail\", \"$message\")";
mysql_query($query) or die ("cannot insert the message");

The date was previously calculated:

$date = date("YmdHis", time());

How to display messages

There is nothing special. We query the database to retrieve messages, and we display them. To avoid long pages, we only display 10 messages at a time. The only problem is how to put all messages on several pages. To know the page we want to display, we use a variable (page) passed as an argument in the URL.

The first thing to do is therefore to retrieve this variable. This is easy to do with the function parse_str used to create the same variables as those passed in the URL:

parse_str(getenv("QUERY_STRING"));

We then have a variable $page. It is now just a matter of retrieving the right messages. We retrieve the complete table, but with the function mysql_data_seek, we access only the messages we are looking for:

for($i = $page*10; ($i < $nbr) && ($i < $page*10+10); $i++) {
	if (! mysql_data_seek($result, $i)) {
		continue;
	}
	if (! $row = mysql_fetch_row($result)) {
		continue;
	}
// We display messages
	list($year, $month, $day) = split("-", substr($row[0], 0, 10));
	$name = $row[1];
	$mail = $row[2];
	$message = $row[3];
	echo "<TABLE align=\"...
	...
}

Just before, we did tests to guess the page to display:

At the beginning of the script:

if (($page == "") || ($page < 0)) {
// On numerote les pages a partir de 0
	$page = 0;
}

And after having retrieved the number of messages ($nbr=mysql_num_rows($result);):

$nbr = mysql_num_rows($result);
// If we ask for a page that does not exists yet,
// we return the last page
if ($page*10+10 > $nbr) {
	$page = ($nbr-$nbr%10)/10;
}

Last point: if the message has not been saved because an element is missing, we put back the data in the form with another PHP script:

<SPAN style="font-family: Arial; font-weight: normal;">name:</SPAN><BR>
<?
echo "<INPUT name=\"name\" maxlength=\"64\" type=\"text\" size=\"20\"";
echo " style=\"font-size: 10px\;\" value=\"$nameform\">\n";
?>

And so on with $mail and $message. These 3 variables ($name, $mail and $message) are always set every time the form has been submitted, and reinitialized if the message has been added.

At the very end of the page, we add links to the previous and next pages, according to the page we are displaying.

Useful functions

name description
htmlentities transforms special characters of a string into HTML entities
parse_str retrieves and creates all variables passed in an URL
split splits a string into an array according to a separator string
date formats a date

download the complete code.

printable format printable format



Copyright © 2000-2002 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences.