满分求PHP分页显示代码及解决两提示和一个疑问
<?php$link=mysql_connect("localhost","root","***")ordie("Couldnotconnect:".mysql_erro...
<?php
$link = mysql_connect("localhost","root","***")
or die("Could not connect: ".mysql_error());
mysql_select_db("php",$link);
if( isset($_GET['page']) ){
$page = intval( $_GET['page'] );
}
else{
$page = 1;
}
$PageSize=2;
$sql = "select count(*) as amount from pro ";
$result = mysql_query($sql);
$row = mysql_fetch_row($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 ){
$sql = "select * from table order by id desc limit ". ($page-1)*$page_size .",
$page_size";
$result = mysql_query($sql);
while ( $row = mysql_fetch_row($result) ){
$rowset[] = $row;
}
}else{
$rowset = array();
}
// 没有包含显示结果的代码,那不在讨论范围,只要用foreach就可以很简单的用得到的二维
数组来显示结果
?>
Notice: Undefined index: amount in F:\Lw\Untitled-1.php on line 25
Notice: Use of undefined constant 数组来显示结果 - assumed '数组来显示结果' in F:\Lw\Untitled-1.php on line 71
能解决满分送上
如何用foreach 来实现二维数组 展开
$link = mysql_connect("localhost","root","***")
or die("Could not connect: ".mysql_error());
mysql_select_db("php",$link);
if( isset($_GET['page']) ){
$page = intval( $_GET['page'] );
}
else{
$page = 1;
}
$PageSize=2;
$sql = "select count(*) as amount from pro ";
$result = mysql_query($sql);
$row = mysql_fetch_row($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 ){
$sql = "select * from table order by id desc limit ". ($page-1)*$page_size .",
$page_size";
$result = mysql_query($sql);
while ( $row = mysql_fetch_row($result) ){
$rowset[] = $row;
}
}else{
$rowset = array();
}
// 没有包含显示结果的代码,那不在讨论范围,只要用foreach就可以很简单的用得到的二维
数组来显示结果
?>
Notice: Undefined index: amount in F:\Lw\Untitled-1.php on line 25
Notice: Use of undefined constant 数组来显示结果 - assumed '数组来显示结果' in F:\Lw\Untitled-1.php on line 71
能解决满分送上
如何用foreach 来实现二维数组 展开
展开全部
这是我正常用的一个,看合不合你用
listall.php的内容
<?
//require("head.inc");
require("db.inc");
require("functions.inc");
if (strlen(trim($unit_id))==0)
{
exit;
}
if (strlen(trim($col_id))==0)
{
exit;
}
$pt=50; //每页50条
//连接数据库
$conn=db_connect();
if(!$conn) die("mysql connect failed. please wait to retry...");
?>
<Script Language="Javascript">
<!--
function openwin(loadpos){
window.open(loadpos,'surveywin','top=58,left=66,width=670,height=450, toolbar=yes,menubar=no,scrollbars=yes, resizable=no');
}
-->
</Script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>列出所有</title>
<style>.p1{font-size:9pt}
</style>
</head>
<body>
<table border="0" width="100%" class="p1" cellspacing="1">
<tr><td width=100%><?echo get_colname($conn,$col_id,$unit_id)?></td></tr>
<?
$sql="select * from tb".$unit_id." where col_id = ".$col_id." ORDER BY info_id DESC";
$result=mysql_query($sql,$conn);
if($result) $num=mysql_num_rows($result);
$total_recorder=$num;
$total_page=ceil($total_recorder/$pt);
if(!isset($page) or $page=="") $page=1;
//有效性检验(注意这个地方)
if($page<1) $page=1;
if($page>$total_page) $page=$total_page;
$firstpage=1;
$lastpage=$total_page;
$priorpage=($page>$firstpage)?($page-1):1;
$nextpage=($page<$lastpage)?($page+1):$lastpage;
if($page==$lastpage){
$startx=$pt * ($page - 1)+1;
$starty=$total_recorder;
}
else{
$startx=$pt * ($page - 1)+1;
$starty=$pt * ($page);
}
?>
<tr>
<form action=<? echo "listall.php?unit_id=$unit_id&col_id=$col_id"; ?> method=post>
<td width="100%"> <b>>>分页显示</b>:
<?
if($page>$firstpage){ //此处也可以这样写
echo "<A href='listall.php?page=$firstpage&unit_id=$unit_id&col_id=$col_id'>首页</A> ";
echo "<A href='listall.php?page=$priorpage&unit_id=$unit_id&col_id=$col_id'>上页</A> ";
}
else{
echo "首页 ";
echo "上页 ";
}
if($page<$lastpage){ //此处也可以这样写 $page<$lastpage
echo "<A href='listall.php?page=$nextpage&unit_id=$unit_id&col_id=$col_id'>下页</A> ";
echo "<A href='listall.php?page=$lastpage&unit_id=$unit_id&col_id=$col_id'>尾页</A> ";
}
else{
echo "下页 ";
echo "尾页 ";
}
?>
页次:<b><? echo $page; ?></b>/<b><? echo $total_page; ?></b>页
<b><? echo $pt; ?></b>篇/页 共<b><? echo $total_recorder; ?></b>篇
转到第: <input type="text" name="page" size="4" style="border: 1 solid #C0C0C0" class="p1">页 <input type="submit" value="Goto" name="B1" class="p2" style="border-style: ridge; border-width: 1">
</td>
</form>
</tr>
<tr><td width="100%" valign=top align="left">
<?
if($total_recorder==0 ){
echo "<b>刚刚开始启用,目前还没有内容.</b>";
}
else{
mysql_data_seek($result,$startx-1);
for($i=$startx;$i<=$starty;$i++){ //显示留言
$row=mysql_fetch_object($result);
$infoURL="javascript:openwin('show.php?id=".$row->info_id."&unit_id=$unit_id')";
if(strlen(trim($row->file_url))!=0)
{
$download=" <a href=".$row->file_url.">下载</a> ";
}
else
{
$download="";
}
if(!strlen(trim($row->info_acl))==0){
if(acl_verify($row->info_acl,$user_id)){
echo "<br><img border=0 src=images/web/triangle.gif><a href=$infoURL>".$row->info_subject."</a>$download(发布人:".get_usercnname($conn,$row->user_id).$row->info_date.")";
}
}
else
echo "<br><img border=0 src=images/web/triangle.gif><a href=$infoURL>".$row->info_subject."</a>$download(发布人:".get_usercnname($conn,$row->user_id).$row->info_date.")";
}
}
?>
<br><br>
<b>>>分页显示</b>:
<?
if($page>$firstpage){ //此处也可以这样写
echo "<A href='listall.php?page=$firstpage&unit_id=$unit_id&col_id=$col_id'>首页</A> ";
echo "<A href='listall.php?page=$priorpage&unit_id=$unit_id&col_id=$col_id'>上页</A> ";
}
else{
echo "首页 ";
echo "上页 ";
}
if($page<$lastpage){ //此处也可以这样写 $page<$lastpage
echo "<A href='listall.php?page=$nextpage&unit_id=$unit_id&col_id=$col_id'>下页</A> ";
echo "<A href='listall.php?page=$lastpage&unit_id=$unit_id&col_id=$col_id'>尾页</A> ";
}
else{
echo "下页 ";
echo "尾页 ";
}
?>
</td>
</tr>
</table>
</body>
<? db_close($conn); ?>
</html>
在需要调用分页的文件里加的内容
<?
if($page>$firstpage){ //此处也可以这样写
echo "<A href='listall.php?page=$firstpage&unit_id=$unit_id&col_id=$col_id'>首页</A> ";
echo "<A href='listall.php?page=$priorpage&unit_id=$unit_id&col_id=$col_id'>上页</A> ";
}
else{
echo "首页 ";
echo "上页 ";
}
if($page<$lastpage){ //此处也可以这样写 $page<$lastpage
echo "<A href='listall.php?page=$nextpage&unit_id=$unit_id&col_id=$col_id'>下页</A> ";
echo "<A href='listall.php?page=$lastpage&unit_id=$unit_id&col_id=$col_id'>尾页</A> ";
}
else{
echo "下页 ";
echo "尾页 ";
}
?>
listall.php的内容
<?
//require("head.inc");
require("db.inc");
require("functions.inc");
if (strlen(trim($unit_id))==0)
{
exit;
}
if (strlen(trim($col_id))==0)
{
exit;
}
$pt=50; //每页50条
//连接数据库
$conn=db_connect();
if(!$conn) die("mysql connect failed. please wait to retry...");
?>
<Script Language="Javascript">
<!--
function openwin(loadpos){
window.open(loadpos,'surveywin','top=58,left=66,width=670,height=450, toolbar=yes,menubar=no,scrollbars=yes, resizable=no');
}
-->
</Script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>列出所有</title>
<style>.p1{font-size:9pt}
</style>
</head>
<body>
<table border="0" width="100%" class="p1" cellspacing="1">
<tr><td width=100%><?echo get_colname($conn,$col_id,$unit_id)?></td></tr>
<?
$sql="select * from tb".$unit_id." where col_id = ".$col_id." ORDER BY info_id DESC";
$result=mysql_query($sql,$conn);
if($result) $num=mysql_num_rows($result);
$total_recorder=$num;
$total_page=ceil($total_recorder/$pt);
if(!isset($page) or $page=="") $page=1;
//有效性检验(注意这个地方)
if($page<1) $page=1;
if($page>$total_page) $page=$total_page;
$firstpage=1;
$lastpage=$total_page;
$priorpage=($page>$firstpage)?($page-1):1;
$nextpage=($page<$lastpage)?($page+1):$lastpage;
if($page==$lastpage){
$startx=$pt * ($page - 1)+1;
$starty=$total_recorder;
}
else{
$startx=$pt * ($page - 1)+1;
$starty=$pt * ($page);
}
?>
<tr>
<form action=<? echo "listall.php?unit_id=$unit_id&col_id=$col_id"; ?> method=post>
<td width="100%"> <b>>>分页显示</b>:
<?
if($page>$firstpage){ //此处也可以这样写
echo "<A href='listall.php?page=$firstpage&unit_id=$unit_id&col_id=$col_id'>首页</A> ";
echo "<A href='listall.php?page=$priorpage&unit_id=$unit_id&col_id=$col_id'>上页</A> ";
}
else{
echo "首页 ";
echo "上页 ";
}
if($page<$lastpage){ //此处也可以这样写 $page<$lastpage
echo "<A href='listall.php?page=$nextpage&unit_id=$unit_id&col_id=$col_id'>下页</A> ";
echo "<A href='listall.php?page=$lastpage&unit_id=$unit_id&col_id=$col_id'>尾页</A> ";
}
else{
echo "下页 ";
echo "尾页 ";
}
?>
页次:<b><? echo $page; ?></b>/<b><? echo $total_page; ?></b>页
<b><? echo $pt; ?></b>篇/页 共<b><? echo $total_recorder; ?></b>篇
转到第: <input type="text" name="page" size="4" style="border: 1 solid #C0C0C0" class="p1">页 <input type="submit" value="Goto" name="B1" class="p2" style="border-style: ridge; border-width: 1">
</td>
</form>
</tr>
<tr><td width="100%" valign=top align="left">
<?
if($total_recorder==0 ){
echo "<b>刚刚开始启用,目前还没有内容.</b>";
}
else{
mysql_data_seek($result,$startx-1);
for($i=$startx;$i<=$starty;$i++){ //显示留言
$row=mysql_fetch_object($result);
$infoURL="javascript:openwin('show.php?id=".$row->info_id."&unit_id=$unit_id')";
if(strlen(trim($row->file_url))!=0)
{
$download=" <a href=".$row->file_url.">下载</a> ";
}
else
{
$download="";
}
if(!strlen(trim($row->info_acl))==0){
if(acl_verify($row->info_acl,$user_id)){
echo "<br><img border=0 src=images/web/triangle.gif><a href=$infoURL>".$row->info_subject."</a>$download(发布人:".get_usercnname($conn,$row->user_id).$row->info_date.")";
}
}
else
echo "<br><img border=0 src=images/web/triangle.gif><a href=$infoURL>".$row->info_subject."</a>$download(发布人:".get_usercnname($conn,$row->user_id).$row->info_date.")";
}
}
?>
<br><br>
<b>>>分页显示</b>:
<?
if($page>$firstpage){ //此处也可以这样写
echo "<A href='listall.php?page=$firstpage&unit_id=$unit_id&col_id=$col_id'>首页</A> ";
echo "<A href='listall.php?page=$priorpage&unit_id=$unit_id&col_id=$col_id'>上页</A> ";
}
else{
echo "首页 ";
echo "上页 ";
}
if($page<$lastpage){ //此处也可以这样写 $page<$lastpage
echo "<A href='listall.php?page=$nextpage&unit_id=$unit_id&col_id=$col_id'>下页</A> ";
echo "<A href='listall.php?page=$lastpage&unit_id=$unit_id&col_id=$col_id'>尾页</A> ";
}
else{
echo "下页 ";
echo "尾页 ";
}
?>
</td>
</tr>
</table>
</body>
<? db_close($conn); ?>
</html>
在需要调用分页的文件里加的内容
<?
if($page>$firstpage){ //此处也可以这样写
echo "<A href='listall.php?page=$firstpage&unit_id=$unit_id&col_id=$col_id'>首页</A> ";
echo "<A href='listall.php?page=$priorpage&unit_id=$unit_id&col_id=$col_id'>上页</A> ";
}
else{
echo "首页 ";
echo "上页 ";
}
if($page<$lastpage){ //此处也可以这样写 $page<$lastpage
echo "<A href='listall.php?page=$nextpage&unit_id=$unit_id&col_id=$col_id'>下页</A> ";
echo "<A href='listall.php?page=$lastpage&unit_id=$unit_id&col_id=$col_id'>尾页</A> ";
}
else{
echo "下页 ";
echo "尾页 ";
}
?>
展开全部
Notice: Undefined index: amount in F:\Lw\Untitled-1.php on line 25
解决方法:mysql_fetch_row改为mysql_fetch_array
-----------------
Notice: Use of undefined constant 数组来显示结果 - assumed '数组来显示结果' in F:\Lw\Untitled-1.php on line 71
解决方法:第71行缺少引号"数组来显示结果"
-----------------
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}
解决方法:mysql_fetch_row改为mysql_fetch_array
-----------------
Notice: Use of undefined constant 数组来显示结果 - assumed '数组来显示结果' in F:\Lw\Untitled-1.php on line 71
解决方法:第71行缺少引号"数组来显示结果"
-----------------
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
简单极了,把 =1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询