原生分页后搜索

来源:互联网 发布:手机二维码扫描软件 编辑:程序博客网 时间:2024/05/17 23:15

搜索页面

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Document</title>  
  6.     <script src="jq.js"></script>  
  7. </head>  
  8. <body>  
  9.     <form action="?r=sp/search" method="post">  
  10.         <table border="1" align="center">  
  11.             <tr>  
  12.                 <td><input type="text" class="price" name="price"></td>  
  13.                 <td><input type="submit" value="搜索" class="search"></td>  
  14.             </tr>  
  15.         </table>  
  16.     </form>  
  17. </body>  
  18. </html>  
控制器
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. class SpController extends Controller{  
  2.     public $enableCsrfValidation = false;  
  3.     public function actionLogin()  
  4.     {  
  5.         return $this->renderPartial('login');  
  6.     }  
  7.     public function actionSearch()  
  8.     {  
  9.         if(\Yii::$app->request->isPost)  
  10.         {  
  11.             $price=\yii::$app->request->post('price');  
  12.         }  
  13.         else  
  14.         {  
  15.             $price=\yii::$app->request->get('price');  
  16.         }  
  17.         $db=new Page_search;  
  18.         $ac=$db->find()->where("username like '%$price%'")->count();  
  19.         //接受分页的值  
  20.         $page=\yii::$app->request->get('page');  
  21.         //判断分页是否存在  
  22.         $pages=isset($page)?$page:1;  
  23.         //设置每页显示条数  
  24.         $number=2;  
  25.         //设置总页数  
  26.         $num_page=ceil($ac/$number);  
  27.         //设置上下页  
  28.         $last=$pages-1<=1?$pages:$pages-1;  
  29.         $next=$pages+1>=$num_page?$num_page:$pages+1;  
  30.         //计算偏移量  
  31.         $offset=($pages-1)*$number;  
  32.         $where="username like '%$price%'";  
  33.         $sql="select * from page_search where $where limit $offset,$number";  
  34.         $list=$db->findBySql($sql)->asArray()->all();        
  35.         $p['last']=$last;  
  36.         $p['next']=$next;  
  37.         $p['num_page']=$num_page;  
  38.         $p['price']=$price;  
  39.         return $this->renderPartial('search',['list'=>$list,'page'=>$p]);  
  40.     }  
  41. }  
搜索后展示页面
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Document</title>  
  6. </head>  
  7. <body>  
  8.     <table border="center">  
  9.         <tr>  
  10.             <td>姓名</td>  
  11.             <td>电话</td>  
  12.             <td>地址</td>  
  13.         </tr>  
  14.         <?php foreach ($list as $key => $v): ?>  
  15.             <tr>  
  16.                 <td><?= $v['username']?></td>  
  17.                 <td><?= $v['userphone']?></td>  
  18.                 <td><?= $v['usersite']?></td>  
  19.             </tr>  
  20.         <?php endforeach ?>  
  21.         <tr>  
  22.             <td><a href="?r=sp/search&page=1&price=<?= $page['price']?>">首页</a></td>  
  23.             <td><a href="?r=sp/search&page=<?= $page['last']?>&price=<?= $page['price']?>">上页</a></td>  
  24.             <td><a href="?r=sp/search&page=<?= $page['next']?>&price=<?= $page['price']?>">下页</a></td>  
  25.             <td><a href="?r=sp/search&page=<?= $page['num_page']?>&price=<?= $page['price']?>">尾页</a></td>  
  26.         </tr>  
  27.     </table>  
  28. </body>  
  29. </html> 
0 0