Jump to content

Pagination - What is the variable that I should pass from page to page?


Recommended Posts

Hello,

 

I have gotten some code for pagination, and I have gotten it to work for the first page.  However, when I click on the links to the next pages, the results are blank.

 

So, apparently the problem is that I am not passing the correct variable to these next pages.

 

The problem is that my output that I'm trying to paginate is a little fancy, and it's hard for me to tell what variable to pass from page to page, and whether or not this variable is an array. 

 

My output code is below.  This is all kicked off after a user submits a value for the variable "$find" via an HTML form.  What should I use as a variable to pass from page to page?

 

Thanks in advance,

 

John

 

$result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'")
or die(mysql_error());

if(mysql_num_rows($result)>0){
while($table=mysql_fetch_row($result)){

print "<p class=\"topic\">$table[0]</p>\n";
$r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC LIMIT $offset, $rowsperpage");


print "<table class=\"navbar\">\n";
while($row=mysql_fetch_array($r)){
print "<tr>";

print "<td>".'<a href="http://'.$row['site'].'" class="links2">'.$row['site'].'</a>'."</td>";
print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>";
print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Vote.'</a>'.'</span>'."</td>";
}
print "</tr>\n";
}
print "</table>\n";

effectively you would only need to pass the $rowsperpage variable.

 

each time you move forward a page you would need to add the value of $rowsperpage to $offset before the following query was executed

 

$r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC LIMIT $offset, $rowsperpage");

 

have a look here if you need clarification

http://php.about.com/od/mysqlcommands/g/Limit_sql.htm

 

No

 

Your first query should count the total number of rows in the table. Then from a page number value you can work out the offset. So the only parameter needed is the page number i.e. articles.php?p=1 , articles.php?p=2 , etc

 

i.e. If you want to display 15 records per page

$rowsPerPage = 15;
$numberOfPages = ceil($numberOfRows / $rowsPerPage);

$currentPageNumber = is_numeric($_GET['p']) ? $_GET['p'] : 1;
$offset = $rowsPerPage * ($currentPageNumber - 1));

// add to the sql query
$sqlQuery .= " LIMIT ".$offset.", ".$rowsPerPage;

You could do with a function i.e

<< 1 2 3 4 5 6 7 8 9 10 ... >>

<?php
// display the pagination links
function displayLinks($numberOfPages, $maxPageLinks, $currentPageNumber, $filename) {
$displayLinksString = false;

if($currentPageNumber > 1) {
	$displayLinksString .= '<a href="'.$filename. '?p='.($currentPageNumber - 1)).'">'."<<".'</a>  ';
}

$curWindowNum = intval($currentPageNumber / $maxPageLinks);
if($currentPageNumber % $maxPageLinks) {
	$curWindowNum++;
}

$maxWindowNum = intval($numberOfPages / $maxPageLinks);
if($numberOfPages % $maxPageLinks) {
	$maxWindowNum++;
}

if($curWindowNum > 1) {
	$displayLinksString .= '<a href="'.$filename.'?p='.(($curWindowNum - 1) * $maxPageLinks)).'">...</a>';
}

for($jumpToPage = 1 + (($curWindowNum - 1) * $maxPageLinks); ($jumpToPage <= ($curWindowNum * $maxPageLinks)) && ($jumpToPage <= $numberOfPages); $jumpToPage++) {
	if($jumpToPage == $currentPageNumber) {
		$displayLinksString .= ' <b>'.$jumpToPage.'</b> ';
	} else {
		$displayLinksString .= ' <a href="'.$filename.'?p='.$jumpToPage.'">'.$jumpToPage.'</a> ';
	}
}

if($curWindowNum < $maxWindowNum) {
	$displayLinksString .= '<a href="'.$filename.'?p='.(($curWindowNum) * $maxPageLinks + 1)).'">...</a> ';
}

if(($currentPageNumber < $numberOfPages) && ($numberOfPages != 1)) {
	$displayLinksString .= ' <a href="'.$filename. '?p='.($currentPageNumber + 1)).'">'. ">>".'</a> ';
}

return $displayLinksString;			  
}

$rowsPerPage = 15;
$maxPageLinks = 10;
$numberOfPages = ceil($numberOfRows / $rowsPerPage);
$currentPageNumber = is_numeric($_GET['p']) ? $_GET['p'] : 1;
// display pagination links
print displayLinks($numberOfPages, $maxPageLinks, $currentPageNumber, "articles.php");
?>

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.