我在网上找到了这个很棒的代码来帮助分页,而且运行良好,唯一的问题是:每个页面显示相同的4行。
任何想法都将不胜感激
<?php
//Include the PS_Pagination class
include('includes/ps_pagination.php');
//Connect to mysql db
$conn = mysql_connect("localhost", "root", "root");
mysql_select_db('database',$conn);
$sql = "SELECT * FROM studies WHERE niche = '{$_GET['niche']}'";
//Create a PS_Pagination object
$pager = new PS_Pagination($conn,$sql,4,5);
//The paginate() function returns a mysql
//result set for the current page
$rs = $pager->paginate();
//Loop through the result set
while($row = mysql_fetch_assoc($rs)) {
$a=0;
while ($a < $num) {
$id=mysql_result($result,$a,"id");
$title=mysql_result($result,$a,"title");
$strategies=mysql_result($result,$a,"strategies");
$client=mysql_result($result,$a,"client");
$copy=mysql_result($result,$a,"copy");
$thumbmedia=mysql_result($result,$a,"thumbmedia");
$niche=mysql_result($result,$a,"niche");
echo '<div class="container"><p class="subheadred"><a href="casestudy.php?id='.$id.'">'.$title.'</a></p></div>';
echo '<div class="containerstudy"><div class="column1"><p class="subheadsmall">Strategies</p><p class="sidebarred">'.$strategies.'</p>';
echo '<p class="subheadsmall">Client</p><p class="sidebargrey">'.$client.'</p></div>';
echo '<div class="column2"><p class="bodygrey">'.substr($copy, 0, 300).'<a href="casestudy.php?id='.$id.'">...more</a></p></div>';
echo '<div class="column3"><img src="images/'.$thumbmedia.'" height="160" /></div></div>';
$a++;
}
}
//Display the navigation
echo $pager->renderNav();
?>
这是包含的文件:
<?php
/**
* PHPSense Pagination Class
*
* PHP tutorials and scripts
*
* @package PHPSense
* @author Jatinder Singh Thind
* @copyright Copyright (c) 2006, Jatinder Singh Thind
* @link http://www.phpsense.com
*/
// ------------------------------------------------------------------------
class PS_Pagination {
var $php_self;
var $rows_per_page; //Number of records to display per page
var $total_rows; //Total number of rows returned by the query
var $links_per_page; //Number of links to display per page
var $sql;
var $debug = false;
var $conn;
var $page;
var $max_pages;
var $offset;
/**
* Constructor
*
* @param resource $connection Mysql connection link
* @param string $sql SQL query to paginate. Example : SELECT * FROM users
* @param integer $rows_per_page Number of records to display per page. Defaults to 10
* @param integer $links_per_page Number of links to display per page. Defaults to 5
*/
function PS_Pagination($connection, $sql, $rows_per_page = 1, $links_per_page = 5) {
$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_page = $rows_per_page;
$this->links_per_page = $links_per_page;
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);
if(isset($_GET['page'])) {
$this->page = intval($_GET['page']);
}
}
/**
* Executes the SQL query and initializes internal variables
*
* @access public
* @return resource
*/
function paginate() {
if(!$this->conn) {
if($this->debug) echo "MySQL connection missing<br />";
return false;
}
$all_rs = @mysql_query($this->sql);
if(!$all_rs) {
if($this->debug) echo "SQL query failed. Check your query.<br />";
return false;
}
$this->total_rows = mysql_num_rows($all_rs);
@mysql_close($all_rs);
$this->max_pages = ceil($this->total_rows/$this->rows_per_page);
//Check the page value just in case someone is trying to input an aribitrary value
if($this->page > $this->max_pages || $this->page <= 0) {
$this->page = 1;
}
//Calculate Offset
$this->offset = $this->rows_per_page * ($this->page-1);
//Fetch the required result set
$rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}");
if(!$rs) {
if($this->debug) echo "Pagination query failed. Check your query.<br />";
return false;
}
return $rs;
}
/**
* Display the link to the first page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'First'
* @return string
*/
function renderFirst($tag='First') {
if($this->page == 1) {
return $tag;
}
else {
return '<a href="'.$this->php_self.'?page=1">'.$tag.'</a>';
}
}
/**
* Display the link to the last page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'Last'
* @return string
*/
function renderLast($tag='Last') {
if($this->page == $this->max_pages) {
return $tag;
}
else {
return '<a href="'.$this->php_self.'?page='.$this->max_pages.'">'.$tag.'</a>';
}
}
/**
* Display the next link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '>>'
* @return string
*/
function renderNext($tag=' >>') {
if($this->page < $this->max_pages) {
return '<a href="'.$this->php_self.'?page='.($this->page+1).'">'.$tag.'</a>';
}
else {
return $tag;
}
}
/**
* Display the previous link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '<<'
* @return string
*/
function renderPrev($tag='<<') {
if($this->page > 1) {
return '<a href="'.$this->php_self.'?page='.($this->page-1).'">'.$tag.'</a>';
}
else {
return $tag;
}
}
/**
* Display the page links
*
* @access public
* @return string
*/
function renderNav() {
for($i=1;$i<=$this->max_pages;$i+=$this->links_per_page) {
if($this->page >= $i) {
$start = $i;
}
}
if($this->max_pages > $this->links_per_page) {
$end = $start+$this->links_per_page;
if($end > $this->max_pages) $end = $this->max_pages+1;
}
else {
$end = $this->max_pages;
}
$links = '';
$niche = $_GET['niche'];
for( $i=$start ; $i<$end ; $i++) {
if($i == $this->page) {
$links .= " $i ";
}
else {
$links .= ' <a href="'.$this->php_self.'?page='.$i.'&niche='.$niche.'">'.$i.'</a> ';
}
}
return $links;
}
/**
* Display full pagination navigation
*
* @access public
* @return string
*/
function renderFullNav() {
return $this->renderFirst().' '.$this->renderPrev().' '.$this->renderNav().' '.$this->renderNext().' '.$this->renderLast();
}
/**
* Set debug mode
*
* @access public
* @param bool $debug Set to TRUE to enable debug messages
* @return void
*/
function setDebug($debug) {
$this->debug = $debug;
}
}
?>
答案 0 :(得分:1)
乍一看,我发现你的查询每次运行时都会从头开始重复,这意味着每次运行它都会从开始搜索。你需要一个计数器告诉它从下一页开始。类似的东西:
$sql = "SELECT * FROM studies WHERE niche = '{$_GET['niche']}' limit $startPoint, $offset";
这样,偏移量与每页所需的项目数相同,而$ startPoint是下次迭代时搜索开始的点。
这样的事情会起作用:if(!$startPoint){$startPoint = 0;}else{$startPoint = {$_GET['$startPoint'];}
代码中的将起点传递回查询,并在每次迭代后通过偏移量增加它。
请记住,我没有考虑注入之类的东西,以及变量$ num似乎不是来自第一个文件中的任何地方。我无法看到你在哪里初始化它以便测试$ a。
在更深入地查看您的代码之后,我发现需要更改一些内容以使其工作至少在这里是更改的代码:
<?php
//Include the PS_Pagination class
include('includes/ps_pagination.php');
//Connect to mysql db
$conn = mysql_connect("localhost", "root", "root");
mysql_select_db('database',$conn);
$sql = "SELECT * FROM studies";// WHERE niche = '{$_GET['niche']}'";
//Create a PS_Pagination object
$pager = new PS_Pagination($conn,$sql,4,5);
//The paginate() function returns a mysql
//result set for the current page
$rs = $pager->paginate();
//Loop through the result set
while($row = mysql_fetch_assoc($rs)) {
// $a=0;
// while ($a < $num) {
// $id=mysql_result($result,$a,"id");
// $title=mysql_result($result,$a,"title");
// $strategies=mysql_result($result,$a,"strategies");
// $client=mysql_result($result,$a,"client");
// $copy=mysql_result($result,$a,"copy");
// $thumbmedia=mysql_result($result,$a,"thumbmedia");
// $niche=mysql_result($result,$a,"niche");
$id=$row['id'];
$title=$row['title'];
$strategies=$row['strategies'];
$client=$row['client'];
$copy=$row['copy'];
$thumbmedia=$row['thumbmedia'];
$niche=$row['niche'];
echo '<div class="container"><p class="subheadred"><a href="casestudy.php?id='.$id.'">'.$title.'</a></p></div>';
echo '<div class="containerstudy"><div class="column1"><p class="subheadsmall">Strategies</p><p class="sidebarred">'.$strategies.'</p>';
echo '<p class="subheadsmall">Client</p><p class="sidebargrey">'.$client.'</p></div>';
echo '<div class="column2"><p class="bodygrey">'.substr($copy, 0, 300).'<a href="casestudy.php?id='.$id.'">...more</a></p></div>';
echo '<div class="column3"><img src="images/'.$thumbmedia.'" height="160" /></div></div>';
// $a++;
// }
}
//Display the navigation
echo $pager->renderNav();
?>
注意我已经注释了一些不必要的部分
变量$ a没有意义,因为它与$ num相比,因为它不存在,因此没有任何东西可以显示。现在这些行:
// $id=mysql_result($result,$a,"id");
// $title=mysql_result($result,$a,"title");
// $strategies=mysql_result($result,$a,"strategies");
// $client=mysql_result($result,$a,"client");
// $copy=mysql_result($result,$a,"copy");
// $thumbmedia=mysql_result($result,$a,"thumbmedia");
// $niche=mysql_result($result,$a,"niche");
当您尝试从$ result获取结果集时,也会出错。在任何地方你都没有将结果集放入$ result中,你导入的类就是这样做,并将结果集放入一个名为$ rs的标识符中。
由于您使用$row = mysql_fetch_assoc($rs)
来读取结果集,然后获取变量,您需要做的就是通过这样的数组获取列行
$id=$row['id'];
等等。一旦你这样做,那么代码应该按预期工作。然而,这让我们回到了这一点:
$sql = "SELECT * FROM studies WHERE niche = '{$_GET['niche']}'";
我必须得到最后一部分,因为这个变量没有被传递回查询字符串(看到你正在使用get),另一个问题是你第一次加载页面时这个变量'niche'没有' t存在所以第一次运行代码时,您将得到一个空白页,除非通过传入此变量的链接访问此脚本。在这一点上,我已经离开它注释掉,因为我不确定你想通过利基做什么。
希望有所帮助。
答案 1 :(得分:0)
您的查询字符串中是否设置了页面?
答案 2 :(得分:0)
我不确定这个分页库对行的索引做了什么,但是你总是将$ a设置为0.也许索引不是相对于页面而是返回给整个记录集?
换句话说,如果你在第1页将$ a设置为0,在第2页设置4,等等。
您应该可以使用$ rows_per_page和$ page来计算它。或者这可能是$ offset值,因此您将$ a设置为offset并循环它直到您点击$ rows_per_page其他行。
修改澄清解决方案:
所以我看到你能做的是:
$a=0;
while ($a < $pager->rows_per_page) {
$row = $a + $pager->offset;
$id=mysql_result($result,$row,"id");
$title=mysql_result($result,$row,"title");
$strategies=mysql_result($result,$row,"strategies");
$client=mysql_result($result,$row,"client");
$copy=mysql_result($result,$row,"copy");
$thumbmedia=mysql_result($result,$row,"thumbmedia");
$niche=mysql_result($result,$row,"niche");
...
$a++;
}
答案 3 :(得分:0)
我认为如果您更改了paginate函数,那么它将有一个参数$page
,并且您将在函数的开头设置$this->page = $page
,它将起作用。
您还必须将代码中此函数的调用更改为$pager->paginate($_GET['page'])
。
寻呼机的效率也很低,它只是为了找出整个查询,响应有多少行。我会使用类似的东西:
$all_rs = @mysql_query('SELECT COUNT(*) FROM (' . $this->sql . ')');
if(!$all_rs) {
if($this->debug) echo "SQL query failed. Check your query.<br />";
return false;
}
$this->total_rows = mysql_result($all_rs, 0, 0);
@mysql_close($all_rs);