Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem here, I have a table with data inside it and do some pagination, the problem is, I cant pass the data between the page inside pagination, for example, I have a count function, and will view 10 data per page, but the count will return back to 1 if I go to next page, instead of 11, please can somebody help me? here a the code to help you guys see more clearly :

Code:
PHP
<?php require_once('auth.php'); ???>
<!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>
<style>
#tengah 
{border:10px outset #0066FF;} 

#tepiatas 
{border:5px outset #0066FF;}

.paginate {
font-family:Arial, Helvetica, sans-serif;
	padding: 3px;
	margin: 3px;
}

.paginate a {
	padding:2px 5px 2px 5px;
	margin:2px;
	border:1px solid #999;
	text-decoration:none;
	color: #666;
}
.paginate a:hover, .paginate a:active {
	border: 1px solid #999;
	color: #000;
}
.paginate span.current {
    margin: 2px;
	padding: 2px 5px 2px 5px;
		border: 1px solid #999;
		
		font-weight: bold;
		background-color: #999;
		color: #FFF;
	}
	.paginate span.disabled {
		padding:2px 5px 2px 5px;
		margin:2px;
		border:1px solid #eee;
		color:#DDD;
	}
	
		
	ul{margin:6px;
	padding:0px;}	

</style>
<link rel="shortcut icon" href="quran.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>

</head>
<body bgcolor="white">
<table width="100%" border="1" align="center" bgcolor="white">
  <tr>
  <td id="tepiatas" height="140" colspan="3">
</td>
  </tr>
  <tr>
  
	
    <td id="tepiatas" width="15%" height="536" align="center" bgcolor="white">
	<?php include 'menu.php'; ???>
	</td>
	
    <!-- menu section -->
	
    <td id="tengah" width="85%" bgcolor="white">
	
	<h3><center>Senarai Kilang di Fasa 1</center>
	<br />
	<br />
	
include 'dbconnect.php';
$tableName="kilang_fasa1";		
	$targetpage = "fasa1add_view.php"; 	
	$limit = 10; 
	$count ='0';
	
	$query = "SELECT COUNT(*) as num FROM $tableName";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	$stages = 3;
	$page = mysql_escape_string($_GET['page']);
	if($page){
		$start = ($page - 1) * $limit; 
	}else{
		$start = 0;	
		}	
	
    // Get page data
	$query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
	$result = mysql_query($query1);
	
	// Initial page num setup
	if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;					
	
	
	$paginate = '';
	if($lastpage > 1)
	{	
	

	
	
		$paginate .= "<div class="paginate">";
		// Previous
		if ($page > 1){
			$paginate.= "<a href="$targetpage?page=$prev">previous</a>";
		}else{
			$paginate.= "previous";	}
			

		
		// Pages	
		if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page){
					$paginate.= "$counter";
				}else{
					$paginate.= "<a href="$targetpage?page=$counter">$counter</a>";}					
			}
		}
		elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($page < 1 + ($stages * 2))		
			{
				for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $page){
						$paginate.= "$counter";
					}else{
						$paginate.= "<a href="$targetpage?page=$counter">$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href="$targetpage?page=$LastPagem1">$LastPagem1</a>";
				$paginate.= "<a href="$targetpage?page=$lastpage">$lastpage</a>";		
			}
			// Middle hide some front and some back
			elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
			{
				$paginate.= "<a href="$targetpage?page=1">1</a>";
				$paginate.= "<a href="$targetpage?page=2">2</a>";
				$paginate.= "...";
				for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
				{
					if ($counter == $page){
						$paginate.= "$counter";
					}else{
						$paginate.= "<a href="$targetpage?page=$counter">$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href="$targetpage?page=$LastPagem1">$LastPagem1</a>";
				$paginate.= "<a href="$targetpage?page=$lastpage">$lastpage</a>";		
			}
			// End only hide early pages
			else
			{
				$paginate.= "<a href="$targetpage?page=1">1</a>";
				$paginate.= "<a href="$targetpage?page=2">2</a>";
				$paginate.= "...";
				for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "$counter";
					}else{
						$paginate.= "<a href="$targetpage?page=$counter">$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($page < $counter - 1){ 
			$paginate.= "<a href="$targetpage?page=$next">next</a>";
		}else{
			$paginate.= "next";
			}
			
		$paginate.= "</div>";		
	
	
}
echo '<center>';
echo 'Sebanyak ' .$total_pages.' Kilang Ditemui';
echo '<br>';
echo '<br>';
 // pagination
 echo $paginate;
 echo '</br></br></center>';
 echo '<br>';
 echo '<br>';
'<ul>';		

echo '<center>';
echo '<table width="50%" border="1">';
echo '<tr>';
echo'<td width="5%" align="center">Bil</td>';
echo'<td width="5%" align="center">Nombor Fail</td>';
echo'<td width="10%" align="center">Kod Kilang</td>';
echo'<td width="25%" align="center">Nama Kilang</td>';
echo'<td width="5%" align="center">Data Penuh</td>';
echo'</tr>';


while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
	 	$count = $count+1;
		echo '<tr>';
		echo '<td align="center">'.$count.'</td>';
		echo '<td align="center">'.$row['no_fail'].' </td>';
		echo '<td align="center">'.$row['kod_kilang'].' </td>';
		echo '<td align="center">'.$row['nama_kilang'].'</td>';
		?>
		<td align="center"><a href="fasa1add_viewmore.php?id=<?php echo $row['no_fail']; ?>">Semak</a></td>
				echo '</tr>';
		
	}
	
	echo '</table>';
	echo '<center>';
	echo '<br>';
 	echo '<br>';
	echo $paginate;
?>
</br></br></center></center></ul>
<br />
<br />
<br />
<a href="fasa1add.php">Tambah Data</a> ||
<a href="logout.php">Keluar</a>
	</br></br></h3></td>
	
   
  </tr>
  
</table>
</body>
</html>
Posted
Updated 28-Sep-11 18:38pm
v2

1 solution

you should implement paging by mysql, my mean is you send multi parameter to DB that include:
numRow: number of rows that show every page
countRow: number of rows that return (may in last page countRow <= numRow)
pageIndex: current page

so you should use session to send this parameters to next / prev page.
for example: in first page include these information:
numRow: 10, countRow: ?, pageIndex: 1

if you can not implement this idea, send request me to sent similar code.
good luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900