把分页显示到固定的模板上
来源:互联网 发布:macbook 安装windows 编辑:程序博客网 时间:2024/05/21 11:27
把分页显示到固定的模板上,在fenye3.php上有一个表格,从数据库(fenye2.php)中查找的内容都显
示到表格上,然后进行分页。上次的分页都是从数据库中直接输出,单纯的从提交的页面输出一个表格
1、fenye2.php
<?php
include 'db.class.php';
$db=new db('localhost','root','123','ecshop_test');
$sql='select * from brand where 1';
$res=mysql_query($sql);
$return=array();//定义个空数组,
while($row=mysql_fetch_assoc($res)){
$return[]=$row;//它与brand_list.php中的<?php echo $value
['brand_name'];有关
} //因为下面有分页,不可能即显示所有的记录,又显示两条记录
//分页
$page=isset($_GET['page'])?$_GET['page']:1;//当前页是第一页
$pagesize=3;//每页显示3条记录
$offset=$pagesize*($page-1);//跳过的数量
$sql="select * from brand limit $offset,$pagesize";//从数据
库中读出每页显示几条内容
$result=mysql_query($sql);
//获得符合条件的所有的记录
$return=array();
while($row=mysql_fetch_assoc($result)){
$return[]=$row;// <?php foreach($return as $value): <?
php echo $value['brand_logo'];
}
//获得所有得记录数
$sql="select count(*) as total from brand";
$res=mysql_query($sql);
$rows=mysql_fetch_assoc($res);
$total_rows=$rows['total'];
include 'page.class.php';
$url='fenye2.php';
$html=page::show($total_rows,$page,$pagesize,$url);
include 'fenye3.php';
?>
2、fenye3.php
<body>
<h1>欢迎进入来看看</h1>
<table border="1">
<tr>
<th>品牌名称</th>
<th>品牌网址</th>
<th>品牌描述</th>
<th>排序</th>
<th>是否显示</th>
</tr>
<?php foreach($return as $value): ?>
<tr>
<td><?php echo $value['brand_name'];?></td>
<td><?php echo $value['brand_logo'];?></td>
<td><?php echo $value['brand_url'];?></td>
<td ><?php echo $value['brand_desc'];?></td>
<td >50</td>
<td ></td>
</tr>
<?php endforeach; ?>
<tr>
<div id="turn-page"><?php echo $html;?> </div>
</tr>
</table>
</body>
3、page.class.php // 封装分页代码
<?php
//封装这个类,在调用这个类的方法,让他给我们返回一个字符串,也就是html代码
/*
*@param string $total_rows 所有的记录数
*@param int $page 当前的页码数
*@param int $pagesize 每页需要显示几条记录
*@param string $url 分页后要跳转的地址
*@param $return html 代码,包含了分页的哪些字符串
*/
class page{
public static function show($total_rows,$page,$pagesize,$url){
//之前我们使用new class ().今天我们换一种方法,通过类调用他的方法
//先声明一个空字符串,然后再通过一步一步的连接,最终返回div中的字符串
$return='';
//求出总的页数
$total_page=ceil($total_rows/$pagesize);//floor,round
$request_url=$url.'?page=';
$return.="总共 $total_rows 个记录 分为$total_page 页 当前第 $page 页 每页显示的数
$pagesize";
// $first='<a href='''.$request_url.'">第一页</a>';
//格式化字符串
$first=sprintf('<a href="%s">%s</a>',$request_url.'1','第一页');
//一次求出上一页,下一页,尾页的字符串
if($page == 1){
$prev = '';
} else{
$prev = sprintf('<a href="%s">%s</a>',$request_url.($page-1),'上一页');
}
if($total_page==$page){
$next='';
}else{
$next=sprintf('<a href="%s">%s</a>',$request_url.($page+1),'下一页');
}
$last=sprintf('<a href="%s">%s</a>',$request_url.$total_page,'尾页');
//声明一个保存下拉列表的字符串,给这个下拉列表一个监听他发生变化的事件
$select_page='<select onchange="goPage(this)">';
//循环的输出sekect列表中的option选项
for($i=1;$i<=$total_page;$i++){
if($i==$page){
$select_page.=sprintf('<option value="%s" selected>%s</option>',$i,$i);
}else{
$select_page.=sprintf('<option value="%s">%s</option>',$i,$i);
}
}
$select_page.='</select>';
//一定要注意,定界符的结果时一定要顶格写,分号结束
$select_script =<<<SCR
<script type="text/javascript">
function goPage(obj){
window.location.href="$request_url"+obj.value;
}
</script><!--scr一定要顶格写-->
SCR;
$return.=$first.$prev.$next.$last.$select_page.$select_script;
return $return;
}
}
?>
4、db.class.php //封装数据库
<?php
class db{
private $host;
private $user;
private $pass;
private $db_name;
public function __construct($host,$user,$pass,$db_name){
//初始化对象的属性
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
$this->db_name=$db_name;
$this->connect();
}
public function connect(){
mysql_connect($this->host,$this->user,$this->pass);
mysql_select_db($this->db_name);
mysql_query("set names utf8");
}
}
?>
- 把分页显示到固定的模板上
- 把从SQList查询的数据显示到屏幕上
- asp 固定 显示分页数字
- delphi编写分页显示的模板
- 在活体检测的程序上把检测到的活体显示在右上角
- OPENCV如何把视频显示到MFC的picture控件上
- OpenCV如何把图像显示到MFC的picture控件上
- OpenCV如何把图像显示到MFC的picture控件上
- OPENCV如何把图片显示到MFC的picture控件上
- 用java程序把本地的一个文件拷贝到hdfs上并显示进度
- tableView视图列表3: 把解析到的XML数据显示在TableView上
- 把页面Table表格内容导出为Excel-仅是当前页面上显示的table 分页未知(转载 略修改)
- [iphone-cocos2d]把label显示到页面上
- QtCharts小知识:如何把曲线显示到控件上
- 通过onActivityResult()先跳转到联系人界面,然后把传回来的手机号显示到应用的EditText上
- 固定定位在Android机上的显示问题
- DataGridView将数据内容绑定到固定的列上
- 把页面上显示的价钱划上斜杠
- jQuery EasyUI中对表格进行编辑的实现代码
- 黑马程序员之 ASP.NET学习笔记:GridView控件的常用属性及方法
- hdu 1215(因数)
- 黑马程序员之 ASP.NET学习笔记:ADO.NET基础知识总结
- 判断ip地址是否合法
- 把分页显示到固定的模板上
- 文件夹之间的拷贝
- vs2008建立DLL的过程和调用过程
- 求1000以内的完数
- 计算编辑距离的算法
- PADS画板外形导入与打印
- 计算结果(利用循环)
- 天易14----JQuery实现简单的联级菜单制作
- 二叉树的非递归遍历