当前位置:编程学习 > php >>

php生成html分页问题

temp.html <HTML> <TITLE>{title}</TITLE> <BODY> {articletable}<br> {mune} </BODY> </HTML> php生成文件 <?php $conn=mysql_connect('localhost','root','') or die('连接失败:'.mysql_error()); //选择数据表 if(mysql_select_db('mynews',$conn)){ echo'选择数据库成功!'.'<p>'; }else{ echo'数据库选择失败!'.mysql_error().'<p>'; } @header("Content-Type: text/html; charset=utf-8"); mysql_query("SET NAMES 'utf8'"); $fp = fopen ("temp.html","r"); //读方式打开temp.html $content = fread ($fp,filesize ("temp.html")); //查看temp.html内容 $onepage =2; //每页10条 $sql = "select id from news"; $query = mysql_query ($sql); $num = mysql_num_rows ($query); $allpages = ceil ($num / $onepage); //分成多少页 for ($i = 0;$i<$allpages; $i++){ if ($i == 0){ $indexpath = "index.html"; } else { $indexpath = "index_".$i.".html"; } $start = $i * $onepage; $list = ''; $sql_for_page = "select * from news limit $start,$onepage"; $result=mysql_query($sql_for_page); while($row=mysql_fetch_array($result)) { $list .= 'uid='.$row['id'].$row['title'].'<br>'; } $content1 = str_replace ("{articletable}",$list.$i,$content); //分页 $list1 = ''; for ($j = 0;$j<$allpages; $j++){ if ($j == 0){ $list1 .= '<a href="index.html" >第'.($j+1).'页 </a>|'; } else { $list1 .= "<a href='index_".$j.".html' >第".($j+1)."页 </a>|"; } } $content2 = str_replace ("{mune}",$list1,$content1); if (is_file ($indexpath)){ @unlink ($indexpath); //若文件已存在,则删除 } $handle = fopen ($indexpath,"w"); //打开文件指针,创建文件 /*   检查文件是否被创建且可写 */ if (!is_writable ($indexpath)){ echo "文件:".$indexpath."不可写,请检查其属性后重试!"; //修改为echo } if (!fwrite ($handle,$content2)){ //将信息写入文件 echo "生成文件".$indexpath."失败!"; //修改为echo } fclose ($handle); //关闭指针 } fclose ($fp); die ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!"); ?> 现在生成 uid=1biaoti uid=2biaoti 0 第1页 |第2页 |第3页 |第4页 |第5页 |第6页 |第7页 |第8页 |第9页 |第10页 |第11页 |第12页 | 我希望能成为 上一页 1 2 3 4 5 下一页 (这种模式) 意思是我现在位置是第2页,当点下一页是:上一页 1 2 3 4 5 下一页 当我现在位置是第5页,点下一页效果是 上一页 2 3 4 5 6 下一页 (第一种) 上一页 6 7 8 9 10 下一页 (第二种) 以上其中一种,希望能有完整代码,在线等......
追问:大哥,能发完整的代码吗???实现效果就这两种其中一种
上一页 2 3 4 5 6 下一页  (第一种)
上一页 6 7 8 9 10 下一页  (第二种)
答案:加一个变量记录当前页面$nowpage,改一下$list1,键值从$nowpage-2到$nowpage+2五条记录,上一页的是$nowpage-1,下一页的是$nowpage+1,注意边界情况
其他:<?php 
	header("content-type:text/html;charset=utf-8");
	require_once 'classes/MyDB.class.php';
	$db=new MyDB();
	$sql="select * from book";
	$arr=$db->execute($sql);
	$rows=count($arr);
	if($rows%5==0){
		$pages=$rows/5;
	}else{
		$pages=(int)($pages/5)+1;
	}
	@$currentPage=$_GET['currentPage'];
	if(!isset($currentPage) || $currentPage<1){
		$currentPage=1;
	}
	if($currentPage>$pages && $pages!=0){
		$currentPage=$pages;
	}
	$sql="select * from book limit ".(($currentPage-1)*5).",5";
	$arr=$db->execute($sql);
?>
<html>
<head>
	<title>图书列表</title>
</head>
<body>
<table width="100%" border="0" align="center" bgcolor="FFDC75">
<tr>
	<td align="center">
		<font face="Verdana" size="+1">
			图书列表
		</font>
	</td>
</tr>
</table>
<br>
<table align="center" border="1" bordercolor="FFDC75" cellpadding="3" cellspacing="3">
   <tr>
       <th>序号</th>
       <th>书名</th>
       <th>价格</th>
       <th>数量</th>       
       <th>操作</th>
   </tr> 
   <?php foreach($arr as $a){?>
	 <form method="post" action="addorderline.php">
			<input type="hidden" name="bookid" value="<?php echo $a->id;?>"/>
   			 <tr>
       			 <td align="center"><?php echo $a->id;?></td>
      			 <td align="center"><?php echo $a->name;?></td>
      			 <td align="center"><?php echo $a->price;?></td>
      			 <td align="center"><input type="text" name="shuliang" value="0" size="4"></td>       
      			 <td align="center"><input type="submit" value="购买"/></td>
			</tr>
	</form> 
	<?php }?>
	
</table>
<br>
<center>
<input type="button" onclick="document.location='listCart.php'" value="查看购物车">
 <input type="button" onclick="document.location='listOrder.php'" value="查看用户订单">
 <input type="button" onclick="document.location='confirm.php'" value="提交订单">
<br/>
<strong>
	共<?php echo $pages;?>页 
	当前第<?php echo $currentPage;?>页 
	<a href='listBook.php?currentPage=1'>首页</a> 
	<a href='listBook.php?currentPage=<?php echo $currentPage-1;?>'>上一页</a> 
	<a href='listBook.php?currentPage=<?php echo $currentPage+1;?>'>下一页</a> 
	<a href='listBook.php?currentPage=<?php echo $pages;?>'>末页</a> 
</strong>
</center>
</body>
</html>

这个 你在 改改 应该可以成为你想要的那种... 

上一个:请教一个问题,网页开发~ 经验多的人 php,ajax,javascript
下一个:php可以运行哪些加密文件

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,