PHP Programming > Code Snippets
How does this pagination script work?
(1/1)
Verandaguy:
I found this database-free pagination script on another forum the other day; unfortunately, the thread was old, and the author didn't provide instructions on how to use it. My questions are: 1. What defines an entry, and 2. what defines how many entries one can insert before a second page starts?
Here's the code:
--- PHP Code: ---<?php
class display {
function pagination($rows, $per_page, $current_page, $page_link) {
global $core,$C;
// Create a Page Listing
$this->pages = ceil(10 * $rows / $per_page);
// If there's only one page, return now and don't bother
if($this->pages == 1) {
return;
}
// Pagination Prefix
$output .= "<!-- Pagination by Dennis Pedrie. Used by Permission -->";
$output = "Pages: ";
// Should we show the FIRST PAGE link?
if($current_page > 2) {
$output .= "<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>";
}
// Should we show the PREVIOUS PAGE link?
if($current_page > 1) {
$previous_page = $current_page - 1;
$output .= " <a href=\"". $page_link .">page=". $previous_page ."/\" title=\"Previous Page\"><</a>";
}
// Current Page Number
$output .= "<strong>[ ". $current_page ." ]</strong>";
// Should we show the NEXT PAGE link?
if($current_page < $this->pages) {
$next_page = $current_page + 1;
$output .= "<a href=\"". $page_link ."?page=". $next_page ."/\" title=\"Next Page\">></a>";
}
// Should we show the LAST PAGE link?
if($current_page < $this->pages - 1) {
$output .= " <a href=\"". $page_link ."?page=". $this->pages ."/\" title=\"Last Page\">>></a>";
}
// Return the output.
return $output;
}
}
$display = new display;
echo $display->pagination("45", "15", "1", "http://theapplenewsreel.com/news/index.php");
?>
--- End code ---
Nuker_Viper:
if your site has a functions page that is a separate file like functions.php. IF so here is an easier:
this is the function:
--- PHP Code: ---function countLogs() {
global $dbprefix;
$query = "SELECT COUNT(id) FROM {$dbprefix}logs";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
@extract($row);
return $row[0];
}
--- End code ---
here is the php file:
--- PHP Code: ---<?php
echo "
<table align='center' border='0' cellspacing='0' cellpadding='3' width='500'>
<tr>
<td style='border: solid $bordercolor 1px' background='themes/$theme/$bgimage' class='titles' align='center'><b>$dispconsolename</b></td>
</tr>
<tr>
<td style='border: solid $bordercolor 1px; border-top-width: 0px' align='center' class='main'><br>
<blockquote>
<table align='center' border='0' cellspacing='0' cellpadding='0' width='450'>
";
if($start == "" OR !is_numeric($start)) { $start = 0; }
$totallogs = countLogs();
$totalpages = round($totallogs/10);
$loglinks = "";
if($totalpages > 1) {
for($i=0; $i<$totalpages; $i++) {
$startnum = $totallogs+(($i*10)-$totallogs);
//echo "$i*10 <br>";
$pagenum = $i+1;
$loglinks .= "<a href='console.php?pid=$pid&start=$startnum'>Page $pagenum</a>, ";
}
}
$query = "SELECT * FROM {$dbprefix}logs ORDER BY id DESC LIMIT $start,10 ";
$result = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
extract($row);
$datelogged = date("n/j/Y g:i A", $datelogged);
$action = stripslashes(textFilter($action));
echo "
<tr><td class='main'><b>Date:</b> $datelogged</td></tr>
<tr><td class='main'><b>Action:</b> $action</td></tr>
<tr><td><hr size='1' color='$bordercolor' width='450'><br></td></tr>
";
}
echo "
</table>
$loglinks
</blockquote>
</td>
</tr>
</table>
";
}
?>
--- End code ---
The code:
--- PHP Code: ---$startnum = $totallogs+(($i*10)-$totallogs);
//echo "$i*10 <br>";
--- End code ---
change the number 10 to whatever the number of entries you want to display.
of course you will have to change the varibles and table info.
Navigation
[0] Message Index
Go to full version