Jump to content

Pagination navigation not showing.


Jacbey

Recommended Posts

Hi guys,

 

I recently wrote a pagination script and it works fine, except from when trying to echo the navigation.

 

This is the Pagination.php Script

 <?php

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 = " WHERE school = 'ssb' ORDER BY orientation 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> ';

	$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>";
}
}

?>

 

And this is the actual page which needs paginating:

 

<?php

									include('Pagination.php');

									$page = 1;

									// how many records per page
									$size = 9;

									// we get the current page from $_GET
									if (isset($_GET['page'])){
									    $page = (int) $_GET['page'];
									}

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

									// now use this SQL statement to get records from your table
									//$SQL = "SELECT * FROM mytable " . $pagination->getLimitSql();


									$myServer = "********";  
									$myUser = "*******";
									$myPass = "*******";
									$myDB = "*******";

									//connection to the database
									$dbhandle = mysql_connect($myServer, $myUser, $myPass)
									  or die("Couldn't connect to SQL Server on $myServer"); 

									//select a database to work with
									$selected = mysql_select_db($myDB, $dbhandle)
									  or die("Couldn't open database $myDB"); 



									$album_query = "SELECT * FROM albums WHERE album_name = '" . $_REQUEST['album'] . "' ";

									$album_result = mysql_query($album_query) or die('Error Getting Information Requested for album');

									$album_row = mysql_fetch_array($album_result);




									$album = $_REQUEST['album'] . " " . $album_row['school'];


									$photo_query = "SELECT * FROM `" . $album . "` " . $pagination->getLimitSql();

									//$photo_query = "SELECT * FROM `" . $album . "` WHERE school = 'ssb' ORDER BY orientation" . $pagination->getLimitSql();


									$photo_result = mysql_query($photo_query) or die('Error Getting Information Requested for photos');

									$photo_row = mysql_fetch_array($photo_result);

									$photo_row_count = mysql_num_rows($photo_result);




									$i = 0;
									$cellcnt = 0;
									while ($i < $photo_row_count) {

									$thumb_selection = $_REQUEST['album'] . " " . $album_row['school'];
									$photo_selection = $_REQUEST['album'] . " " . $album_row['school'];

									if($thumb_selection == "nutcracker 2007 ssb")
										$act_thumb = mysql_result($photo_result,$i,"thumbnail");
									else
										$act_thumb = mysql_result($photo_result,$i,"thumbnail") . "/" . mysql_result($photo_result,$i,"photo_id") . ".jpg";

									if($photo_selection == "nutcracker 2007 ssb")
										$act_photo = mysql_result($photo_result,$i,"photo_path");
									else
										$act_photo = mysql_result($photo_result,$i,"photo_path") . "/" . mysql_result($photo_result,$i,"photo_id") . ".jpg";

									if (!(mysql_result($photo_result,$i,"photo_name") == ""))
										$photo_name_font_tag = "<font style=' padding: 5px; background-color:grey; font-family: Vag Round, Vag Rounded; color: #C02777;'>";
									else
										$photo_name_font_tag = "<font style='font-family: Vag Round, Vag Rounded; color: #C02777;'>";

									$f1 ="
										<td align='center'>
											<a class='grouped_elements' rel='group1' href='" . $act_photo . "'><img src='" . $act_thumb . "' />
											<br />
											" . mysql_result($photo_result,$i,"photo_name") . "</a>
										</td></font>
										";

									echo "" .  $f1 ."";

									$cellcnt++;
									if ($cellcnt == 3) {
										echo "</tr> <tr>";
										$cellcnt = 0;
									}

									$i++;
								}

									?>
								</table>
							</td>
							<td>
								 
							</td>
							<td>
								 
							</td>
						</tr>
						<tr>
							<td>
								 
							</td>
							<td>
								<?php 

								$navigav = $pagination->create_links();
								//$navigav = $pagination -> create_links($output);
								echo $navigav;

								?>

 

Any help would be greatly appreciated!

 

Jacob.

Link to comment
Share on other sites

Font tags? Really?

 

Anyway, have you checked the HTML source to see if there is anything output in the navigation section? Are you getting at least the containing DIV and UL elements? If no, then this condition is probably the reason why

	if ($totalPages < 1 || $totalPages == 1){
		return null;
	}

[Note: why didn't you just use $totalPages <= 1 or $totalPages < 2 ?]

 

If you are getting the conatining DIV and UL elements . . . I see that all of the output created in that method is defined within if() conditions. So, all of those must be resulting in false.

 

In either of the two cases you will want to do some debugging. Echo out the variables used in the if90 conditions just before the if() statement to see what they contain.

Link to comment
Share on other sites

Thanks for the help, but I've tried and tried and it just will not work. It keeps echoing nothing. :(

 

That is why you use debugging. Debugging doesn't fix your problem, it only helps to determine where the problem is. I already gave you advice on how you can add some debugging code. Here is a more illustrative example:

 

Put this right before the first if() statement in your method to display the links

echo "Var totalPages = {$totalPages}<br>\n";
echo "Var currentPage = {$currentPage}<br>\n";
echo "Var loopStart = {$loopStart}<br>\n";
echo "Var loopEnd = {$loopEnd}<br>\n";

 

Those are all the variables that appear to be used in that method for conditional testing. If that doesn't help you to determine the problem, then ut some echo's inside the if() statements so you can determine which ones, if any, are running.

 

For example, change this

		if ($totalPages < 1 || $totalPages == 1){
		return null;
	}

 

		if ($totalPages < 1 || $totalPages == 1){
		echo "[1]<br>\n";
		return null;
	}

 

use a different number in each branch. Then run you rpage and check the debugging info that is displayed.

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.