ThinkPHP

来源:互联网 发布:大学排课软件 编辑:程序博客网 时间:2024/05/16 18:31
<?phpnamespace Home\Controller;use Think\Controller;class NewsController extends Controller {    public function add(){        if(IS_POST){            $post = I('post.');            $res = D('news')->add($post);            if($res){                $this->success('添加成功','show');            }else{                $this->error('添加失败');            }        }else{            $this->display();        }    }    public function show(){        $s_title = I('get.s_title');        $s_content = I('get.s_content');        $search = array('s_title'=>$s_title, 's_content'=>$s_content);        $News = M('news'); // 实例化News对象        // 查询满足要求的总记录数        $count = $News->where("title like '%$s_title%' and content like '%$s_content%'")->count();        // 实例化分页类 传入总记录数和每页显示的记录数(4)        $Page       = new \Think\Page($count,4);        $show       = $Page->show();// 分页显示输出        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性        $list = $News->where("title like '%$s_title%' and content like '%$s_content%'")->limit($Page->firstRow.','.$Page->listRows)->select();        $this->assign('search',$search);        $this->assign('data',$list);// 赋值数据集        $this->assign('page',$show);// 赋值分页输出        $this->display(); // 输出模板    }}

add.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>添加</title></head><body>    <center>        <form action="__URL__/add" method="post">            <table>                <tr>                    <td>标题</td>                    <td><input type="text" name="title"></td>                </tr>                <tr>                    <td>内容</td>                    <td><textarea name="content" id="" cols="30" rows="10"></textarea></td>                </tr>                <tr>                    <td></td>                    <td><input type="submit" value="添加"></td>                </tr>            </table>        </form>    </center></body></html>

show.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>展示</title></head><body>    <center>        <div>            <form action="__URL__/show" method="get">                标题:<input type="text" name="s_title" value="{$search.s_title}">                内容:<input type="text" name="s_content" value="{$search.s_content}">                <input type="submit" value="搜索">            </form>        </div>        <table border="1">            <tr>                <th>id</th>                <th>标题</th>                <th>内容</th>            </tr>            <if condition="empty($data) neq false">                <td colspan="3">没有该数据。。。。。。。。。。。</td>            <else />                <foreach name="data" item="v">                    <tr>                        <td>{$v.id}</td>                        <td>{$v.title}</td>                        <td>{$v.content}</td>                    </tr>                </foreach>            </if>        </table>        <div>{$page}</div>        <a href="__URL__/add"><button>添加</button></a>    </center></body></html>
原创粉丝点击