Page 1 of 1

Previous and Next page navigation links in Pagination Code

Posted: Mon Jul 30, 2007 7:34 am
by dream2rule
Hello All,

I have written a code for pagination in php and mysql but i need to display previous and next page links as i have records exceeding 500.

And also i would like you all to help me optimize the code as i am just a beginner in PHP.

Code: Select all

<?php
//connecting to the database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("live_search") or die(mysql_error());

//retrieving query string values
$lower_bound = $_GET['lb'];
$upper_bound = $_GET['ub'];
$page_no = $_GET['page'];

if((isset($upper_bound)) && (isset($lower_bound)))
{
	$bound = ($upper_bound - $lower_bound) + 1;
	//echo "Bound - ".$bound."<br>";
	
	$sql_data = "SELECT * FROM ajax_live_search ORDER BY area LIMIT $lower_bound,$bound";
	//echo $sql_data."<br>";
	$rs_data = mysql_query($sql_data);
	
	while($row = mysql_fetch_array($rs_data))
	{
		echo "<table cellpadding='0' cellspacing='0' border='0' width='50%'><tr>$row[1]</tr></table>";
	}
}

//retrieving the total number of records from db
$sql = "SELECT area FROM ajax_live_search ORDER BY area";
$rs = mysql_query($sql);
$total_records = mysql_num_rows($rs);
$limit_per_page = 20;
$number_of_pages = ceil($total_records/$limit_per_page);

//calculating the lower bound and upper bound for the page
$lb=0; //initial value

for($i=1; $i <= $number_of_pages; $i++)
{	
	$ub = $lb + ($limit_per_page - 1);
	
	if($i < $number_of_pages)
	{	
		//if viewing the current page, link does not appear else the link to that page appears
		if($i == $page_no)
			echo "&nbsp;".$i."&nbsp;|";
		else
			echo "&nbsp;"."<a href=pagination.php?page=$i&lb=$lb&ub=$ub>".$i."</a>"."&nbsp;|";
	}
	else
	{
		if($i == $page_no)
			echo "&nbsp;".$i."&nbsp;";
		else
			echo "&nbsp;"."<a href=pagination.php?page=$i&lb=$lb&ub=$total_records>".$i."</a>"."&nbsp;";
	}
	$lb = $ub + 1;
}
?>
Any help would be highly appreciated.

Thanks and Regards,
Dream2rule

Posted: Mon Jul 30, 2007 7:42 am
by boo
I think the only suggestion that I have is to change this SQL statement

Code: Select all

$sql = "SELECT area FROM ajax_live_search ORDER BY area";
To this

Code: Select all

$sql = "SELECT COUNT(*) FROM ajax_live_search";
Then you just use the value coming from the SQL and you will not need to use mysql_num_rows

I dont see a reason to bring back all of the records if you are just looking for the count of the records in the file.

Posted: Mon Jul 30, 2007 7:44 am
by dream2rule
Oh yes truly.. Thanks!

and any idea about the previous and next links?

Regards

Posted: Mon Jul 30, 2007 7:53 am
by dream2rule

Code: Select all

$sql = "SELECT count(*) FROM ajax_live_search";
$rs = mysql_query($sql);
$total_records = $rs[0];
echo "Total Records - ".$total_records."<br>";
In the above code, i ain't getting the value in the $total_records :(

Can anyone help?

Regards

Posted: Mon Jul 30, 2007 8:26 am
by superdezign
dream2rule wrote:

Code: Select all

$sql = "SELECT count(*) FROM ajax_live_search";
$rs = mysql_query($sql);
$total_records = $rs[0];
echo "Total Records - ".$total_records."<br>";
In the above code, i ain't getting the value in the $total_records :(

Can anyone help?

Regards
$rs is a resource, not an array. You're missing a step.

Posted: Tue Jul 31, 2007 12:03 am
by dream2rule
yeah.. finally got it.

Thanks a tonne :) :)

Re: Previous and Next page navigation links in Pagination Code

Posted: Thu Jun 26, 2008 5:22 am
by washim007
<?php
/*
Place code to connect to your DB here.
*/
//include('config.php'); // include your code to connect to DB.


$tbl_name="details_tb"; //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;

/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name where car_id_no like '%$car_id_no%'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

/* Setup vars for query. */
$targetpage = "welcome.php"; //your file name (the name of this file)
$limit = 4; //how many items to show per page
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0

/* Get data. */
$sql = "SELECT * FROM $tbl_name where car_id_no like '%$car_id_no%' LIMIT $start, $limit";
$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1

/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
else
$pagination.= "<span class=\"disabled\">« previous</span>";

//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}

//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
else
$pagination.= "<span class=\"disabled\">next »</span>";
$pagination.= "</div>\n";
}
?>