Pagination links don't show

Hi,

I am having problems with pagination that I have setup using this tutorial,

http://www.goodphptutorials.com/article/show/simple-php-mysql-pagination/2

Thanks to help in another question, it's now all setup and limits the rows shown to what I specify.
However when I add the pagination links code...

$navigation = $pagination->create_links();
echo $navigation; // will draw our page navigation

Nothing is displayed.

Can anyone see why it's not showing? or if tere's a way I can create my own links?

Thanks,
Paul.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="http://cawsearch.com/css/hoverbox.css" />
<link rel="stylesheet" type="text/css" href="http://cawsearch.com/css/ie_fixes.css" />
<style>
body{ 
	font-family: Arial;
	font-size: 12px;
	color: #000000;
}
.results-header{ 
	background: #F4F4F4 url(http://cawsearch.com/images/site/header-blue.jpg);
	font-family: Arial;
	font-weight: bold;
	font-size: 12px;
	color: #FFFFFF;
	padding: 5px;
	border-right-style: dotted;
	border-right-width: 1px;
	border-bottom-style: dotted;
	border-bottom-width: 1px;
	border-top-style: dotted;
	border-top-width: 1px;
}
.results-header-left{ 
	background: #F4F4F4 url(http://cawsearch.com/images/site/header-blue.jpg);
	font-family: Arial;
	font-weight: bold;
	font-size: 12px;
	color: #FFFFFF;
	padding: 5px;
	border-style: dotted;
	border-width: 1px;
}
.results-show a:active,
.results-show a:visited,
.results-show a:link{
	color: #407CBF;
}
.results-show{ 
	font-family: Arial;
	font-size: 12px;
	color: #000000;
	padding: 5px;
	border-color: #E0DFE3;
	border-right-style: dotted;
	border-right-width: 1px;
	border-bottom-style: dotted;
	border-bottom-width: 1px;
}
.results-show-multi{ 
	font-family: Arial;
	font-size: 12px;
	color: #000000;
	padding: 5px;
	border-color: #E0DFE3;
	border-right-style: dotted;
	border-right-width: 1px;
	border-bottom-style: dotted;
	border-bottom-width: 1px;
	border-left-style: dotted;
	border-left-width: 1px;
}
.results-show-left{ 
	font-family: Arial;
	font-size: 12px;
	color: #000000;
	padding: 5px;
	border-color: #E0DFE3;
	border-left-style: dotted;
	border-left-width: 1px;
	border-bottom-style: dotted;
	border-bottom-width: 1px;
	border-right-style: dotted;
	border-right-width: 1px;
}
.results-footer{ 
	font-family: Arial;
	font-size: 10px;
	color: #000000;
	padding: 5px;
}
.test{ 
	font-family: Arial;
	font-size: 10px;
	color: #000000;
	padding: 5px;
}
.navigation{ 
	font-family: Arial;
	font-size: 12px;
	color: #000000;
}
</style>
</head>
 
<body link="#407CBF" vlink="#407CBF" alink="#407CBF">
 
<center>
<a href="http://cawsearch.com/"><img border="0" src="http://cawsearch.com/images/site/CAWSearch.png"></a>
<br>
<form method="post" action="http://cawsearch.com/svr2008/search.php">
<input type="text" name="search" size=25 maxlength=25>
<select onchange="this.form.action=this.options[this.selectedIndex].value">
<option value="http://cawsearch.com/svr2008/search.php">Smackdown vs RAW 2008</option>
<option value="http://cawsearch.com/dor2/caws.php">Day Of Reckoning 2</option>
</select>
<input type="Submit" name="Submit" value="Search">
</form> 
 
<a href="http://cawsearch.com/games.php">Browse Games</a>  -  <a href="http://cawsearch.com/submit.php">Submit Your Creation</a>  -  <a href="http://forums.smacktalks.org/">Community</a><br>
<br><br> 
 
<table border="0" width="800" cellpadding="0">
	<tr>
		<td width="240" align="right"><img border="0" src="http://cawsearch.com/images/site/svr2008.png"></td>
		<td width="560" align="center" class="navigation">Browse: <a href="caws.php">CAWS</a> | <a href="movesets.php">Movesets</a> | <a href="belts.php">Belts</a> | <a href="entrances.php">Entrances</a> | <a href="attributes.php">Attributes</a> | <a href="#">Submit Your Creation</a><br><br>
		Currently Viewing: CAWS (<?php if (isset($_GET['letter'])) echo $_GET['letter']; else echo "All";?>)<br><br>
		<a href="caws.php">All</a> | <a href="?letter=0">0</a> <a href="?letter=1">1</a> <a href="?letter=2">2</a> <a href="?letter=3">3</a> <a href="?letter=4">4</a> <a href="?letter=5">5</a> <a href="?letter=6">6</a> <a href="?letter=7">7</a> <a href="?letter=8">8</a> <a href="?letter=9">9</a> | <a href="?letter=A">A</a> <a href="?letter=B">B</a> <a href="?letter=C">C</a> <a href="?letter=D">D</a> <a href="?letter=E">E</a> <a href="?letter=F">F</a> <a href="?letter=G">G</a> <a href="?letter=H">H</a> <a href="?letter=I">I</a> <a href="?letter=J">J</a> <a href="?letter=K">K</a> <a href="?letter=L">L</a> <a href="?letter=M">M</a> <a href="?letter=N">N</a> <a href="?letter=O">O</a> <a href="?letter=P">P</a> <a href="?letter=Q">Q</a> <a href="?letter=R">R</a> <a href="?letter=S">S</a> <a href="?letter=T">T</a> <a href="?letter=U">U</a> <a href="?letter=V">V</a> <a href="?letter=W">W</a> <a href="?letter=X">X</a> <a href="?letter=Y">Y</a> <a href="?letter=Z">Z</a></td>
	</tr>
</table><br>
 
<table border="0" cellspacing="0" cellpadding="0" class="test">
	<tr>
		<td class="results-header-left" width="20">&nbsp;</td>
		<td class="results-header" width="180">Name</td>
		<td class="results-header" width="130">Attire</td>
		<td class="results-header" width="130">Creator</td>
		<td class="results-header" width="40">Rating</td>
		<td class="results-header" width="50">Preview</td>
		<td class="results-header" width="50">Attributes</td>
		<td class="results-header" width="50">Entrance</td>
		<td class="results-header" width="50">Movesets</td>
	</tr>
 
<?php
//connect to mysql
 
mysql_connect("localhost","",""); 
mysql_select_db(""); 
 
$letter=mysql_real_escape_string($_GET["letter"]);
 
class Pagination 
{
	/**
	 * Current Page
	 *
	 * @var integer
	 */
	var $page;
	
	/**
	 * Size of the records per page
	 *
	 * @var integer
	 */
	var $size;
	
	/**
	 * Total records
	 *
	 * @var integer
	 */
	var $total_records;
	
	/**
	 * Link used to build navigation
	 *
	 * @var string
	 */
	var $link;
	
	/**
	 * Class Constructor
	 *
	 * @param integer $page
	 * @param integer $size
	 * @param integer $total_records
	 */
	function Pagination($page = null, $size = null, $total_records = null)
	{
		$this->page = $page;
		$this->size = $size;
		$this->total_records = $total_records;
	}
	
	/**
	 * Set's the current page
	 *
	 * @param unknown_type $page
	 */
	function setPage($page)
	{
		$this->page = 0+$page;
	}
	
	/**
	 * Set's the records per page
	 *
	 * @param integer $size
	 */
	function setSize($size)
	{
		$this->size = 0+$size;
	}
		
	/**
	 * Set's total records
	 *
	 * @param integer $total
	 */
	function setTotalRecords($total)
	{
		$this->total_records = 0+$total;
	}
	
	/**
	 * Sets the link url for navigation pages
	 *
	 * @param string $url
	 */
	function setLink($url)
	{
		$this->link = $url;
	}
	
	/**
	 * Returns the LIMIT sql statement
	 *
	 * @return string
	 */
	function getLimitSql()
	{
		$sql = "LIMIT " . $this->getLimit();
		return $sql;
	}
		
	/**
	 * Get the LIMIT statment
	 *
	 * @return string
	 */
	function getLimit()
	{
		if ($this->total_records == 0)
		{
			$lastpage = 0;
		}
		else 
		{
			$lastpage = ceil($this->total_records/$this->size);
		}
		
		$page = $this->page;		
		
		if ($this->page < 1)
		{
			$page = 1;
		} 
		else if ($this->page > $lastpage && $lastpage > 0)
		{
			$page = $lastpage;
		}
		else 
		{
			$page = $this->page;
		}
		
		$sql = ($page - 1) * $this->size . "," . $this->size;
		
		return $sql;
	}
	
	/**
	 * Creates page navigation links
	 *
	 * @return 	string
	 */
	function create_links()
	{
		$totalItems = $this->total_records;
		$perPage = $this->size;
		$currentPage = $this->page;
		$link = $this->link;
		
		$totalPages = floor($totalItems / $perPage);
		$totalPages += ($totalItems % $perPage != 0) ? 1 : 0;
 
		if ($totalPages < 1 || $totalPages == 1){
			return null;
		}
 
		$output = null;
		//$output = '<span id="total_page">Page (' . $currentPage . '/' . $totalPages . ')</span>&nbsp;';
				
		$loopStart = 1; 
		$loopEnd = $totalPages;
 
		if ($totalPages > 5)
		{
			if ($currentPage <= 3)
			{
				$loopStart = 1;
				$loopEnd = 5;
			}
			else if ($currentPage >= $totalPages - 2)
			{
				$loopStart = $totalPages - 4;
				$loopEnd = $totalPages;
			}
			else
			{
				$loopStart = $currentPage - 2;
				$loopEnd = $currentPage + 2;
			}
		}
 
		if ($loopStart != 1){
			$output .= sprintf('<li class="disabledpage"><a href="' . $link . '">&#171;</a></li>', '1');
		}
		
		if ($currentPage > 1){
			$output .= sprintf('<li class="nextpage"><a href="' . $link . '">Previous</a></li>', $currentPage - 1);
		}
		
		for ($i = $loopStart; $i <= $loopEnd; $i++)
		{
			if ($i == $currentPage){
				$output .= '<li class="currentpage">' . $i . '</li> ';
			} else {
				$output .= sprintf('<li><a href="' . $link . '">', $i) . $i . '</a></li> ';
			}
		}
 
		if ($currentPage < $totalPages){
			$output .= sprintf('<li class="nextpage"><a href="' . $link . '">Next</a></li>', $currentPage + 1);
		}
		
		if ($loopEnd != $totalPages){
			$output .= sprintf('<li class="nextpage"><a href="' . $link . '">&#187;</a></li>', $totalPages);
		}
 
		return '<div class="pagination"><ul>' . $output . '</ul></div>';
	}
}
 
 
  
$page = 1;
 
// how many records per page
$size = 3;
 
// we get the current page from $_GET
if (isset($_GET['page'])){
    $page = (int) $_GET['page'];
}
 
// create the pagination class
$pagination = new Pagination();
$pagination->setLink("caws.php?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);
 
// SQL statement to get records from table
$SQL = "SELECT * FROM caws WHERE name LIKE '$letter%' ORDER BY name " . $pagination->getLimitSql();
 
$result = mysql_query($SQL); 
 
$arr_data = array();
 
echo mysql_error(); 
 
while ($row = mysql_fetch_assoc($result))
{
	if (!isset($arr_data[$row["name"]]))
  {
    $arr_data[$row["name"]] = array();
  }   
  $arr_data[$row["name"]][] = $row;
}
 
foreach ( $arr_data as $name => $rows )
{    
     if (sizeof($rows) == 1)
     { // only 1 details row, print all on 1 row
        $row = $rows[0];
        $detail_row = 1;
		$total_value=$row['total_value'];
  $total_votes=$row['total_votes'];
  $rating = ($total_votes>0)?round($total_value / $total_votes,$decimals):0;
        print "
        <tr>
		<td class=results-show-left><img src='http://cawsearch.com/images/icons/{$row["console"]}.png'></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/view_caw.php?idx={$row["id"]}'>{$row["name"]}</a></td>
		<td class=results-show>{$row["attire"]}</td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/creator.php?c={$row["creator"]}'>{$row["creator"]}</a></td>
		<td class=results-show>$rating/10</td>
		<td class=hoverbox><li><a href=#>Roll Over<img src=http://cawsearch.com{$row["image"]} class=preview /></a></li></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/attributes.php?letter={$row["name"]}'>Click Here</a></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/entrances.php?letter={$row["name"]}'>Click Here</a></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/movesets.php?letter={$row["name"]}'>Click Here</a></td>
	</tr>";
     }
     else // if (sizeof($rows) == 1)
     {
       // print the name's row
       print "
         <tr>
				<td class=results-show-left>&nbsp;</td>
                 <td colspan=9 class=results-show-multi>
				   <a href='#' onclick=\"document.getElementById('row{$row["id"]}').style.display=='none' ? document.getElementById('row{$row["id"]}').style.display='' : document.getElementById('row{$row["id"]}').style.display = 'none';return false\">
				     <b>{$name}</b> <i>- (Multiple Results)</i>
				   </a>
				 </td>
         </tr>
         ";
       print "
	   <tr><td colspan=9><div id='row{$row["id"]}' style='display:none;'><table border='0' cellspacing='0' cellpadding='0' class='test'>
		<tr>
		<td class='results-header-left' width='20'>&nbsp;</td>
		<td class='results-header' width='180'>Name</td>
		<td class='results-header' width='130'>Attire</td>
		<td class='results-header' width='130'>Creator</td>
		<td class='results-header' width='40'>Rating</td>
		<td class='results-header' width='50'>Preview</td>
		<td class='results-header' width='50'>Attributes</td>
		<td class='results-header' width='50'>Entrance</td>
		<td class='results-header' width='50'>Movesets</td>
		</tr>";
       $detail_row = 0;
		foreach ( $rows as $row )
       {
		$detail_row++;
		$total_value2=$row['total_value'];
		  $total_votes2=$row['total_votes'];
		  $rating2 = ($total_votes2>0)?round($total_value2 / $total_votes2,$decimals):0;
        print "
        <tr>
		<td class=results-show-left><img src='http://cawsearch.com/images/icons/{$row["console"]}.png'></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/view_caw.php?idx={$row["id"]}'>{$row["name"]}</a> ($detail_row)</td>
		<td class=results-show>{$row["attire"]}</td>
		<td class=results-show><a href='http://http://cawsearch.com/svr2008/creator.php?c={$row["creator"]}'>{$row["creator"]}</a></td>
		<td class=results-show>$rating2/10</td>
		<td class=hoverbox><li><a href=#>Roll Over<img src=http://cawsearch.com{$row["image"]} class=preview /></a></li></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/attributes.php?letter={$row["name"]}'>Click Here</a></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/entrances.php?letter={$row["name"]}'>Click Here</a></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/movesets.php?letter={$row["name"]}'>Click Here</a></td>
	</tr>";
 
       } // foreach ( $rows as $row )
      print "
	  </table></div></td></tr>
	  ";
    } // else if (sizeof($rows) == 1)
} //  foreach ( $arr_data as $name => $rows )
 
      print "<tr>
      <td colspan='9' align='center' height='35' valign='middle'>
            <?php
 
			$navigation = $pagination->create_links();
			echo $navigation;
			
			?>
      </td>
</tr>";
 
?>
</table>
 
<br><br><font size="1">CAWSearch 1.0.1</font>
</center>
 
</body>
</html>

Open in new window

m0tSiEAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beverley PortlockCommented:
I suspect that the reason it is not showing anything is because you seem to be telling it that there is nothing to show. You have some code that says


$page = 1;
 
// how many records per page
$size = 3;
 
// we get the current page from $_GET
if (isset($_GET['page'])){
    $page = (int) $_GET['page'];
}
 
// create the pagination class
$pagination = new Pagination();
$pagination->setLink("caws.php?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);


I can see where you set up $page and $size, but there is no sign of $total_records being initialised. PHP will thus assume it is zero.....

Try amending it like this and see if a pagination pops up. If so, then total_records is your problem, else, post back here and let us know


// create the pagination class
$pagination = new Pagination();
$pagination->setLink("caws.php?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);

$total_records = 60;  // temporary for testing

$pagination->setTotalRecords($total_records);
 
 
0
m0tSiEAuthor Commented:
Thanks,

I've just updated the code but there's still no pagination appearing.

Any other ideas of what could be causing it?
0
hieloCommented:
In my opinion, having null as the "default" value where an integer is expected seems ilogical. I would change the constructor from this:
function Pagination($page = null, $size = null, $total_records = null)

to something like this:
function Pagination($page = 1, $size = 1, $total_records = 1)

although the values do NOT have to be 1. You could use zeroes as well. The problem is in fact due to the unitialized $total_records statement:
$pagination->setTotalRecords($total_records);

You can either initialize $total_records, or just supply a value directly:
$pagination->setTotalRecords(10);
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

m0tSiEAuthor Commented:
Thanks hielo, I've updated the code but there's still nothing showing.

http://cawsearch.com/svr2008/caws.php

That's the page it's on if the source code is of any use and here's the code updated with your suggestions.

Thanks.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="http://cawsearch.com/css/hoverbox.css" />
<link rel="stylesheet" type="text/css" href="http://cawsearch.com/css/ie_fixes.css" />
<style>
body{ 
	font-family: Arial;
	font-size: 12px;
	color: #000000;
}
.results-header{ 
	background: #F4F4F4 url(http://cawsearch.com/images/site/header-blue.jpg);
	font-family: Arial;
	font-weight: bold;
	font-size: 12px;
	color: #FFFFFF;
	padding: 5px;
	border-right-style: dotted;
	border-right-width: 1px;
	border-bottom-style: dotted;
	border-bottom-width: 1px;
	border-top-style: dotted;
	border-top-width: 1px;
}
.results-header-left{ 
	background: #F4F4F4 url(http://cawsearch.com/images/site/header-blue.jpg);
	font-family: Arial;
	font-weight: bold;
	font-size: 12px;
	color: #FFFFFF;
	padding: 5px;
	border-style: dotted;
	border-width: 1px;
}
.results-show a:active,
.results-show a:visited,
.results-show a:link{
	color: #407CBF;
}
.results-show{ 
	font-family: Arial;
	font-size: 12px;
	color: #000000;
	padding: 5px;
	border-color: #E0DFE3;
	border-right-style: dotted;
	border-right-width: 1px;
	border-bottom-style: dotted;
	border-bottom-width: 1px;
}
.results-show-multi{ 
	font-family: Arial;
	font-size: 12px;
	color: #000000;
	padding: 5px;
	border-color: #E0DFE3;
	border-right-style: dotted;
	border-right-width: 1px;
	border-bottom-style: dotted;
	border-bottom-width: 1px;
	border-left-style: dotted;
	border-left-width: 1px;
}
.results-show-left{ 
	font-family: Arial;
	font-size: 12px;
	color: #000000;
	padding: 5px;
	border-color: #E0DFE3;
	border-left-style: dotted;
	border-left-width: 1px;
	border-bottom-style: dotted;
	border-bottom-width: 1px;
	border-right-style: dotted;
	border-right-width: 1px;
}
.results-footer{ 
	font-family: Arial;
	font-size: 10px;
	color: #000000;
	padding: 5px;
}
.test{ 
	font-family: Arial;
	font-size: 10px;
	color: #000000;
	padding: 5px;
}
.navigation{ 
	font-family: Arial;
	font-size: 12px;
	color: #000000;
}
</style>
</head>
 
<body link="#407CBF" vlink="#407CBF" alink="#407CBF">
 
<center>
<a href="http://cawsearch.com/"><img border="0" src="http://cawsearch.com/images/site/CAWSearch.png"></a>
<br>
<form method="post" action="http://cawsearch.com/svr2008/search.php">
<input type="text" name="search" size=25 maxlength=25>
<select onchange="this.form.action=this.options[this.selectedIndex].value">
<option value="http://cawsearch.com/svr2008/search.php">Smackdown vs RAW 2008</option>
<option value="http://cawsearch.com/dor2/caws.php">Day Of Reckoning 2</option>
</select>
<input type="Submit" name="Submit" value="Search">
</form> 
 
<a href="http://cawsearch.com/games.php">Browse Games</a>  -  <a href="http://cawsearch.com/submit.php">Submit Your Creation</a>  -  <a href="http://forums.smacktalks.org/">Community</a><br>
<br><br> 
 
<table border="0" width="800" cellpadding="0">
	<tr>
		<td width="240" align="right"><img border="0" src="http://cawsearch.com/images/site/svr2008.png"></td>
		<td width="560" align="center" class="navigation">Browse: <a href="caws.php">CAWS</a> | <a href="movesets.php">Movesets</a> | <a href="belts.php">Belts</a> | <a href="entrances.php">Entrances</a> | <a href="attributes.php">Attributes</a> | <a href="#">Submit Your Creation</a><br><br>
		Currently Viewing: CAWS (<?php if (isset($_GET['letter'])) echo $_GET['letter']; else echo "All";?>)<br><br>
		<a href="caws.php">All</a> | <a href="?letter=0">0</a> <a href="?letter=1">1</a> <a href="?letter=2">2</a> <a href="?letter=3">3</a> <a href="?letter=4">4</a> <a href="?letter=5">5</a> <a href="?letter=6">6</a> <a href="?letter=7">7</a> <a href="?letter=8">8</a> <a href="?letter=9">9</a> | <a href="?letter=A">A</a> <a href="?letter=B">B</a> <a href="?letter=C">C</a> <a href="?letter=D">D</a> <a href="?letter=E">E</a> <a href="?letter=F">F</a> <a href="?letter=G">G</a> <a href="?letter=H">H</a> <a href="?letter=I">I</a> <a href="?letter=J">J</a> <a href="?letter=K">K</a> <a href="?letter=L">L</a> <a href="?letter=M">M</a> <a href="?letter=N">N</a> <a href="?letter=O">O</a> <a href="?letter=P">P</a> <a href="?letter=Q">Q</a> <a href="?letter=R">R</a> <a href="?letter=S">S</a> <a href="?letter=T">T</a> <a href="?letter=U">U</a> <a href="?letter=V">V</a> <a href="?letter=W">W</a> <a href="?letter=X">X</a> <a href="?letter=Y">Y</a> <a href="?letter=Z">Z</a></td>
	</tr>
</table><br>
 
<table border="0" cellspacing="0" cellpadding="0" class="test">
	<tr>
		<td class="results-header-left" width="20">&nbsp;</td>
		<td class="results-header" width="180">Name</td>
		<td class="results-header" width="130">Attire</td>
		<td class="results-header" width="130">Creator</td>
		<td class="results-header" width="40">Rating</td>
		<td class="results-header" width="50">Preview</td>
		<td class="results-header" width="50">Attributes</td>
		<td class="results-header" width="50">Entrance</td>
		<td class="results-header" width="50">Movesets</td>
	</tr>
 
<?php
//connect to mysql
 
mysql_connect("localhost","",""); 
mysql_select_db(""); 
 
$letter=mysql_real_escape_string($_GET["letter"]);
 
class Pagination 
{
	/**
	 * Current Page
	 *
	 * @var integer
	 */
	var $page;
	
	/**
	 * Size of the records per page
	 *
	 * @var integer
	 */
	var $size;
	
	/**
	 * Total records
	 *
	 * @var integer
	 */
	var $total_records;
	
	/**
	 * Link used to build navigation
	 *
	 * @var string
	 */
	var $link;
	
	/**
	 * Class Constructor
	 *
	 * @param integer $page
	 * @param integer $size
	 * @param integer $total_records
	 */
	function Pagination($page = 1, $size = 1, $total_records = 1)
	{
		$this->page = $page;
		$this->size = $size;
		$this->total_records = $total_records;
	}
	
	/**
	 * Set's the current page
	 *
	 * @param unknown_type $page
	 */
	function setPage($page)
	{
		$this->page = 0+$page;
	}
	
	/**
	 * Set's the records per page
	 *
	 * @param integer $size
	 */
	function setSize($size)
	{
		$this->size = 0+$size;
	}
		
	/**
	 * Set's total records
	 *
	 * @param integer $total
	 */
	function setTotalRecords($total)
	{
		$this->total_records = 0+$total;
	}
	
	/**
	 * Sets the link url for navigation pages
	 *
	 * @param string $url
	 */
	function setLink($url)
	{
		$this->link = $url;
	}
	
	/**
	 * Returns the LIMIT sql statement
	 *
	 * @return string
	 */
	function getLimitSql()
	{
		$sql = "LIMIT " . $this->getLimit();
		return $sql;
	}
		
	/**
	 * Get the LIMIT statment
	 *
	 * @return string
	 */
	function getLimit()
	{
		if ($this->total_records == 0)
		{
			$lastpage = 0;
		}
		else 
		{
			$lastpage = ceil($this->total_records/$this->size);
		}
		
		$page = $this->page;		
		
		if ($this->page < 1)
		{
			$page = 1;
		} 
		else if ($this->page > $lastpage && $lastpage > 0)
		{
			$page = $lastpage;
		}
		else 
		{
			$page = $this->page;
		}
		
		$sql = ($page - 1) * $this->size . "," . $this->size;
		
		return $sql;
	}
	
	/**
	 * Creates page navigation links
	 *
	 * @return 	string
	 */
	function create_links()
	{
		$totalItems = $this->total_records;
		$perPage = $this->size;
		$currentPage = $this->page;
		$link = $this->link;
		
		$totalPages = floor($totalItems / $perPage);
		$totalPages += ($totalItems % $perPage != 0) ? 1 : 0;
 
		if ($totalPages < 1 || $totalPages == 1){
			return null;
		}
 
		$output = null;
		//$output = '<span id="total_page">Page (' . $currentPage . '/' . $totalPages . ')</span>&nbsp;';
				
		$loopStart = 1; 
		$loopEnd = $totalPages;
 
		if ($totalPages > 5)
		{
			if ($currentPage <= 3)
			{
				$loopStart = 1;
				$loopEnd = 5;
			}
			else if ($currentPage >= $totalPages - 2)
			{
				$loopStart = $totalPages - 4;
				$loopEnd = $totalPages;
			}
			else
			{
				$loopStart = $currentPage - 2;
				$loopEnd = $currentPage + 2;
			}
		}
 
		if ($loopStart != 1){
			$output .= sprintf('<li class="disabledpage"><a href="' . $link . '">&#171;</a></li>', '1');
		}
		
		if ($currentPage > 1){
			$output .= sprintf('<li class="nextpage"><a href="' . $link . '">Previous</a></li>', $currentPage - 1);
		}
		
		for ($i = $loopStart; $i <= $loopEnd; $i++)
		{
			if ($i == $currentPage){
				$output .= '<li class="currentpage">' . $i . '</li> ';
			} else {
				$output .= sprintf('<li><a href="' . $link . '">', $i) . $i . '</a></li> ';
			}
		}
 
		if ($currentPage < $totalPages){
			$output .= sprintf('<li class="nextpage"><a href="' . $link . '">Next</a></li>', $currentPage + 1);
		}
		
		if ($loopEnd != $totalPages){
			$output .= sprintf('<li class="nextpage"><a href="' . $link . '">&#187;</a></li>', $totalPages);
		}
 
		return '<div class="pagination"><ul>' . $output . '</ul></div>';
	}
}
 
 
  
$page = 1;
 
// how many records per page
$size = 1;
 
// we get the current page from $_GET
if (isset($_GET['page'])){
    $page = (int) $_GET['page'];
}
 
// create the pagination class
$pagination = new Pagination();
$pagination->setLink("caws.php?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
 
$total_records = 60;  // temporary for testing
 
$pagination->setTotalRecords(10);
 
// SQL statement to get records from table
$SQL = "SELECT * FROM caws WHERE name LIKE '$letter%' ORDER BY name " . $pagination->getLimitSql();
 
$result = mysql_query($SQL); 
 
$arr_data = array();
 
echo mysql_error(); 
 
while ($row = mysql_fetch_assoc($result))
{
	if (!isset($arr_data[$row["name"]]))
  {
    $arr_data[$row["name"]] = array();
  }   
  $arr_data[$row["name"]][] = $row;
}
 
foreach ( $arr_data as $name => $rows )
{    
     if (sizeof($rows) == 1)
     { // only 1 details row, print all on 1 row
        $row = $rows[0];
        $detail_row = 1;
		$total_value=$row['total_value'];
  $total_votes=$row['total_votes'];
  $rating = ($total_votes>0)?round($total_value / $total_votes,$decimals):0;
        print "
        <tr>
		<td class=results-show-left><img src='http://cawsearch.com/images/icons/{$row["console"]}.png'></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/view_caw.php?idx={$row["id"]}'>{$row["name"]}</a></td>
		<td class=results-show>{$row["attire"]}</td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/creator.php?c={$row["creator"]}'>{$row["creator"]}</a></td>
		<td class=results-show>$rating/10</td>
		<td class=hoverbox><li><a href=#>Roll Over<img src=http://cawsearch.com{$row["image"]} class=preview /></a></li></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/attributes.php?letter={$row["name"]}'>Click Here</a></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/entrances.php?letter={$row["name"]}'>Click Here</a></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/movesets.php?letter={$row["name"]}'>Click Here</a></td>
	</tr>";
     }
     else // if (sizeof($rows) == 1)
     {
       // print the name's row
       print "
         <tr>
				<td class=results-show-left>&nbsp;</td>
                 <td colspan=9 class=results-show-multi>
				   <a href='#' onclick=\"document.getElementById('row{$row["id"]}').style.display=='none' ? document.getElementById('row{$row["id"]}').style.display='' : document.getElementById('row{$row["id"]}').style.display = 'none';return false\">
				     <b>{$name}</b> <i>- (Multiple Results)</i>
				   </a>
				 </td>
         </tr>
         ";
       print "
	   <tr><td colspan=9><div id='row{$row["id"]}' style='display:none;'><table border='0' cellspacing='0' cellpadding='0' class='test'>
		<tr>
		<td class='results-header-left' width='20'>&nbsp;</td>
		<td class='results-header' width='180'>Name</td>
		<td class='results-header' width='130'>Attire</td>
		<td class='results-header' width='130'>Creator</td>
		<td class='results-header' width='40'>Rating</td>
		<td class='results-header' width='50'>Preview</td>
		<td class='results-header' width='50'>Attributes</td>
		<td class='results-header' width='50'>Entrance</td>
		<td class='results-header' width='50'>Movesets</td>
		</tr>";
       $detail_row = 0;
		foreach ( $rows as $row )
       {
		$detail_row++;
		$total_value2=$row['total_value'];
		  $total_votes2=$row['total_votes'];
		  $rating2 = ($total_votes2>0)?round($total_value2 / $total_votes2,$decimals):0;
        print "
        <tr>
		<td class=results-show-left><img src='http://cawsearch.com/images/icons/{$row["console"]}.png'></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/view_caw.php?idx={$row["id"]}'>{$row["name"]}</a> ($detail_row)</td>
		<td class=results-show>{$row["attire"]}</td>
		<td class=results-show><a href='http://http://cawsearch.com/svr2008/creator.php?c={$row["creator"]}'>{$row["creator"]}</a></td>
		<td class=results-show>$rating2/10</td>
		<td class=hoverbox><li><a href=#>Roll Over<img src=http://cawsearch.com{$row["image"]} class=preview /></a></li></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/attributes.php?letter={$row["name"]}'>Click Here</a></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/entrances.php?letter={$row["name"]}'>Click Here</a></td>
		<td class=results-show><a href='http://cawsearch.com/svr2008/movesets.php?letter={$row["name"]}'>Click Here</a></td>
	</tr>";
 
       } // foreach ( $rows as $row )
      print "
	  </table></div></td></tr>
	  ";
    } // else if (sizeof($rows) == 1)
} //  foreach ( $arr_data as $name => $rows )
 
      print "<tr>
      <td colspan='9' align='center' height='35' valign='middle'>
            <? $navigation = $pagination->create_links();echo $navigation; // will draw our page navigation ?>
      </td>
</tr>";
 
?>
</table>
 
<br><br><font size="1">CAWSearch 1.0.1</font>
</center>
 
</body>
</html>

Open in new window

0
Beverley PortlockCommented:
Change this section from


      print "<tr>
      <td colspan='9' align='center' height='35' valign='middle'>
            <?php
 
                  $navigation = $pagination->create_links();
                  echo $navigation;
                  
                  ?>
      </td>



to




      $navigation = $pagination->create_links();
      print "<tr>
      <td colspan='9' align='center' height='35' valign='middle'>
          $navigation;
               
               
      </td>
0
m0tSiEAuthor Commented:
Thanks, It's now showing it.

If I remove the test part  $total_records = 60;  // temporary for testing  it then dissapears again though.

Can someone show me how to initialise the $total_records?, I tried...

$totalq = "SELECT COUNT(*) FROM caws WHERE name LIKE '$letter%' ORDER BY name ";
$total_records = $totalq;

but it didn't work.

0
hieloCommented:
$total_records is most likely being used outside the class. Try declaring first and then use it to call setTotal Records:

...
$page = 1;
 
// how many records per page
$size = 3;
 
 
$total_records = 10
 
 
// we get the current page from $_GET
if (isset($_GET['page'])){
    $page = (int) $_GET['page'];
}
 
// create the pagination class
$pagination = new Pagination();
$pagination->setLink("caws.php?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);
...

Open in new window

0
m0tSiEAuthor Commented:
It works if I use

$total_records = 10

but if there's more than 10 records, it's only going to show the first 10.

How can I get it to check how many records there is?
0
hieloCommented:
Try updating your code to the following:
$page = 1;
$size = 3;
$total_records = 0;
0
m0tSiEAuthor Commented:
With $total_records = 0; it shows no pagination.

At the minute i've set it to show 2 results per page, my database has only 4 results at the minute so there should be 2 pages to show the 4 records (2 on each).

If I set $total_records to 4, then it works fine, but obviously when I add more results to the database i'll have the same problem again.

Is there a way I can repeat my search query as a count and set $total_records to be the amount of returned rows from this query?
0
hieloCommented:
Then don't specify a limit:
change this:
$SQL = "SELECT * FROM caws WHERE name LIKE '$letter%' ORDER BY name " . $pagination->getLimitSql();

to this:
$SQL = "SELECT * FROM caws WHERE name LIKE '$letter%' ORDER BY name" ;
0
m0tSiEAuthor Commented:
Thanks hielo, i've taken that out but instead of showing the two results per page and pagination, it now just shows all 4 records with no pagination.

Any ideas?
0
hieloCommented:
Try:
$page = 1;
$size = 2;

$r =  mysql_query( "SELECT COUNT(*) as total FROM caws WHERE name LIKE '$letter%' ORDER BY name " );

$total_records = mysql_fetch_assoc($r);
$total_records = $total_records["total"];

// we get the current page from $_GET
if (isset($_GET['page'])){
    $page = (int) $_GET['page'];
}
 
// create the pagination class
$pagination = new Pagination();
$pagination->setLink("caws.php?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);

$SQL = "SELECT * FROM caws WHERE name LIKE '$letter%' ORDER BY name " . $pagination->getLimitSql();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
m0tSiEAuthor Commented:
Nice one hielo!

It's working great now, thanks! :D
0
m0tSiEAuthor Commented:
Thanks a lot for your help and patience! :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.