Page 1 of 1

Almost Pagination?

Posted: Mon Apr 25, 2005 4:53 pm
by Jr
Ok... this is what I came up with but for some reason it won't show me more results when I click on one of the page numbers or next it just shows the pagination stuff and the static stuff. Tt doesn't show the 'prev' button either when its on a page > 1. Then when I click on the '1' page to go back to the results I have already been shown it doesn't show those anymore either. What the heck?!?

Code: Select all

<?php
	/* Set current, prev and next page */
	$page = (!isset($_GET['page']))? 1 : $_GET['page']; 
	$prev = ($page - 1); 
	$next = ($page + 1); 
	
	/* Max results per page */ 
	$max_results = 5; 
	
	/* Calculate the offset */ 
	$from = (($page * $max_results) - $max_results); 
	
	/* Query the db for total results. You need to edit the sql to fit your needs */ 
	$result = mysql_query("select id from members"); 
	
	$total_results = mysql_num_rows($result); 
	
	$total_pages = ceil($total_results / $max_results); 
	
	$pagination = ''; 
	
	/* Create a PREV link if there is one */ 
	if($page > 1) 
	{ 
	$pagination .= '<a href="test.php?page='.$prev.'">Previous</a> '; 
	} 
	
	/* Loop through the total pages */ 
	for($i = 1; $i <= $total_pages; $i++) 
	{ 
	if(($page) == $i) 
	{ 
	$pagination .= $i; 
	} 
	else 
	{ 
	$pagination .= "<a href=\"test.php?page='$i'\">$i</a>"; 
	} 
	} 
	
	/* Print NEXT link if there is one */ 
	if($page < $total_pages)
	{ 
	$pagination .= "<a href=\"test.php?page='$next'\">Next</a>"; 
	} 
	
	/* Now we have our pagination links in a variable($pagination) ready to print to the page. I pu it in a variable because you may want to show them at the top and bottom of the page */ 
	
	/* Below is how you query the db for ONLY the results for the current page */ 
	$result=mysql_query("select * 
	from members 
	LIMIT $from, $max_results "); 
	
	while ($i = mysql_fetch_array($result)) 
	{ 
		print "<table>";
			print "<tr>";
				print "<td>$i[user_name]</td>";
				print "<td>$i[pass]</td>";
			print "</tr>";
		print "</table>";
	} 
		print "<table>";
			print "<tr>";
				print "<td>$pagination</td>";
			print "</tr>";
		print "</table>";

?>

Posted: Tue Apr 26, 2005 3:17 am
by pleigh
i have a pagination code here if you like...it works and very simple....if you like, i will post it for you...

Posted: Tue Apr 26, 2005 10:32 am
by Jr
yeah, if you could post it up that would be great!

I mainly just want to get one that works so I can disect it and actually understand how it works.

Posted: Wed Apr 27, 2005 1:58 am
by pleigh

Code: Select all

//start of pagination
$display = 10;
	
	
	if (isset($_GET['np'])) 
	{ 
		$num_pages = $_GET['np'];
	} 
	else 
	{ 
		$query = "SELECT * FROM posts"; 
		$query_result = mysql_query ($query);
		$num_records = @mysql_num_rows ($query_result);
		
		if ($num_records > $display) 
		{ 
			$num_pages = ceil ($num_records/$display);
		} 
		else 
		{
			$num_pages = 1;
		}
	}
	
	
	if (isset($_GET['s'])) 
	{ 
		$start = $_GET['s'];
	} 
	else 
	{
		$start = 0;
	}
			
	// Make the query.
	$query = "SELECT DATE_FORMAT(postupdate, '%M %e, %Y - %l:%i %p'), topic, postID, firstname FROM posts AS p, users AS u
			WHERE p.userID = u.userID ORDER BY postupdate DESC LIMIT $start, $display";		
	$result = @mysql_query ($query); 
	$num = mysql_num_rows ($result); 
	
	if ($num > 0) 
	{ 	
		if ($num_pages > 1) 
		{			
			
			$current_page = ($start/$display) + 1;
			
			echo '<table border=0 cellpadding=2 cellspacing=0 bordercolor=#ffffff><tr>';
			if ($current_page != 1) 
			{
				echo '<td class=tableborder><a href="report.php?s=' . ($start - $display) . 
				'&np=' . $num_pages . '" class=under><b/>Previous</a></td> ';
			}
			
			
			for ($i = 1; $i <= $num_pages; $i++) 
			{
				if ($i != $current_page) 
				{
					echo '<td class=tableborder><a href="report.php?s=' . (($display * ($i - 1))) . 
					'&np=' . $num_pages . '" class=under><b/>' . $i . '</a></td> ';
				} 
				else 
				{
					echo '<td bgcolor="yellow" class=tableborder><b/>'. $i . ' </td>';
				}
			}
			
			
			if ($current_page != $num_pages) 
			{
				echo '<td class=tableborder><a href="report.php?s=' . ($start + $display) . 
				'&np=' . $num_pages . '" class=under><b/>Next</a></td>';
			}
			echo '</tr></table>';
									
		}		
		
		echo "<table width=100% border=1 bordercolor=#336699 cellpadding=0 cellspacing=0 class=tablehead>
		<tr align=center class=tablehead><td width=20% class=tableborder><table><tr><td><b>NAME</b></td></tr></table></td>
			<td width=50% class=tableborder><table><tr><td><b>TOPIC</b></td></tr></table></td>
			<td width=10% class=tableborder><table><tr><td><b>REPLIES</b></td></tr></table></td>
			<td width=20% class=tableborder><table><tr><td><b>DATE</b></td></tr></table></td></tr>
			</table>";
		
		echo '<table width=100% border=1 bordercolor=#336699 cellpadding=0 cellspacing=0 class=tableborder>';
		while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
		{	
									
			$row[0] = '<font size=1>'.$row[0].'</font>';
  			echo "
			<tr><td width=20% class=tableborder align=center><table><tr><td><b><a href=\"\" class=\"under\">".strtolower($row[3])."</a><b></td></tr></table></td>
			<td width=50% class=tableborder><table><tr><td><a href=\"reportview.php?pid={$row[2]}\" class=\"under\">$row[1]</a></td></tr></table></td>
			<td width=10% class=tableborder align=center><table><tr><td>".replies($row[2])."</td></tr></table></td>
			<td width=20% class=tableborder><table><tr><td>$row[0]</td></tr></table></td></tr>";			
		}
		echo '</table>';
	
		mysql_free_result ($result); 	
	} 
	else 
	{ 
		echo '<h3>There are no records to show.</h3>'; 
	}
	
mysql_close();
this is an admin pagination, i hope it helps.. :)