<?php
/****************************************************************************
* paging.class.php v 1.0
*
* version 1.0
*
* This script allow to generate dynamically navigation of pages on your web site
* It is easy to configure and use this paging class
*
* ----------------------------------------------------------------
*
* You can find examples in the index.php script (with this package)
*
Copyright (C) 2010 Deyan Spasov <deyan@e7studio.com>
*******************************************************************************/
class paging {
public $showPagesNumber = true; //Show page number Example: Page 1 of 20
public $showPagesForm = true; //If you want to use pages form. The system show form with input field and go button
public $showFirstAndLast = true; //If you have paging buttons first page and last page
public $showPrevAndNext = true; //If you have paging button previous page and next page
public $numberOfPages = 7; //Set the number of shown pages
public $pagingClass = 'paging'; //Set css class of your paging
public $pagingFirstText = '«'; //Set text of first button
public $pagingLastText = '»'; //Set text of last button
public $pagingPrevText = '‹'; //Set text of previous button
public $pagingNextText = '›'; //Set text of next button
public $pagingPageText = 'Page'; //Set page text
public $pagingPageOfText = 'of'; //Set of text
public $pagingFormButtonText = 'Go'; //Set text of form button
/*
Generate standart navigation
example: generate('?page=', '&category=1', 5, 10, 100)
result: « ‹ 1 2 3 4 5 6 7 8 9 10 › »
@access public
@parameters
$frontUrl - Set the url before page number
$backUrl - Set the url after page number
$currentPage - Set the current page number
$rowsPerPage - Set how many rows you show on page
$allRows - Set the number of all rows that you have
@return string with paging HTML source code
*/
public function generate($frontUrl, $backUrl, $currentPage, $rowsPerPage, $allRows) {
if($allRows <= $rowsPerPage) {
return '';
}
$pages = ceil ( $allRows / $rowsPerPage );
settype($currentPage, "int");
if($currentPage < 1 || $currentPage > $pages) {
$currentPage = 1;
}
$paging = '<div class="'.$this->pagingClass.'">';
if ($currentPage > 2 && $this->showFirstAndLast) {
$paging .= '<a href="' . $frontUrl . '1' . $backUrl . '" title="First page" class="arrows">'.$this->pagingFirstText.'</a>';
}
if ($currentPage > 1 && $this->showPrevAndNext) {
$paging .= '<a href="' . $frontUrl . '' . ($currentPage - 1) . '' . $backUrl . '" title="Previous page" class="arrows">'.$this->pagingPrevText.'</a>';
}
$halfPages = $this->numberOfPages / 2;
settype($halfPages, 'int');
if($pages > $this->numberOfPages) {
if($currentPage == 1) {
for($i = 1; $i <= $this->numberOfPages; $i ++)
$paging .= '<a href="' . $frontUrl . '' . $i . '' . $backUrl . '" ' . (($i == $currentPage) ? ' class="selected"' : ' class="normal"') . '>' . $i . '</a>';
} elseif ($currentPage == $pages) {
for($i = $pages - $this->numberOfPages + 1; $i <= $pages; $i ++)
$paging .= '<a href="' . $frontUrl . '' . $i . '' . $backUrl . '" ' . (($i == $currentPage) ? ' class="selected"' : ' class="normal"') . '>' . $i . '</a>';
} else {
$start = $currentPage - $halfPages;
if($start == 0) {
$start = 1;
}
if($start < 1) {
$start_at = 1;
$end = (- 1) * $start + $halfPages + $currentPage;
} else {
$start_at = $start;
$end = $start_at + $this->numberOfPages - 1;
if ($end > $pages) {
$start_at = $start_at - ($end - $pages);
$end = $pages;
}
}
for($i = $start_at; $i <= $end; $i ++)
$paging .= '<a href="' . $frontUrl . '' . $i . '' . $backUrl . '" ' . (($i == $currentPage) ? ' class="selected"' : ' class="normal"') . '>' . $i . '</a>';
}
} else {
for($i = 1; $i <= $pages; $i ++)
$paging .= '<a href="' . $frontUrl . '' . $i . '' . $backUrl . '" ' . (($i == $currentPage) ? ' class="selected"' : ' class="normal"') . '>' . $i . '</a>';
}
if($currentPage < $pages && $this->showPrevAndNext) {
$paging .= '<a href="' . $frontUrl . '' . ($currentPage + 1) . '' . $backUrl . '" title="Next page" class="arrows">'.$this->pagingNextText.'</a>';
}
if($currentPage + 1 < $pages && $this->showFirstAndLast) {
$paging .= '<a href="' . $frontUrl . '' . $pages . '' . $backUrl . '" title="Last page" class="arrows">'.$this->pagingLastText.'</a>';
}
if($this->showPagesForm) {
$paging .= '<form method="post" action="" class="page_form">'.$this->pagingPageText.' <input type="text" name="page" value="" /> <button type="submit" name="go_page">'.$this->pagingFormButtonText.'</button></form>';
}
if($this->showPagesNumber) {
$paging .= '<div class="page_numbers">'.$this->pagingPageText.' '.$currentPage.' '.$this->pagingPageOfText.' '.$pages.'</div>';
}
$paging .= '<div style="clear: both"></div>';
return $paging . "</div>";
}
}
?>
我有这个 php 分页类,我想不出一种方法来限制我的 SQL 查询。这会很好地生成分页按钮,但不知道如何限制我的查询输出。另外,我将如何在我的 url 中使用这个类和其他变量。因为这个不支持它我不认为。像。
mypage.php?status=pending&page=4
这是我的“工作”页面中的内容。
<?php
require_once('classes/class.paging.php');
// PAGINATION STUFF
$ordercount1 = mysql_query("SELECT * from orders WHERE technumber='$myuserid'");
$ordercount2 = mysql_num_rows($ordercount1);
// echo $ordercount2;
$pagingClass = new paging();
$limit = 10; //Number of rows that we show on page
$allRows = $ordercount2; //Number of all rows that we have
if(isset($_POST['page'])) {
$_GET['page'] = $_POST['page'];
}
if(!isset($_GET['page']) || !is_numeric($_GET['page'])) {
$_GET['page'] = 1;
}
//We off the page number
$pagingClass->showPagesNumber = false;
$pagingClass->showPagesForm = false;
$pagingClass->numberOfPages = 10;
echo $pagingClass->generate('?page=', '', $_GET['page'], $limit, $allRows);
// END PAGINATION STUFF
?>