here's what i already have, whenever i click a specified letter like for example 'A' it must print out and count how many countries starting with that letter. The problem is whenever the count is greater than the specified limit, when i clicked to page 2, it shows also countries starting with letter B.. pls help.

<html>
<body>
<link rel="stylesheet" type="text/css" href="ddcolortabs.css">
<?php
	include('dbconn1.php');\
	if(!$dbconn)
{
		echo "Error: ".mysql_error();
		exit;
}

	$tbl_name="cc_country";	
	$adjacents = 2;
	
	$query = "SELECT COUNT(countryname) AS num FROM ".$tbl_name." WHERE countryname LIKE '".strval(strtoupper($_REQUEST['s']))."%'";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	$targetpage = $_SERVER['PHP_SELF']; 
	$limit = 15; 
	
	if(isset($_GET['page']))
	{
		$page = $_GET['page'];
	}
	
	if($page) 
	{
		$start = ($page - 1) * $limit; 
	}
	else
	{
		$start = 0;	
	}
	
	$sql = "SELECT * FROM ".$tbl_name." WHERE countryname LIKE '".strval(strtoupper($_REQUEST['s']))."%' ORDER BY countryname ASC LIMIT $start, $limit";
	$result = mysql_query($sql);
	
	if ($page == 0) $page = 1;	
	$prev = $page - 1;	
	$next = $page + 1;	
	$lastpage = ceil($total_pages/$limit);	
	$lpm1 = $lastpage - 1;
	
	$pagination = "";
	if($lastpage > 1)
	{	
		if ($page > 1) 
		{
			$pagination.= "<a href=\"$targetpage?page=$prev\" class=\"link\">«</a>&nbsp;&nbsp;";
		}
		else
		{
			$pagination.= "<span class=\"disabled\">«</span>";	
		}
		
		if ($lastpage < 7 + ($adjacents * 2))
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
				{
					$pagination.= "<span class=\"current\">$counter</span>";
				}
				else
				{
					$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"link\">$counter</a>";					
				}
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))
		{
			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\" class=\"link\">$counter</a>";					
					}
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"link\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"link\">$lastpage</a>";		
			}
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage?page=1\" class=\"link\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\" class=\"link\">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\" class=\"link\">$counter</a>";					
					}
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"link\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"link\">$lastpage</a>";		
			}
			else
			{
				$pagination.= "<a href=\"$targetpage?page=1\" class=\"link\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\" class=\"link\">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\" class=\"link\">$counter</a>";					
					}
				}
			}
		}
		
		if ($page < $counter - 1) 
		{
			$pagination.= "&nbsp;&nbsp;<a href=\"$targetpage?page=$next\" class=\"link\">»</a>";
		}
		else
		{
			$pagination.= "<span class=\"disabled\">»</span>";
		}
	}
?>
<tr>
	<td colspan="2" align="center" style="font-size:10px; text-decoration:underline;">
		Page <?php echo $page; ?> of <?php echo $lastpage; ?>
	</td>
</tr>
<tr>
	<td colspan="2" height="2">
	</td>
</tr>
<tr>
	<td colspan="2" align="center">
		<?php echo $pagination; ?>
	</td>
</tr>
<tr>
	<td colspan="2" height="3">
	</td>
</tr>
<br />
<br />

<tr>

<?php

	echo ("<br>");
	while($row = mysql_fetch_array($result))
	{
		
		echo ("<div class='test'><a href='sourceLink1.php?mod=2&tbl=".$tbl_name."&c=".$row['countryname']."' target='showframe'>".$row['countryname']."</a></div>");
	}
	
	
?>
</tr>
</body>
</html>

Thank you in advance guys. :)

Recommended Answers

All 6 Replies

Dude, that is a lot of code for pagination and with no code tags. Here is a simple pagination script, try incorporating that.

<?
$conn = mysql_connect("localhost", "test", "test");
if(!$conn)
{
	echo "could not connect to db<br />";
}
mysql_select_db("mydev", $conn) or die("could not select db<br />");

$rowsperpage = 100;
$paginatorwidth = 5;

$start = isset($_GET['start'])?$_GET['start'] + 0:0;
$curpage = round($start / $rowsperpage);
$start = $curpage * $rowsperpage;

$_GET['search'] = isset($_GET['search'])?$_GET['search']:"test"; // for development

$query = "select librarypk, bookname from library where bookname like('".$_GET['search']."%') LIMIT ".$start.",".$rowsperpage;
$result = mysql_query($query);
$total = mysql_num_rows($result);

$query = "select count(librarypk) as total from library where bookname like('".$_GET['search']."%')";
$countresult = mysql_query($query);
$counttotal = mysql_result($countresult, 0, "total");

$totalpages = round($counttotal / $rowsperpage);

if($totalpages * $rowsperpage > $counttotal)
{
	$totalpages -= 1;
}

$paginatorlinks = array();
$paginatorlinks[] = '<td><a class="inactive" href="'.$_SERVER['PHP_SELF'].'?start=0&search='.$_GET['search'].'">First</a></td>'."\n";
$paginatorlinks[] = '<td><a class="inactive" href="'.$_SERVER['PHP_SELF'].'?start='.($start - $rowsperpage).'&search='.$_GET['search'].'"><< Previous</a></td>'."\n";
for($i = 0; $i < $totalpages; $i++)
{
	if($i >= $curpage - $paginatorwidth && $i < $curpage + $paginatorwidth)
	{
		if($i == $curpage)
		{
			$class = "class='active'";
		}
		else
		{
			$class = "class='inactive'";
		}
		
		$paginatorlinks[] = '<td style="width:20px;"><a '.$class.' href="'.$_SERVER['PHP_SELF'].'?start='.$i * $rowsperpage.'&search='.$_GET['search'].'">'.$i.'</a></td>'."\n";
	}
}
$paginatorlinks[] = '<td><a class="inactive" href="'.$_SERVER['PHP_SELF'].'?start='.($start + $rowsperpage).'&search='.$_GET['search'].'">Next >></a></td>'."\n";
$paginatorlinks[] = '<td><a class="inactive" href="'.$_SERVER['PHP_SELF'].'?start='.($totalpages * $rowsperpage - $rowsperpage).'&search='.$_GET['search'].'">Last</a></td>'."\n";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
a.inactive{color:blue;}
a.active{color:#FF0000; font-weight:bold;}
</style>
</head>

<body>
<table cellpadding="0" cellspacing="0">
	<tr>
		<?
			foreach($paginatorlinks as $pagelink)
			{
			?>	
				<? echo $pagelink; ?><td>&nbsp;</td>
			<?
			}
		?>
	</tr>
</table>
<?
echo "<p>&nbsp;</p>";
for($i = 0; $i < $total; $i++)
{
	echo mysql_result($result, $i, "librarypk").mysql_result($result, $i, "bookname") . "<br />";
}
?>

</body>
</html>

edited the code above to fix bug in the last page variable because round may go up or down. So I included a little block at line 28 to test the total pages * amount per page and see if it was greater than the total, and if so subtract 1 from total pages. And you probably want to do some type of validation of the inputs before querying the database.

ow thank you for the reply.. i'll try this one today.. :)

Fixed a couple of bugs
1. wouldn't display last page number, now it does
2. pages started at zero, now they start at one
3. on last page next and last were still displayed, fixed.

<?
$conn = mysql_connect("localhost", "test", "test");
if(!$conn)
{
	echo "could not connect to db<br />";
}
mysql_select_db("mydev", $conn) or die("could not select db<br />");

$rowsperpage = 100;
$paginatorwidth = 5;

$start = isset($_GET['start'])?$_GET['start'] + 0:0;
$curpage = round($start / $rowsperpage);
$start = $curpage * $rowsperpage;

$_GET['search'] = isset($_GET['search'])?$_GET['search']:"test"; // for development

$query = "select librarypk, bookname from library where bookname like('".$_GET['search']."%') LIMIT ".$start.",".$rowsperpage;
$result = mysql_query($query);
$total = mysql_num_rows($result);

$query = "select count(librarypk) as total from library where bookname like('".$_GET['search']."%')";
$countresult = mysql_query($query);
$counttotal = mysql_result($countresult, 0, "total");

$totalpages = round($counttotal / $rowsperpage);

if($totalpages * $rowsperpage > $counttotal)
{
	$totalpages -= 1;
}

$paginatorlinks = array();
$paginatorlinks[] = '<td><a class="inactive" href="'.$_SERVER['PHP_SELF'].'?start=0&search='.$_GET['search'].'">First</a></td>'."\n";
$paginatorlinks[] = '<td><a class="inactive" href="'.$_SERVER['PHP_SELF'].'?start='.($start - $rowsperpage).'&search='.$_GET['search'].'"><< Previous</a></td>'."\n";

$loopcontrol = ($counttotal / $totalpages > $totalpages)?$totalpages + 1:$totalpages;

for($i = 0; $i < $loopcontrol; $i++)
{
	if($i >= $curpage - $paginatorwidth && $i < $curpage + $paginatorwidth)
	{
		if($i == $curpage)
		{
			$class = "class='active'";
		}
		else
		{
			$class = "class='inactive'";
		}
		$pagedisplay = $i + 1;
		$paginatorlinks[] = '<td style="width:20px;"><a '.$class.' href="'.$_SERVER['PHP_SELF'].'?start='.$i * $rowsperpage.'&search='.$_GET['search'].'">'.$pagedisplay.'</a></td>'."\n";
	}
}
if($curpage < $totalpages)
{
	$paginatorlinks[] = '<td><a class="inactive" href="'.$_SERVER['PHP_SELF'].'?start='.($start + $rowsperpage).'&search='.$_GET['search'].'">Next >></a></td>'."\n";
	$paginatorlinks[] = '<td><a class="inactive" href="'.$_SERVER['PHP_SELF'].'?start='.($totalpages * $rowsperpage).'&search='.$_GET['search'].'">Last</a></td>'."\n";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
a.inactive{color:blue;}
a.active{color:#FF0000; font-weight:bold;}
</style>
</head>

<body>
<table cellpadding="0" cellspacing="0">
	<tr>
		<?
			foreach($paginatorlinks as $pagelink)
			{
			?>	
				<? echo $pagelink; ?><td>&nbsp;</td>
			<?
			}
		?>
	</tr>
</table>
<?
echo "<p>&nbsp;</p>";
for($i = 0; $i < $total; $i++)
{
	echo mysql_result($result, $i, "librarypk").mysql_result($result, $i, "bookname") . "<br />";
}
?>

</body>
</html>

ow. thank you so much R0bbob, though i have difficulties in fixing it bcoz no tags, but still i can follow.,

hey r0bb0b, good evening! thanks for the reply.. i finally got it worked! sorry for the late reply.. i got others things to do, that why i left the pagination.. but now it wroks fine.. thank you so much.!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.