Jump to content

Can someone help me add pagination? :(


Zoofu

Recommended Posts

<?php
session_start();
include "./globals.php";

?>

<html>

<head>
	<link rel="stylesheet" type="text/css" href="./style.css">

</head>

<body>
<center>
	<div id="holder">
		<div id="userInfo">
			<?php

				if($_SESSION['uid']){
					$sql = "SELECT * FROM `users` WHERE `id`='".$_SESSION['uid']."'";
					$res = mysql_query($sql) or die(mysql_error());

					if(mysql_num_rows($res) == 0){
							session_destroy();
							echo "<div align=\"right\"><a href=\"./login.php\">Login</a> | <a href=\"./register.php\">Register</a></div>\n";
					}else {
						$row = mysql_fetch_assoc($res);
						echo "<div align=\"right\">Logged in as: <a href=\"./index.php?act=profile&id=".$row['id']."\">".$row['username']."</a> | <a href=\"./logout.php\">Logout?</a></div>\n";
						echo "<br>\n";
						if($row['admin'] == '1'){
							echo "<a href=\"./admin.php\">| Admin Panel</a>\n";
						}
					}
				}else {
					echo "<div align=\"right\"><a href=\"./login.php\">Login</a> | <a href=\"./register.php\">Register</a></div>\n";
				}

				echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"85%\">\n";
				echo "<tr><td><center><b><a href=\"./news.php\">| News |</a></b></td></center><td><center><b><a href=\"./catalog.php\">| Catalog |</a></b></td></center><td><center><b><a href=\"./browse.php\">| Browse |</a></b></center></td><td><center><b><a href=\"./index.php\">| Forum |</a></b></center></td></tr>\n";
				echo "</table>\n";

$id = $_GET['id']; 
        $query = "SELECT * FROM users ORDER BY id ASC";
	$result = mysql_query($query);

	echo '
		<center><table class="forum_header" width="80%" border="0" cellpadding="5px">
		  <caption>
		  </caption>
		  <tr>
			<th scope="col">Username</th>
		  </tr>
	';


        while ($row = mysql_fetch_array($result)) {

            echo '<tr>
			  <td class=\"forum_header\"><center><a href="index.php?act=profile&id='.$row['id'].'">'.$row['username'].'</a></center></td>
			  </tr>';

        }

	echo '</table></center>';
			?>
		</div>

	</div>
<?php
if($action == 'browse'){
	include "./browse.php";
}
?>
</center>
</body>

</html>



<?php

$sql10 = "SELECT * FROM `forum_replies`";
$res10 = mysql_query($sql10) or die(mysql_error());
$num1 = mysql_num_rows($res10);

$sql11 = "SELECT * FROM `forum_topics`";
$res11 = mysql_query($sql11) or die(mysql_error());
$num2 = mysql_num_rows($res11);

$sql12 = "SELECT * FROM `users`";
$res12 = mysql_query($sql12) or die(mysql_error());
$num3 = mysql_num_rows($res12);

$total = $num1 + $num2;

echo "<center><table border=\"1\" cellspacing=\"3\" cellpadding=\"3\" width=\"80%\">\n";
echo "<tr align=\"center\"><td class=\"forum_header\">Zoofu contains <b>".$total."</b> posts made by <b>".$num3."</b> users!</a></td></tr>\n";
echo "</table></center>\n";

?>

I keep trying and trying, can someone point me in the right direction of adding pagination to that script?

 

This is my pagination script, well some of it....

 

$id = mss($_GET['id']);
$page = (!$_GET['page'] || $_GET['page'] < 0) ? "1" : $_GET['page']; 
$page = ceil($page);

$limit = 10; 
$start = $limit; 
$end = $page*$limit-($limit); 

 

$pages = ceil($row/$limit); 

		$previous = ($page-1 <= 0) ? "« Prev" : "<a href=\"./index.php?act=topic&id=".$id."&page=".($page-1)."\">« Prev</a>"; 
		$nextpage = ($page+1 > $pages) ? "Next »" : "<a href=\"./index.php?act=topic&id=".$id."&page=".($page+1)."\">Next »</a>"; 
		echo "<tr><td align=\"right\" colspan=\"2\">\n";
		echo "Pages: ";
		echo $previous; 
		for($i=1;$i<=$pages;$i++){ 
		    $href = ($page == $i) ? " ".$i." " : " <a href=\"./index.php?act=topic&id=".$id."&page=".$i."\">".$i."</a> "; 
     
		    echo $href; 
		} 
		echo $nextpage; 

 

If anyone could fix one of the last code... and tell me where to implement it into the 1st code? Thanks. :)

 

Or correct any of my errors? It won't let me change page. and i'm confused.

Link to comment
Share on other sites

Learning Pagination can be intimidating (it was for me anyway). I use to use my own pagination-function but here is the class I use for my projects, it works fairly well.

 

<?php
class pagination {
	var $page = 1; // Current Page
	var $perPage = 10; // Items on each page

	function generate($array, $perPage = 10, $page='page'){
		$this->param = $page;
		// Assign the items per page variable
		if(!empty($perPage)) $this->perPage = $perPage;
		// Assign the page variable
		if(!empty($_GET[$this->param]))
			$this->page = $_GET[$this->param]; //using the get method
		else
			$this->page = 1; // if we don't have a page number then assume we are on the first page
		// Take the length of the array
		$this->length = count($array);
		// Get the number of pages
		$this->pages = ceil($this->length / $this->perPage);
		// Calculate the starting point 
		$this->start  = ceil(($this->page - 1) * $this->perPage);
		// Return the part of the array we have requested
		return array_slice($array, $this->start, $this->perPage);
	}

	function links(){
		$querystr = $_SERVER['QUERY_STRING'];
		if(stristr($querystr, $this->param.'=') === false){
			$querystr .= '&'.$this->param.'=1';
			$this->page = 1;
		}
		if($this->pages > 1){
			$ret = 'Page: ';
			for($i = 1; $i <= $this->pages; ++$i) {
				$ret .= $i == $this->page ? '<b>'.$i.'</b>' : '<a href="?'.str_replace($this->param.'='.$this->page, $this->param.'='.$i, $querystr).'">'.$i .'</a>';
				$ret .= $i != $this->pages ? ', ' : '';
			}
			return $ret;
		}
		return;
	}
}
$pagination = new pagination;
?>

 

The class above is based on a pagination-class found at lotsofcode.com

I slightly modified the original class to put the page= variable at the end of the Query-String as opposed to the beginning. I also made it so you can paginate multiple arrays in the same script. Example, you have Sub-boards and board-topics on the same page which need to paginated. For paginating the sub-boards you could do $paginate->generate($array, 5, 'sub-board_page'). And for paginating the topics, you could do $paginate->generate($array, 5, 'topic_page'). So, your url might look something like this:

http://foo.bar/forums.php?board=1&sub-board_page=2&topic_page=1

 

Look at the post on lotsofcode.com to see how to use this clas. I modified it, but how you implement my modified-class is exactly the same as how you implement the original-class. The only thing you may not like about this class is that it will only generate enumerated links (example - Page: 1, 2, 3, 4, 5, 6, ...) not browsing-links such as 'next page', 'previous page', 'first page', and 'last page'. It shouldn't be too hard to do that yourself, though (if you want the browsing links that badly).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.