php分页加搜索
<?php /** * Created by PhpStorm. * User: Danz * Date: 2017/3/28 * Time: 9:46 */ include_once "conn.php";//引入数据连接
//拼接SQL语句 $wherelist=array(); $urlist=array(); if(!empty($_GET['selecttype'])&&$_GET["selecttype"]!="0") { $wherelist[]="menu_message.typeid=".$_GET['selecttype']; $urllist[]="selecttype=".$_GET['selecttype']; } if(!empty($_GET['search'])) { $wherelist[]=" name like '%".$_GET['search']."%'"; $urllist[]="search=".$_GET['search']; } $where=""; if(count($wherelist)>0) { $where=' and '.implode(' and ',$wherelist); $url='&'.implode('&',$urllist); } //分页的实现原理 //1.获取数据表中总记录数 $sql="select * from menu_message,menu_type where menu_message.typeid=menu_type.id $where "; $result=mysql_query($sql); $totalnum=mysql_num_rows($result); //每页显示条数 $pagesize=2; //总共有几页 $maxpage=ceil($totalnum/$pagesize); $page=isset($_GET['page'])?$_GET['page']:1; //page小于1的时候等于1 if($page <=1) { $page=1; }
//page大于最大页码时变成maxpage if($page>$maxpage) { $page=$maxpage; } //分页按钮 $strli=""; for($n=1;$n<=$maxpage;$n++){ $strli.="<li><a href=\"index2.php?page=".$n.$url."\">$n</a></li>"; } //拼接查询语句 $limit=" limit ".($page-1)*$pagesize.",$pagesize"; $sql1="select *,menu_message.id as mid from menu_type,menu_message where menu_message.typeid=menu_type.id {$where}{$limit}"; //$sql1="select * from news {$where} {$limit}"; $res=mysql_query($sql1); $str = ""; $str1=""; //输出分类选项 $resoption=mysql_query("SELECT * FROM `menu_type`"); while($rowoption=mysql_fetch_assoc($resoption)){ $str1.="<option value=\"".$rowoption['id']."\">".$rowoption['type']."</option>"; } //输出数据 while($row = mysql_fetch_assoc($res)){ $str.="<tr> <td>".$row['mid']."</td> <td>".$row['type']."</td> <td><img src=\"img/" . $row['pic'] . "\" class=\"img-responsive\" alt=\"Responsive image\"></td> <td>".$row['name']."</td> <td>".$row['price']."</td> <td>".$row['discribe']."</td> <td><a href=\"action.php?t=editmenu&mid=".$row['mid']."\">修改</a></td> <td><a href=\"action.php?t=delmenu&mid=".$row['mid']."\">删除</a></td> </tr>"; } ?> <link rel="stylesheet" href="css/bootstrap.css"> <style> img{ width: 300px; } .pagination{ margin:20px 35%; } .search-select{ height:34px; border-left: 0; border-right: 0; border-radius: 0; } .input-group-addon{ background-color: #fff; } </style> <a href="addmenu.php" class="btn btn-block btn-primary">添加食品</a> <form action="index2.php" method="get" name="searchtype"> <div class="input-group"> <input type="text" class="form-control" placeholder="Search for..." name="search"> <div class="input-group-btn"> <select class="btn btn-default search-select" name="selecttype" > <option value="0">请选择</option> <?php echo $str1;?> <span class="caret"></span> </select> </div><!-- /btn-group --> <span class="input-group-btn"> <input type="submit" class="btn btn-default">搜索</input> </span> </div> </form> <script> // function change(){ // document.searchtype.submit(); // } </script> <!-- /input-group --> <table class="table table-hover"> <thead> <tr> <td>id</td> <td>类型</td> <td>图片</td> <td>名称</td> <td>价格</td> <td>描述</td> <td>修改</td> <td>删除</td> </tr> </thead> <tbody> <?php echo $str ?> </tbody> </table> <nav aria-label="Page navigation"> <ul class="pagination pagination-lg"> <li> <a href="index2.php?page=<?php echo ($page-1).$url;?>" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <?php echo $strli;?> <li> <a href="index2.php?page=<?php echo ($page+1).$url;?>" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav>