php在分页时只显示了第一页,后面就不显示了,本来我只查询一张表时是可以正常查到的
<formaction="test.php"method="post"><selectname="option"><optionselected="selected">请...
<form action="test.php" method="post">
<select name="option"><option selected="selected">请选择..</option>
<option>Mainpost</option>
<option>Replypost</option>
</select>
<input type="submit" name="submit" value="确定" />
</form>
<?php
function printTable( $name, $trow)
{if($name= "Mainpost")
{echo $trow['mainID']." | ";
echo $trow['author']." | ";
echo $trow['title']." | ";
echo $trow['mainContent']." | ";
echo "<br/>";
}
else if($name="Replypost")
{echo $trow['replyID']." | ";
echo $trow['author']." | ";
echo $trow['title']." | ";
echo $trow['replyContent']." | ";
echo "<br/>";
}
}
$serverName = "(local)";
$connectionInfo = array("UID"=>***, "PWD"=>***, "Database"=>***);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false)
{
echo "连接失败!";
die( print_r( sqlsrv_errors(), true));
}
if( isset($_GET['page']) )
$page = intval( $_GET['page'] );
else
$page = 1;
if( isset($_POST['option']) && $_POST['option']!= "请选择.." )
$name= $_POST['option'];
else
$name= NULL;
if($name)
{$id=$name."ID";
$id=str_replace( "post", "", $id);
$page_size = 10;
$sql = "select count(*) as amount from ".$name;
$result =sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($result);
$amount = $row['amount'];
if( $amount ){
if( $amount < $page_size )
$page_count = 1;
if( $amount % $page_size )
$page_count = (int)($amount / $page_size) + 1;
else
$page_count = $amount / $page_size;
}
else
$page_count = 0;
$page_string= "" ;
if( $page == 1 )
$page_string .= "第一页|上一页|";
else
$page_string .= "<a href=?page=1>第一页</a>|<a href=?page=".($page-1).">上一页</a>|";
if( ($page == $page_count) || ($page_count == 0) )
$page_string .= "下一页|尾页";
else
$page_string .= "<a href=?page=".($page+1).">下一页</a>|<a href=?page=".$page_count.">尾页</a>";
if( $amount )
{
$tsql="select top ". $page_size ." * from ".$name."
where (".$id." not in
(select top ". ($page-1)*$page_size .$id." from ".$name."
order by ".$id."))
order by ".$id;
$tresult =sqlsrv_query($conn, $tsql);
if( $tresult === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
while ($trow =sqlsrv_fetch_array($tresult, SQLSRV_FETCH_ASSOC) )
{ $rowset[] = $trow;
printTable($name, $trow);
}
}
else
$rowset = array();
echo $page_string;
sqlsrv_free_stmt( $tresult);
}
sqlsrv_close( $conn);
?> 展开
<select name="option"><option selected="selected">请选择..</option>
<option>Mainpost</option>
<option>Replypost</option>
</select>
<input type="submit" name="submit" value="确定" />
</form>
<?php
function printTable( $name, $trow)
{if($name= "Mainpost")
{echo $trow['mainID']." | ";
echo $trow['author']." | ";
echo $trow['title']." | ";
echo $trow['mainContent']." | ";
echo "<br/>";
}
else if($name="Replypost")
{echo $trow['replyID']." | ";
echo $trow['author']." | ";
echo $trow['title']." | ";
echo $trow['replyContent']." | ";
echo "<br/>";
}
}
$serverName = "(local)";
$connectionInfo = array("UID"=>***, "PWD"=>***, "Database"=>***);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false)
{
echo "连接失败!";
die( print_r( sqlsrv_errors(), true));
}
if( isset($_GET['page']) )
$page = intval( $_GET['page'] );
else
$page = 1;
if( isset($_POST['option']) && $_POST['option']!= "请选择.." )
$name= $_POST['option'];
else
$name= NULL;
if($name)
{$id=$name."ID";
$id=str_replace( "post", "", $id);
$page_size = 10;
$sql = "select count(*) as amount from ".$name;
$result =sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($result);
$amount = $row['amount'];
if( $amount ){
if( $amount < $page_size )
$page_count = 1;
if( $amount % $page_size )
$page_count = (int)($amount / $page_size) + 1;
else
$page_count = $amount / $page_size;
}
else
$page_count = 0;
$page_string= "" ;
if( $page == 1 )
$page_string .= "第一页|上一页|";
else
$page_string .= "<a href=?page=1>第一页</a>|<a href=?page=".($page-1).">上一页</a>|";
if( ($page == $page_count) || ($page_count == 0) )
$page_string .= "下一页|尾页";
else
$page_string .= "<a href=?page=".($page+1).">下一页</a>|<a href=?page=".$page_count.">尾页</a>";
if( $amount )
{
$tsql="select top ". $page_size ." * from ".$name."
where (".$id." not in
(select top ". ($page-1)*$page_size .$id." from ".$name."
order by ".$id."))
order by ".$id;
$tresult =sqlsrv_query($conn, $tsql);
if( $tresult === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
while ($trow =sqlsrv_fetch_array($tresult, SQLSRV_FETCH_ASSOC) )
{ $rowset[] = $trow;
printTable($name, $trow);
}
}
else
$rowset = array();
echo $page_string;
sqlsrv_free_stmt( $tresult);
}
sqlsrv_close( $conn);
?> 展开
3个回答
展开全部
送你一个分页类
<?php
class page{
/*
1、我要知道一页有几行数据。
2、当前是多少页
3、最大数据条数
方法一:计算最大页数
方法二:判断页号是否越界
方法三:返回部分分页sql语句
*方法四:动态输出分页URL。
*/
public $pageSize;//一页有多少行
public $maxData;//最大的数据条数
public $nowPage;//当前页面
public $url;
/*1、初始化数据
*/
function __construct($pageSize=10,$maxData,$nowPage=1){
$this->pageSize=$pageSize;
$this->maxData=$maxData;
$this->nowPage=$_GET['page'] ? $_GET['page'] :1;
$this->url=$_SERVER['PHP_SELF'];
}
/*
2、得到limit的起始位置。
*/
function getStart(){
return ($this->nowPage-1)*$this->pageSize;
}
/*
3、输入html链接
*/
function outHtml(){
$str="<a href='test.php?page=".($this->nowPage-1)."'>上一页</a>";
$str.="<a href='test.php?page=".($this->nowPage+1)."'>下一页</a>";
return $str;
}
}
$conn=mysql_connect('localhost','root','1234@abcd');
mysql_select_db('lampoa');
mysql_query('set names utf8');
/*******************************/
$sql="select count(*) as count from lamp_city";
$counts=mysql_query($sql);
$counts=mysql_fetch_assoc($counts);
/*******************************/
$everyPage=10;
$page=new page($everyPage,$counts['count']);//是分页类
//var_dump($page);
$sql1="select * from lamp_city limit ".$page->getStart().",{$everyPage}";
$result=mysql_query($sql1);
while($rows=mysql_fetch_assoc($result)){
var_dump($rows);
}
echo $sql1;
echo '<br />';
echo $page->outHtml();
//那我们个类中肯定要有一个方法返回 limit 0,10
?>
<?php
class page{
/*
1、我要知道一页有几行数据。
2、当前是多少页
3、最大数据条数
方法一:计算最大页数
方法二:判断页号是否越界
方法三:返回部分分页sql语句
*方法四:动态输出分页URL。
*/
public $pageSize;//一页有多少行
public $maxData;//最大的数据条数
public $nowPage;//当前页面
public $url;
/*1、初始化数据
*/
function __construct($pageSize=10,$maxData,$nowPage=1){
$this->pageSize=$pageSize;
$this->maxData=$maxData;
$this->nowPage=$_GET['page'] ? $_GET['page'] :1;
$this->url=$_SERVER['PHP_SELF'];
}
/*
2、得到limit的起始位置。
*/
function getStart(){
return ($this->nowPage-1)*$this->pageSize;
}
/*
3、输入html链接
*/
function outHtml(){
$str="<a href='test.php?page=".($this->nowPage-1)."'>上一页</a>";
$str.="<a href='test.php?page=".($this->nowPage+1)."'>下一页</a>";
return $str;
}
}
$conn=mysql_connect('localhost','root','1234@abcd');
mysql_select_db('lampoa');
mysql_query('set names utf8');
/*******************************/
$sql="select count(*) as count from lamp_city";
$counts=mysql_query($sql);
$counts=mysql_fetch_assoc($counts);
/*******************************/
$everyPage=10;
$page=new page($everyPage,$counts['count']);//是分页类
//var_dump($page);
$sql1="select * from lamp_city limit ".$page->getStart().",{$everyPage}";
$result=mysql_query($sql1);
while($rows=mysql_fetch_assoc($result)){
var_dump($rows);
}
echo $sql1;
echo '<br />';
echo $page->outHtml();
//那我们个类中肯定要有一个方法返回 limit 0,10
?>
展开全部
我怎么感觉你的语句不能分页,分页是这样的语句
select * from 表名 limit 开始行数,结束的行数
写得很复杂,感觉你的逻辑也有点不对。也许你的思路跟我的不一样吧!
select * from 表名 limit 开始行数,结束的行数
写得很复杂,感觉你的逻辑也有点不对。也许你的思路跟我的不一样吧!
更多追问追答
追问
我用的是sqlserver,不支持limit语句,而且之前用这个语句查到数据了,现在只不过用变量替换上去
追答
原来是这样啊!那你应该把sql语句输出来,贴sqlserver查询器里试试
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
好好检查你的SQL语句
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询