Page navigation using PHP
In this post i will explain how to write a comments page with page navigation like first page, next page, previous page and last page links. You can use the same logic to write any kind of page, article, blog etc. This article assumes you have basic understanding of PHP and MySql.
Writing comments/suggestions in a blog is a common thing. With PHP and MySql it is the easier too. A simple comment page would require a comment field, a name field and email field. A captcha is optional but it is strongly recommended that you use captcha to prevent automated entries by bots.
Ok. First write html tags to design entry form. The form would consist of a textarea field to capture multilines of text. Two text fields one for name and another for email. Here is the code for the form
I have commented the html code to prevent browser from displaying the controls instead of the code. Now when the enters data and click the submit button the php code will work. The if($_POST) will check whether a post back (submit) is done. If so the code will connect mysql server and open default database and insert a row of data. Ok. I forgot the script to create a mysql table, here is the sql script to create 'comments' table,
CREATE TABLE comments (
`ID` int(11) NOT NULL auto_increment,
`comment_date` DATETIME NOT NULL,
`comment` varchar(5000) NOT NULL,
`comment_by` varchar(55) default NULL,
`email` varchar(50) default NULL,
PRIMARY KEY (`ID`)
);
Below is the php code that is used to insert data into mysql table ('comments')
include 'header.html';
if ($_POST)
{
$link = mysql_connect("localhost", "root", "myuniverse"); //replace with your mysql server details.
if (!$link)
{
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db("test", $link); //replace with your mysql database name
if (!$db_selected)
{
die ('Error: ' . mysql_error());
}
$comment=$_POST["comment"];
$comment_by=$_POST["comment_by"];
$email=$_POST["email"];
if ($comment=="")
{
echo 'Empty comments are not accepted!';
}
else
{
$qry="INSERT INTO comments (comment_date,comment,comment_by,email) VALUES (curdate(),
'$comment','$comment_by','$email')";
$result = mysql_query($qry);
if ($result)
{
echo 'You comment was added successfully. Thank You';
}
else
{
echo 'Error: '. mysql_error();
}
}
}
?>
Now, we have inserted the code. We need to display the data. It is certain that we should not display all the comments on the same page. If your website gets older the number of comments will grow and displaying it on a single page is not a good idea. So we split up the data and display a small amount of comments on a page. The user can click the Next, Previous links to navigate the comments page. For this purpose we need to count the number of rows in the comments table and divide it by number of rows per page. We can set this value to a constant. For example 10,20,30 or something you want. The following code will do the same. It will determine the total pages and current page and create Next Page, Previous page, Last page links with parameter (page no). Here is the piece of code to display comments,
$link = mysql_connect("localhost", "root", "myuniverse");
if (!$link)
{
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db("test", $link);
if (!$db_selected)
{
die ('Error: ' . mysql_error());
}
$result = mysql_query("SELECT comment,comment_by from comments ORDER BY comment_date DESC");
echo '
Read Comments
';$tot_rows=mysql_num_rows($result);
if (isset($_GET["pageno"]))
$pageno=$_GET["pageno"];
else
$pageno=1;
$rowsperpage=10;
$currow=($pageno-1)*$rowsperpage;
if ($tot_rows>0)
mysql_data_seek($result,$currow);
$cnt=0;
$totalpages=ceil($tot_rows/$rowsperpage);
while ($row = mysql_fetch_assoc($result))
{
echo '
';';
echo ''.$row["comment"].'
';
echo $row["comment_by"];
echo '
$cnt=$cnt+1;
if ($cnt>=$rowsperpage)
break;
}
echo '
';
$next=$pageno+1;
$previous=$pageno-1;
echo 'Total comments: '.$tot_rows;
echo '
';
if ($pageno>1)
echo 'First page ';
if ($pageno<$totalpages)
echo 'Next page > ';
if ($pageno>1)
echo ' Previous page ';
if ($pageno<$totalpages)echo 'Last page';
include 'footer.html';
?>
Replace the mysql server name, user name and password to your own server name, username password in mysql_connect function. Also change the database name in mysql_select_db() function. I have used include 'header.html' and include 'footer.html'. You should create your own header and footer files that display header and footer of your website. Otherwise remove those lines.
Here is the complete code:
Save the file to comments.php or something else and run! The output should look as below. Note: I have used style sheets for my header.html so the colors vary.
No comments:
Post a Comment