![]()
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
PrincipleThe 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 scriptThe 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 messageWe 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):
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:
The date was previously calculated:
How to display messagesThere 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:
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:
Just before, we did tests to guess the page to display: At the beginning of the script:
And after having retrieved the number of messages (
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:
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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Copyright © 2000-2002 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||