PHP - 搜索、分页

classify.php 核心代码

<?php
    $keyword = isset($_REQUEST['keyword'])?$_REQUEST['keyword']:"";
?>
<div>
    <form action="classify.php" method="get" id="search-form">
        <input type="text" name="keyword" value="<?php echo $keyword;?>" id="keywords"/>
        <input type="button" value="搜索" onclick="search()"/>
    </form>
</div>
<script>
    function search(){
        $('#search-form').submit();
    }
</script>
<?php
    $uuid = $_COOKIE['uuid'];
    //查询语句
    $select = " SELECT * FROM classify WHERE uuid = '".$uuid."' ";
    //数据总数
    $count = " SELECT count(*) FROM classify WHERE uuid = '".$uuid."' ";
    if($keyword != ""){
        $select .= " AND name like '%".$keyword."%' ";
        $count .= " AND name like '%".$keyword."%' ";
    }
    $count = mysql_query($count);
    $count = mysql_fetch_assoc($count);
    $count = $count['count(*)'];
    //获取页面数
    $page = isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
    //每页数量
    $pagesize = 10;
    //总页数
    $pages = ceil($count/$pagesize);
    if($page < 1){
        $page = 1;
    }
    if($page > $pages){
        $page = $pages;
    }
    //排序
    $order = " ORDER BY id DESC ";
    //偏移量
    $offset = ($page - 1)*$pagesize;
    $limit = " LIMIT ".$offset.", ".$pagesize;
    //最后查询语句
    $select .= $order.$limit;
    //查询
    $classify = mysql_query($select);
    if($classify){
        while ($row = mysql_fetch_assoc($classify)){
            $result[] = $row;
        }
    }
    if(empty($result)){
        echo "未查询到信息";
    }else{
        foreach ($result as $key => $val){
            echo $val['name'];
        }
    }
?>
<a>共<?php echo $count;?>条数据</a>
<a>共<?php echo $pages;?>页</a>
<a>当前第<?php echo $page;?>页</a>
<a onclick="jump(1)">首页</a>
<a onclick="jump( <?php echo $page;?> - 1 )">上一页</a>
<a onclick="jump( <?php echo $page;?> + 1 )">下一页</a>
<a onclick="jump( <?php echo $pages;?> )">尾页</a>
<input type="text" id="Pnum" />
<input type="button" value="GO" onclick="go();" />
<script>
    var keyword;
    $(document).ready(function(){
        keyword = $('#keywords').val();
    });
    function jump(page){
        window.location.href = "?keyword=" + keyword + "&page=" + page;
    }
    function go(){
        var goPage = $('#Pnum').val();
        window.location.href = "?keyword=" + keyword + "&page=" + goPage;
    }
</script>
返回文章列表 文章二维码 打赏
本页链接的二维码
打赏二维码
期待你的评论

2 条评论
  1. xiaomo 黄金   xiaomo  

    围观大佬@(滑稽)

    1. 小尾巴 王者   小尾巴   博主
      @ xiaomo

      做人呐,最重要的就是低调 ๑乛◡乛๑