1.查询显示数据库的内容

控制器里的内容

public function shouye()
    {
        $n = M("car");
        $arr = $n->select();
        $this->assign("shuju",$arr);
        $this->display();

htm页面,用<foreach></foreach>标签遍历显示数据

<table width="100%" border="1" cellspacing="0" cellpadding="0">
         <tr>
             <td>代号</td>
             <td>名称</td>
             <td>操作</td>
         </tr>
         
         <foreach name="shuju" item="v"><!--shuju是传递的变量的名称-->
             <tr>
                 <td>{$v.code}</td>数据的列名
                 <td>{$v.name}</td>
                 <td>操作</td>
             </tr>
         </foreach>
     </table>

2.根据关键字查询相关的数据,建立form表单

<form action="__ACTION__" method="get">
            <div>名称:<input type="text" name="name"/><input type="submit" value="查询"/></div>
            
        </from>

 public function shouye()
    {
        
    /*    $tj = "1=1";
        $name = "";*/
        if(!empty($_GET["name"]))
        {
            $name = $_GET["name"];
            $tj = "Name like '%{$name}%'";//根据name搜索name列相关的数据
        }
    
        $n = M("car");
        $arr = $n->where($tj)->select();
        $this->assign("shuju",$arr);
        $this->show();

增加分页

把分页的类文件page.class.php文件引入到数据文件夹中更改命名空间为 names Homes\shuju

 public function shouye()
    {
    
       $tj = "1=1";
        $name = "";
        if(!empty($_GET["name"]))
        {
            $name = $_GET["name"];
            $tj = "Name like '%{$name}%'";
        }
    
        $n = M("car");
        
        //分页
        $zts = $n->where($tj)->count();//求数据的总条数和查询同时查询的条数
        
        $page = new \Home\shuju\Page($zts,3);//造page对象,传入数据总条数,每页显示几条

    
        $arr = $n->where($tj)->limit($page->limit)->select();
        $this->assign("shuju",$arr);
        $this->assign("fenye",$page->fpage());//显示分页信息;
        //$this->assign("name",name);
        $this->show();
        
    }

 

posted on 2017-03-12 16:53  甘冬冬  阅读(1645)  评论(0编辑  收藏  举报