1. 程式人生 > 其它 >thinkphp 框架自帶搜尋+分頁+搜尋標紅

thinkphp 框架自帶搜尋+分頁+搜尋標紅

、、、、、、、、、、控制器方法

public function index()
    {
        //接受搜尋關鍵字
        $word=input('word');
        $where=[];
        if (!empty($word)){
            $where['title']=['like',"%".$word."%"];
        }
        $data=ArticleModel::index($where,$word);
        if (!empty($data)){
            foreach ($data as
$k=>$v){ $v['title']=str_replace($word,"<span style='color: red'>$word</span>",$v['title']); } } $this->assign('data',$data); $this->assign('word',$word); return $this->fetch(); }

、、、、、、、、、、、、、、、、表單頁面

<!DOCTYPE html>
<html lang="
en"> <head> <meta charset="utf-8"> <title>後臺管理系統</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content=""> <meta name="author" content=""> <link href="__STATIC__/admin/css/main.css
" rel="stylesheet" type="text/css"/> <link href="__STATIC__/admin/css/bootstrap.min.css" rel="stylesheet" type="text/css"/> <link href="__STATIC__/admin/css/bootstrap-responsive.min.css" rel="stylesheet" type="text/css"/> <script src="__STATIC__/admin/js/jquery-1.8.1.min.js"></script> <script src="__STATIC__/admin/js/bootstrap.min.js"></script> </head> <body> <div class="header"> <h1 class="page-title">商品列表</h1> </div> <div class="well"> <!-- search button --> <form action="/task/article/index" method="get" class="form-search"> <div class="row-fluid" style="text-align: left;"> <div class="pull-left span4 unstyled"> <p> 商品名稱:<input class="input-medium" name="word" value="{$word}" type="text"></p> </div> </div> <button type="submit" class="btn">查詢</button> <a class="btn btn-primary" href="#">新增</a> </form> </div> <div class="well"> <!-- table --> <table class="table table-bordered table-hover table-condensed"> <thead> <tr> <th>編號</th> <th>標題</th> <th>時間</th> <th>所在欄目</th> <th>是否稽核</th> <th>圖片</th> <th>操作</th> </tr> </thead> <tbody> {foreach $data as $k=>$v} <tr class="success"> <td>{$k+1}</td> <td><a href="javascript:void(0);">{$v['title']}</a></td> <td>{$v['time']}</td> <td>{$v['content']}</td> <td> {if $v['get']==1 } <span style="color: red">已稽核</span> {else/} <span style="color: greenyellow">未稽核</span> {/if} </td> <td><img src="$v['img']" alt="無法顯示"></td> <td> <a href="javascript:void(0);"> 編輯 </a> <a href="javascript:void(0);" onclick="if(confirm('確認刪除?')) location.href='#'"> 刪除 </a> </td> </tr> {/foreach} </tbody> </table> {$data->render()} </div> </body> </html>

。。。、、、、、、、、、、、、、、模型頁面

//資料庫資料展示
public static function index($where,$word){
        //搜尋接值
    $config=[
        'query'=>['word'=>$word]
    ];
    return  self::where($where)
        ->order('id','desc')
        ->paginate(3,false,$config);
}
}