1) database.php
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("pagination")or die(mysql_error());
2) page.php
<?php
include('database.php');
if(isset($_GET['page']))
{
$current_page=$_GET['page'];$current_page=mysql_real_escape_string($current_page);
}
else$current_page=1;
$result =mysql_query("SELECT * FROM `names`");
$rows=mysql_num_rows($result);
$per_page=5;
$numpages=ceil($rows/$per_page);
/******************************comment******************************
clicked page minus 1 multiplied by 5 if page =2 then (1*5=5)limit=5
******************************comment*******************************/
$limit=($current_page-1)*$per_page;$query=mysql_query("SELECT * FROM `names` LIMIT $limit ,$per_page");
while($row=mysql_fetch_array($query))
{
echo $row['name'].' '.$row['name_id'].'<br><br>';
}
{
echo "<a href='page.php?page=1'> << first</a>"." ";$previous=$current_page-1;
echo "<a href='page.php?page=$previous'> << Previous </a>"." ";
}
{
echo '|';
}
$buffer = ($range % 2)? ceil($range/2)-1 : ceil($range/2);
if($numpages != $range && $range < $numpages)
{
$t_beginno = $current_page - $buffer;
$t_endno = (range % 2)? $current_page + $buffer : $current_page + ($buffer-1);$endno = ($t_beginno < 1 )? $range : (($numpages - $current_page < $buffer)? $numpages : $t_endno);
}
else
{ $beginno = 1; $endno = $numpages;}
//______________________________________
for($page=$beginno;$page<=$endno;$page++)
{
}
if($current_page==1)
{
$next=$current_page+1;
echo "<a href='page.php?page=$next'> << next</a>"." ";
echo "<a href='page.php?page=$numpages'> << last </a>"." ";
}
if(($current_page!=1) && ($current_page!=$numpages))
{
echo '|';
}
{
$next=$current_page+1;echo "<a href='page.php?page=$next'> << next</a>"." ";
}
CREATE TABLE IF NOT EXISTS `names` (
`name_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
PRIMARY KEY (`name_id`)
);
then insert values in the Database
watch live view of codeExplanation:
database.php:
1) we are connecting to our server using mysql_connect()
2) Then we are selecting the database using mysql_select_db()
page.php:
1) include('database.php'); include the database.php in page.php so that every time when page.php is executed it gets connection and database details from database.php
2) if(isset($_GET['page']))
{
$current_page=$_GET['page'];
$current_page=mysql_real_escape_string($current_page);
}
else
$current_page=1;
when we execute page.php user gets the following number eg:- 1 2 3 4 5 6 7 Next>> last>>
if the user click on any of the above number then that number will be stored in Gobal variable called $_GET
so the line if(isset($_GET['page'])) indicates that if the user has clicked on any of the number and that number is taken in a variable 'page' if this condition is successful then we are taking that page number in a variable called '$current_page' as
$current_page=$_GET['page']; suppose you have cliked on number three(3) then the value of $current_page is 3.
* In the next line
$current_page=mysql_real_escape_string($current_page);
we are passing the value through mysql_real_escape_string() this function is used to prevent SQL injection.
*Then
else
$current_page=1;
indicates if user has not clicked then the default page is 1
No comments:
Post a Comment