把分页显示到固定的模板上

来源:互联网 发布: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");
  }
 }
?>

原创粉丝点击