Hello!
In this tutorial we're going to make our own guestbook using PHP!
Table of contents
1. What should I know already?
To use this tutorial you should already have a small knowledges of:
- CSS
- HTML
- PHP
-- loops
-- variables
-- GET && POST methods
-- operators
- MYSQL
-- queries
-- functions
2. What do I know after this tutorial?
After this tutorial you'll know how to create your own guestbook, use mysql queries, combine html, css and php and how to use the GET methods and POST methods in different ways
Oke, lets start.
Good luck!
3. The database
Lets start with creating our database, go to your PHPMYADMIN and create a new database.
Call it: Guestbook.
Reduced: 47% of original size [ 1077 x 522 ] - Click to view full image
Now select the database in the left panel.
Create a new table, 5 fields, call it: messages by filling in and clicking on the 'Go' button.
Reduced: 47% of original size [ 1079 x 278 ] - Click to view full image
Create these 5 fields:
id (250 INT) = PRIMAIRE KEY && action = auto_increment
author (50 VARCHAR)
email (150 VARCHAR)
date (50 VARCHAR)
message (LONGTEXT)
Storrage engine MyIsam.
Reduced: 48% of original size [ 1055 x 616 ] - Click to view full image
Click on "Save",
And we're done with our database
ALERT: Be sure you set the id to INT and auto_increment primairy key.
If you had any problems you can always go to IMPORT and set this SQL into a .sql file and import it (or just goto SQL and copy-past it).
4. Script to let people add a message
Now we're going to make a script which lets the users add a message to the guestbook.
This message will be inserted into the database once it checked if everything is allright with what the user filled in.
Let's start with a form we'll make.
The user haves to fill in : name, email and message.
So our form will look something like this:
file: guestbook.php
method='POST' - this tells the application that we'll use the method POST, so all values of the fields will be set into $_POST['field_name'] once the form has been submitted
action='<?php echo $_SERVER['PHP_SELF'] - after the form has been submited it will go to the currently file the form is running in
And we add a submit button which will submit our form.
But now we have to let it check if the submit button(we called: 'submit_guestbook') is pressed, if so: then it should check if everything is filled; INSERT a new row(the message) into the database with the correct filled in info, otherwise it will returns an error.
We'll check the isset() function to check if the button does exist and is pressed.
The issed function works as follow in a loop:
it checks if the variable between ( and ) does exist, if so, then it will run the script between { and }.
In our case will the variable we'll check, be: $_POST['submit_guestbook'] (remember, once the form has submitted, all input fields values will be set into $_POST['field_name'], the field name of the submit button is 'submit_guestbook' so it will be $_POST['submit_guestbook'] once the form have been submitted, so if that does exist; we know: the form has submitted).
It will show the form , untill the form has been submitted by clicking on the 'submit_guestbook' submit button.
Now we have to check if the user filled in everything. We'll use the empty() function to check it.
This function you can use as follow in a loop:
We'll use !empty() to check if the variable between ( and ) is NOT empty. We'll check all input fields bij cehcking if the value filled in in the input field($_POST['field_name_here']) are filled in(given a value).
Our fields we called: author, email, message
So once the form have been submitted the values the user filled in those fields will come into:
$_POST['author'], $_POST['email'], $_POST['message'] cause we use the POST method for the submitting method of the form.
So these POST vars we'll be checking.
Oke, if everything is filled in, we'll INSERT the message into the database(add the message to the guestbook), we'll use the mysql_query() function for this. It works as follow:
In our case the tablename = messages and our fields in that table are author, message and email and the values the user filled for those fields are set in $_POST['author'], $_POST['message'], $_POST['email'] (ALERT: The fieldnames from the form I just called the same as those from the database table, but it's not a must, if you called them else it will just be $_POST['fieldname_you_called_it'], you can call them whatever you want, just like you hold them in mind).
So our query will be:
If you having problems with all those ' and "s, you should take a look at k4mui's tutorial.
Let's put it together with some finishing thoughs(I'll add a little table to the form)!
And we're done! Now we have our own form wich lets the user add a message to the guestbook (database).
Now we only have to SHOW all messages and get the info from the database.
Let's do it!
5. Overview of all messages in the guestbook
For this we'll use the SELECT query, to get all info from the database.
It works like:
Our fields we need to get the values of: date, author, email, message
In our case there's no clause, we just select all messages from the database(guestbook).
We'll order them by ID (so a higher ID = later added), in our case the ordertype will be DESC (= descending).
So it will first select the fields with the highest id(caus it would order by id(ORDER BY id)).
We'll first don't use LIMIT, so it will just select ALL rows it find with the query and select them descending, ordering by id(starting with the highest id one(so the one which is added as last) and then go select descending).
We'll use * to define it haves to select ALL fields, you can also set instead: date, author, email, message caus the field id we won't actually need.
Now we have to set the values of the fields it selected with this query, into a array. We'll use mysql_fetch_assoc(query).
it will check: as long that $row can contain a new row which have been selected with the query in $messages, then do the follow.
All fields it select with the query, will be set(in our case) into a array for the variable $row.
$row[fieldname] will contain the value it selected from fieldname.
Take for example: there are 2 messages in our database 'messages' table, on with as value of the 'message' field: "Test message" and the other: "Hello!".
Now, if we would use our query and loop, and echo $row['message'] it will return: Testmessage Hello!
Caus it will find 2 rows, first it will do the loop for the first row, there he find one with as message "Test message" , and it defines that as value for $row['message'], and then it echo's it, and the same for the other row.
Oke, let's make our guestbook messages overview!
it will select all rows it finds in the table 'messages' and each time it finds a new row, it will run the while loop, set all values of the fields into $message['fieldname'] and then run that script (it will make a new table, show the message in a new table and the author of the message, etc.).
Now lets spread the messages in the guestbook about more pages than only 1, cause else, if there have been added a lot of rows(messages) to the database, it would show them all.
We'll use the LIMIT thing for this.
now take for example, $limit is equal to : 0,5
this means it will select (descending ordered by the field 'id') the first 5 rows, starting at the row with the highest id - then descending the first 5 fields it finds.
If $limit would be equal to 5,5; it would select the first 5 rows, starting at the row with the highest id - 5,descending, so if that would be 20, it would select the fields with the id's 15-10 (10-15).
This will check if the $_GET['page'] does exist(if the var page is given a value to in the url), if so:
it will set $page equal to it (for example, $_GET['page'] is equal to 2) then it will generate the last limit by doing this (2) * $guestbook_limit (5 in our case, so we'll show 5 rows (messages) per page), so if $page = 2, it will be 5*2 = 10, then the $firstlimit = $lastlimit-5 = 5, so the finaly $limit will be:
It's quite hard to explain, so I hope you'd understand, anyways; this script will just generate the messages it should select descending by looking at the page given in th url(http://file.php?page=amount).
Now we have to show the correct links to the different limit 'pages'.
You can just do it by hand:
or just make a loop for it.
it will first get the amount of rows(messages) by the query in $amount_messages, we use mysql_num_rows() which will count the amount of results of the query which selects all rows from the table 'messages'. Then it will set $i equal to 0, and as long it's smaller than the id of the last row(message), it will increase it by 5 (so the page will jump up by 1, caus each page contains 5 rows/messages), then it will generate the page and show a link to it.
I can't explain it but will just give the code with a bit description, for those who'd not like to add the links by theirself but letting the system do it by a script.
And we put it together.
6. The whole guestbook
Oke, let's put everything together for our whole guestbook!
I'll add some comments to, so you know how to put all together in what order, so you understand the structure.
AND WE'RE DONE!
Good luck!
I hope you'd understand the most
If you have any questions, feel free to ask me.
Skyfe.
In this tutorial we're going to make our own guestbook using PHP!
Table of contents
QUOTE
1. What should I know already?
2. What do I know after this tutorial?
3. The database
4. Adding messages to the database
5. Overview of all messages in the guestbook
6. The whole guestbook
2. What do I know after this tutorial?
3. The database
4. Adding messages to the database
5. Overview of all messages in the guestbook
6. The whole guestbook
1. What should I know already?
To use this tutorial you should already have a small knowledges of:
- CSS
- HTML
- PHP
-- loops
-- variables
-- GET && POST methods
-- operators
- MYSQL
-- queries
-- functions
2. What do I know after this tutorial?
After this tutorial you'll know how to create your own guestbook, use mysql queries, combine html, css and php and how to use the GET methods and POST methods in different ways
Oke, lets start.
Good luck!
3. The database
Lets start with creating our database, go to your PHPMYADMIN and create a new database.
Call it: Guestbook.
Reduced: 47% of original size [ 1077 x 522 ] - Click to view full image
Now select the database in the left panel.
Create a new table, 5 fields, call it: messages by filling in and clicking on the 'Go' button.
Reduced: 47% of original size [ 1079 x 278 ] - Click to view full image
Create these 5 fields:
id (250 INT) = PRIMAIRE KEY && action = auto_increment
author (50 VARCHAR)
email (150 VARCHAR)
date (50 VARCHAR)
message (LONGTEXT)
Storrage engine MyIsam.
Reduced: 48% of original size [ 1055 x 616 ] - Click to view full image
Click on "Save",
And we're done with our database
ALERT: Be sure you set the id to INT and auto_increment primairy key.
If you had any problems you can always go to IMPORT and set this SQL into a .sql file and import it (or just goto SQL and copy-past it).
CODE
CREATE TABLE `messages` (
`id` int(250) NOT NULL auto_increment,
`author` varchar(50) NOT NULL,
`email` varchar(150) NOT NULL,
`date` varchar(50) NOT NULL,
`message` longtext NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
`id` int(250) NOT NULL auto_increment,
`author` varchar(50) NOT NULL,
`email` varchar(150) NOT NULL,
`date` varchar(50) NOT NULL,
`message` longtext NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
4. Script to let people add a message
Now we're going to make a script which lets the users add a message to the guestbook.
This message will be inserted into the database once it checked if everything is allright with what the user filled in.
Let's start with a form we'll make.
The user haves to fill in : name, email and message.
So our form will look something like this:
file: guestbook.php
CODE
<form method='POST' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<b>username:</b> <input type='text' name='author'><br>
<b>email:</b> <input type='text' name='email'><br>
<textarea cols='60' rows='5' name='message'></textarea>
<input type='submit' name='submit_guestbook' value='submit message'>
</form>
<b>username:</b> <input type='text' name='author'><br>
<b>email:</b> <input type='text' name='email'><br>
<textarea cols='60' rows='5' name='message'></textarea>
<input type='submit' name='submit_guestbook' value='submit message'>
</form>
method='POST' - this tells the application that we'll use the method POST, so all values of the fields will be set into $_POST['field_name'] once the form has been submitted
action='<?php echo $_SERVER['PHP_SELF'] - after the form has been submited it will go to the currently file the form is running in
And we add a submit button which will submit our form.
But now we have to let it check if the submit button(we called: 'submit_guestbook') is pressed, if so: then it should check if everything is filled; INSERT a new row(the message) into the database with the correct filled in info, otherwise it will returns an error.
We'll check the isset() function to check if the button does exist and is pressed.
The issed function works as follow in a loop:
CODE
if(isset(variable))
{
...
}
{
...
}
it checks if the variable between ( and ) does exist, if so, then it will run the script between { and }.
In our case will the variable we'll check, be: $_POST['submit_guestbook'] (remember, once the form has submitted, all input fields values will be set into $_POST['field_name'], the field name of the submit button is 'submit_guestbook' so it will be $_POST['submit_guestbook'] once the form have been submitted, so if that does exist; we know: the form has submitted).
CODE
if(isset($_POST['submit_guestbook'])) {
}else{
//show the form
}
}else{
//show the form
}
It will show the form , untill the form has been submitted by clicking on the 'submit_guestbook' submit button.
Now we have to check if the user filled in everything. We'll use the empty() function to check it.
This function you can use as follow in a loop:
CODE
if(empty(variable)) {
//in this case the variable between ( and ) is empty
}
//in this case the variable between ( and ) is empty
}
We'll use !empty() to check if the variable between ( and ) is NOT empty. We'll check all input fields bij cehcking if the value filled in in the input field($_POST['field_name_here']) are filled in(given a value).
Our fields we called: author, email, message
So once the form have been submitted the values the user filled in those fields will come into:
$_POST['author'], $_POST['email'], $_POST['message'] cause we use the POST method for the submitting method of the form.
So these POST vars we'll be checking.
CODE
if(!empty($_POST['author']) && !empty($_POST['message']) && !empty($_POST['email'])) {
//this will be the case if all fields are filled in(author, message, email)
}else{
//this will be the case if the user did NOT fill in all fields(author, message, email)
}
//this will be the case if all fields are filled in(author, message, email)
}else{
//this will be the case if the user did NOT fill in all fields(author, message, email)
}
Oke, if everything is filled in, we'll INSERT the message into the database(add the message to the guestbook), we'll use the mysql_query() function for this. It works as follow:
CODE
$anyvar = mysql_query("INSERT INTO tablename(field1, field2, field3)VALUES(value_for_field1, value_for_field2, value_for_field3) ")or die(error);
In our case the tablename = messages and our fields in that table are author, message and email and the values the user filled for those fields are set in $_POST['author'], $_POST['message'], $_POST['email'] (ALERT: The fieldnames from the form I just called the same as those from the database table, but it's not a must, if you called them else it will just be $_POST['fieldname_you_called_it'], you can call them whatever you want, just like you hold them in mind).
So our query will be:
CODE
$add_message = mysql_query("INSERT INTO messages (author, message, email) VALUES ('".$_POST['author']."', '".$_POST['message']."', '".$_POST['email']."' ") ") or die ("<font color='red'>Couldn't add your message! \n Please try again.</font>");
If you having problems with all those ' and "s, you should take a look at k4mui's tutorial.
Let's put it together with some finishing thoughs(I'll add a little table to the form)!
CODE
<?php
if(isset($_POST['submit_guestbook'])) {
if(!empty($_POST['author']) && !empty($_POST['message']) && !empty($_POST['email'])) {
//this will be the case if all fields are filled in(author, message, email), so we can add the message to the database
$add_message = mysql_query("INSERT INTO messages (author, message, email) VALUES ('".$_POST['author']."', '".$_POST['message']."', '".$_POST['email']."' ") ") or die ("<font color='red'>Couldn't add your message! \n Please try again.</font>");
}else{
//this will be the case if the user did NOT fill in all fields(author, message, email)
echo "<font color='red'>Please fill in all fields!</font>";
}
}else{
//show the form
?>
<form method='POST' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<table style='border:1px solid #BABABA'>
<tr>
<td> <b>username:</b> </td>
<td> <input type='text' name='author'> </td>
</tr>
<tr>
<td> <b>email:</b> </td>
<td> <input type='text' name='email'> </td>
</tr>
<tr>
<td colspan='2' align='center'> <textarea cols='60' rows='5' name='message'></textarea> </td>
</tr>
<tr>
<td colspan='2' align='center'> <input type='submit' name='submit_guestbook' value='submit message'> </td>
</tr>
</table>
</form>
<?php
}
?>
if(isset($_POST['submit_guestbook'])) {
if(!empty($_POST['author']) && !empty($_POST['message']) && !empty($_POST['email'])) {
//this will be the case if all fields are filled in(author, message, email), so we can add the message to the database
$add_message = mysql_query("INSERT INTO messages (author, message, email) VALUES ('".$_POST['author']."', '".$_POST['message']."', '".$_POST['email']."' ") ") or die ("<font color='red'>Couldn't add your message! \n Please try again.</font>");
}else{
//this will be the case if the user did NOT fill in all fields(author, message, email)
echo "<font color='red'>Please fill in all fields!</font>";
}
}else{
//show the form
?>
<form method='POST' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<table style='border:1px solid #BABABA'>
<tr>
<td> <b>username:</b> </td>
<td> <input type='text' name='author'> </td>
</tr>
<tr>
<td> <b>email:</b> </td>
<td> <input type='text' name='email'> </td>
</tr>
<tr>
<td colspan='2' align='center'> <textarea cols='60' rows='5' name='message'></textarea> </td>
</tr>
<tr>
<td colspan='2' align='center'> <input type='submit' name='submit_guestbook' value='submit message'> </td>
</tr>
</table>
</form>
<?php
}
?>
And we're done! Now we have our own form wich lets the user add a message to the guestbook (database).
Now we only have to SHOW all messages and get the info from the database.
Let's do it!
5. Overview of all messages in the guestbook
For this we'll use the SELECT query, to get all info from the database.
It works like:
CODE
$anyvar = mysql_query("SELECT fieldnames FROM tablename WHERE clause ORDER BY field [ordertype] LIMIT [amount] ");
Our fields we need to get the values of: date, author, email, message
In our case there's no clause, we just select all messages from the database(guestbook).
We'll order them by ID (so a higher ID = later added), in our case the ordertype will be DESC (= descending).
So it will first select the fields with the highest id(caus it would order by id(ORDER BY id)).
We'll first don't use LIMIT, so it will just select ALL rows it find with the query and select them descending, ordering by id(starting with the highest id one(so the one which is added as last) and then go select descending).
CODE
$messages = mysql_query("SELECT * FROM messages ORDER BY id DESC");
We'll use * to define it haves to select ALL fields, you can also set instead: date, author, email, message caus the field id we won't actually need.
Now we have to set the values of the fields it selected with this query, into a array. We'll use mysql_fetch_assoc(query).
CODE
while($row = mysql_fetch_assoc($messages)) {
...
}
...
}
it will check: as long that $row can contain a new row which have been selected with the query in $messages, then do the follow.
All fields it select with the query, will be set(in our case) into a array for the variable $row.
$row[fieldname] will contain the value it selected from fieldname.
Take for example: there are 2 messages in our database 'messages' table, on with as value of the 'message' field: "Test message" and the other: "Hello!".
Now, if we would use our query and loop, and echo $row['message'] it will return: Testmessage Hello!
Caus it will find 2 rows, first it will do the loop for the first row, there he find one with as message "Test message" , and it defines that as value for $row['message'], and then it echo's it, and the same for the other row.
Oke, let's make our guestbook messages overview!
CODE
$messages = mysql_query("SELECT * FROM messages ORDER BY id DESC");
while($message = mysql_fetch_assoc($messages)) {
?>
<table class='guestbook' align='center'>
<tr>
<td> <font size='1' color='#BABABA'><?php echo $message['date']; ?></font> </td>
</tr>
<tr>
<td> <?php echo $message['message']; ?> </td>
</tr>
<tr>
<td align='right'> <font size='1'><b><?php echo $message['author']."</b>(<a href='mailto:".$message['email']."'><font size='1'>".$message['email']."</font></a>)"; ?></font> </td>
</tr>
</table><br>
<?php
}
?>
while($message = mysql_fetch_assoc($messages)) {
?>
<table class='guestbook' align='center'>
<tr>
<td> <font size='1' color='#BABABA'><?php echo $message['date']; ?></font> </td>
</tr>
<tr>
<td> <?php echo $message['message']; ?> </td>
</tr>
<tr>
<td align='right'> <font size='1'><b><?php echo $message['author']."</b>(<a href='mailto:".$message['email']."'><font size='1'>".$message['email']."</font></a>)"; ?></font> </td>
</tr>
</table><br>
<?php
}
?>
it will select all rows it finds in the table 'messages' and each time it finds a new row, it will run the while loop, set all values of the fields into $message['fieldname'] and then run that script (it will make a new table, show the message in a new table and the author of the message, etc.).
Now lets spread the messages in the guestbook about more pages than only 1, cause else, if there have been added a lot of rows(messages) to the database, it would show them all.
We'll use the LIMIT thing for this.
CODE
$messages = mysql_query("SELECT * FROM messages ORDER BY id DESC LIMIT $limit ");
now take for example, $limit is equal to : 0,5
this means it will select (descending ordered by the field 'id') the first 5 rows, starting at the row with the highest id - then descending the first 5 fields it finds.
If $limit would be equal to 5,5; it would select the first 5 rows, starting at the row with the highest id - 5,descending, so if that would be 20, it would select the fields with the id's 15-10 (10-15).
CODE
$guestbook_limit = 5; //set this to the amount of messages(rows) you want to be showed per 'page'
if(!isset($_GET['page']) || empty($_GET['page']) || $_GET['page'] == 1) {
$page = 1;
$firstlimit = 0;
$limit = $firstlimit.",".$guestbook_limit;
}else{
$page = $_GET['page'];
$lastlimit = $_GET['page'] * $guestbook_limit;
$firstlimit = $lastlimit-$guestbook_limit;
$limit = $firstlimit.",".$guestbook_limit;
}
if(!isset($_GET['page']) || empty($_GET['page']) || $_GET['page'] == 1) {
$page = 1;
$firstlimit = 0;
$limit = $firstlimit.",".$guestbook_limit;
}else{
$page = $_GET['page'];
$lastlimit = $_GET['page'] * $guestbook_limit;
$firstlimit = $lastlimit-$guestbook_limit;
$limit = $firstlimit.",".$guestbook_limit;
}
This will check if the $_GET['page'] does exist(if the var page is given a value to in the url), if so:
it will set $page equal to it (for example, $_GET['page'] is equal to 2) then it will generate the last limit by doing this (2) * $guestbook_limit (5 in our case, so we'll show 5 rows (messages) per page), so if $page = 2, it will be 5*2 = 10, then the $firstlimit = $lastlimit-5 = 5, so the finaly $limit will be:
CODE
5,5
, so if the page = 2, the limit will be 5,5 , so it selects if you would have 20 messages, it would select the ones: starting at 5 descending (20-5 = 15) and then select the first 5 rows descending, so it will select the rows with the ids: 15-10 (10-15).It's quite hard to explain, so I hope you'd understand, anyways; this script will just generate the messages it should select descending by looking at the page given in th url(http://file.php?page=amount).
Now we have to show the correct links to the different limit 'pages'.
You can just do it by hand:
CODE
<a href='index.php?page=1'>page1</a>
<a href='index.php?page=2'>page2</a>
etc..
<a href='index.php?page=2'>page2</a>
etc..
or just make a loop for it.
CODE
$amount_messages = mysql_num_rows(mysql_query("SELECT * FROM messages"));
?><table align='center'>Page <?php
for($i=0;$i<=$amount_messages;$i=$i+$guestbook_limit) {
$lasti = $i+5;
$ipage = $lasti/$guestbook_limit;
echo "<a href='".$_SERVER['PHP_SELF']."?act=guestbook&&page=".$ipage."'> ".$ipage." </a> ";
if($i+$guestbook_limit <= $amount_messages) {
echo " - ";
}
}
?><table align='center'>Page <?php
for($i=0;$i<=$amount_messages;$i=$i+$guestbook_limit) {
$lasti = $i+5;
$ipage = $lasti/$guestbook_limit;
echo "<a href='".$_SERVER['PHP_SELF']."?act=guestbook&&page=".$ipage."'> ".$ipage." </a> ";
if($i+$guestbook_limit <= $amount_messages) {
echo " - ";
}
}
it will first get the amount of rows(messages) by the query in $amount_messages, we use mysql_num_rows() which will count the amount of results of the query which selects all rows from the table 'messages'. Then it will set $i equal to 0, and as long it's smaller than the id of the last row(message), it will increase it by 5 (so the page will jump up by 1, caus each page contains 5 rows/messages), then it will generate the page and show a link to it.
I can't explain it but will just give the code with a bit description, for those who'd not like to add the links by theirself but letting the system do it by a script.
And we put it together.
CODE
$messages = mysql_query("SELECT * FROM messages ORDER BY id DESC LIMIT $limit");
$guestbook_limit = 5;
if(!isset($_GET['page']) || empty($_GET['page']) || $_GET['page'] == 1) {
$page = 1;
$firstlimit = 0;
$limit = $firstlimit.",".$guestbook_limit;
}else{
$page = $_GET['page'];
$lastlimit = $_GET['page'] * $guestbook_limit;
$firstlimit = $lastlimit-$guestbook_limit;
$limit = $firstlimit.",".$guestbook_limit;
}
while($message = mysql_fetch_assoc($messages)) {
?>
<table class='guestbook' align='center'>
<tr>
<td> <font size='1' color='#BABABA'><?php echo $message['date']; ?></font> </td>
</tr>
<tr>
<td> <?php echo $message['message']; ?> </td>
</tr>
<tr>
<td align='right'> <font size='1'><b><?php echo $message['author']."</b>(<a href='mailto:".$message['email']."'><font size='1'>".$message['email']."</font></a>)"; ?></font> </td>
</tr>
</table><br>
<?php
}
//optional:
$amount_messages = mysql_num_rows(mysql_query("SELECT * FROM messages"));
?><table align='center'>Page <?php
for($i=0;$i<=$amount_messages;$i=$i+$guestbook_limit) {
$lasti = $i+5;
$ipage = $lasti/$guestbook_limit;
echo "<a href='".$_SERVER['PHP_SELF']."?act=guestbook&&page=".$ipage."'> ".$ipage." </a> ";
if($i+$guestbook_limit <= $amount_messages) {
echo " - ";
}
}
?>
$guestbook_limit = 5;
if(!isset($_GET['page']) || empty($_GET['page']) || $_GET['page'] == 1) {
$page = 1;
$firstlimit = 0;
$limit = $firstlimit.",".$guestbook_limit;
}else{
$page = $_GET['page'];
$lastlimit = $_GET['page'] * $guestbook_limit;
$firstlimit = $lastlimit-$guestbook_limit;
$limit = $firstlimit.",".$guestbook_limit;
}
while($message = mysql_fetch_assoc($messages)) {
?>
<table class='guestbook' align='center'>
<tr>
<td> <font size='1' color='#BABABA'><?php echo $message['date']; ?></font> </td>
</tr>
<tr>
<td> <?php echo $message['message']; ?> </td>
</tr>
<tr>
<td align='right'> <font size='1'><b><?php echo $message['author']."</b>(<a href='mailto:".$message['email']."'><font size='1'>".$message['email']."</font></a>)"; ?></font> </td>
</tr>
</table><br>
<?php
}
//optional:
$amount_messages = mysql_num_rows(mysql_query("SELECT * FROM messages"));
?><table align='center'>Page <?php
for($i=0;$i<=$amount_messages;$i=$i+$guestbook_limit) {
$lasti = $i+5;
$ipage = $lasti/$guestbook_limit;
echo "<a href='".$_SERVER['PHP_SELF']."?act=guestbook&&page=".$ipage."'> ".$ipage." </a> ";
if($i+$guestbook_limit <= $amount_messages) {
echo " - ";
}
}
?>
6. The whole guestbook
Oke, let's put everything together for our whole guestbook!
I'll add some comments to, so you know how to put all together in what order, so you understand the structure.
CODE
##########################################################
####THIS PART WILL SHOW ALL MESSAGES IN THE GUESTBOOK###############
#########################################################
$messages = mysql_query("SELECT * FROM messages ORDER BY id DESC LIMIT $limit");
$guestbook_limit = 5;
if(!isset($_GET['page']) || empty($_GET['page']) || $_GET['page'] == 1) {
$page = 1;
$firstlimit = 0;
$limit = $firstlimit.",".$guestbook_limit;
}else{
$page = $_GET['page'];
$lastlimit = $_GET['page'] * $guestbook_limit;
$firstlimit = $lastlimit-$guestbook_limit;
$limit = $firstlimit.",".$guestbook_limit;
}
while($message = mysql_fetch_assoc($messages)) {
?>
<table class='guestbook' align='center'>
<tr>
<td> <font size='1' color='#BABABA'><?php echo $message['date']; ?></font> </td>
</tr>
<tr>
<td> <?php echo $message['message']; ?> </td>
</tr>
<tr>
<td align='right'> <font size='1'><b><?php echo $message['author']."</b>(<a href='mailto:".$message['email']."'><font size='1'>".$message['email']."</font></a>)"; ?></font> </td>
</tr>
</table><br>
<?php
}
//optional:
$amount_messages = mysql_num_rows(mysql_query("SELECT * FROM messages"));
?><table align='center'>Page <?php
for($i=0;$i<=$amount_messages;$i=$i+$guestbook_limit) {
$lasti = $i+5;
$ipage = $lasti/$guestbook_limit;
echo "<a href='".$_SERVER['PHP_SELF']."?act=guestbook&&page=".$ipage."'> ".$ipage." </a> ";
if($i+$guestbook_limit <= $amount_messages) {
echo " - ";
}
}
###############################################################
#######THIS PART WILL MAKE A FORM WHICH LETS THE USER ADD A MESSAGE#########
##############################################################
if(isset($_POST['submit_guestbook'])) {
if(!empty($_POST['author']) && !empty($_POST['message']) && !empty($_POST['email'])) {
//this will be the case if all fields are filled in(author, message, email), so we can add the message to the database
$add_message = mysql_query("INSERT INTO messages (author, message, email) VALUES ('".$_POST['author']."', '".$_POST['message']."', '".$_POST['email']."' ") ") or die ("<font color='red'>Couldn't add your message! \n Please try again.</font>");
}else{
//this will be the case if the user did NOT fill in all fields(author, message, email)
echo "<font color='red'>Please fill in all fields!</font>";
}
}else{
//show the form
?>
<form method='POST' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<table style='border:1px solid #BABABA'>
<tr>
<td> <b>username:</b> </td>
<td> <input type='text' name='author'> </td>
</tr>
<tr>
<td> <b>email:</b> </td>
<td> <input type='text' name='email'> </td>
</tr>
<tr>
<td colspan='2' align='center'> <textarea cols='60' rows='5' name='message'></textarea> </td>
</tr>
<tr>
<td colspan='2' align='center'> <input type='submit' name='submit_guestbook' value='submit message'> </td>
</tr>
</table>
</form>
<?php
}
?>
####THIS PART WILL SHOW ALL MESSAGES IN THE GUESTBOOK###############
#########################################################
$messages = mysql_query("SELECT * FROM messages ORDER BY id DESC LIMIT $limit");
$guestbook_limit = 5;
if(!isset($_GET['page']) || empty($_GET['page']) || $_GET['page'] == 1) {
$page = 1;
$firstlimit = 0;
$limit = $firstlimit.",".$guestbook_limit;
}else{
$page = $_GET['page'];
$lastlimit = $_GET['page'] * $guestbook_limit;
$firstlimit = $lastlimit-$guestbook_limit;
$limit = $firstlimit.",".$guestbook_limit;
}
while($message = mysql_fetch_assoc($messages)) {
?>
<table class='guestbook' align='center'>
<tr>
<td> <font size='1' color='#BABABA'><?php echo $message['date']; ?></font> </td>
</tr>
<tr>
<td> <?php echo $message['message']; ?> </td>
</tr>
<tr>
<td align='right'> <font size='1'><b><?php echo $message['author']."</b>(<a href='mailto:".$message['email']."'><font size='1'>".$message['email']."</font></a>)"; ?></font> </td>
</tr>
</table><br>
<?php
}
//optional:
$amount_messages = mysql_num_rows(mysql_query("SELECT * FROM messages"));
?><table align='center'>Page <?php
for($i=0;$i<=$amount_messages;$i=$i+$guestbook_limit) {
$lasti = $i+5;
$ipage = $lasti/$guestbook_limit;
echo "<a href='".$_SERVER['PHP_SELF']."?act=guestbook&&page=".$ipage."'> ".$ipage." </a> ";
if($i+$guestbook_limit <= $amount_messages) {
echo " - ";
}
}
###############################################################
#######THIS PART WILL MAKE A FORM WHICH LETS THE USER ADD A MESSAGE#########
##############################################################
if(isset($_POST['submit_guestbook'])) {
if(!empty($_POST['author']) && !empty($_POST['message']) && !empty($_POST['email'])) {
//this will be the case if all fields are filled in(author, message, email), so we can add the message to the database
$add_message = mysql_query("INSERT INTO messages (author, message, email) VALUES ('".$_POST['author']."', '".$_POST['message']."', '".$_POST['email']."' ") ") or die ("<font color='red'>Couldn't add your message! \n Please try again.</font>");
}else{
//this will be the case if the user did NOT fill in all fields(author, message, email)
echo "<font color='red'>Please fill in all fields!</font>";
}
}else{
//show the form
?>
<form method='POST' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<table style='border:1px solid #BABABA'>
<tr>
<td> <b>username:</b> </td>
<td> <input type='text' name='author'> </td>
</tr>
<tr>
<td> <b>email:</b> </td>
<td> <input type='text' name='email'> </td>
</tr>
<tr>
<td colspan='2' align='center'> <textarea cols='60' rows='5' name='message'></textarea> </td>
</tr>
<tr>
<td colspan='2' align='center'> <input type='submit' name='submit_guestbook' value='submit message'> </td>
</tr>
</table>
</form>
<?php
}
?>
AND WE'RE DONE!
Good luck!
I hope you'd understand the most
If you have any questions, feel free to ask me.
Skyfe.
discuss this topic to forum
