欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

php原生分页

程序员文章站 2022-05-19 18:21:04
...

php原生分页

<?php   
function con(){
    $serverHost = "127.0.0.1";     //数据库地址
    $user = 'root';                      //数据库用户名
    $password = 'root';                 //数据库密码
    $dbName = "webbase";
    header("content-type:text/html;charset=utf-8");
    $link=mysqli_connect($serverHost,$user,$password,$dbName);

    // 检查连接
    if (!$link) {
        die("连接错误: " . mysqli_connect_error());
    }
    mysqli_query($link,'set names utf8');//设置字符集
    return $link;
}
//显示总页数的函数
function taotalCount($sql){
    $link=con();
    $list = mysqli_query($link, $sql);
    $obj = mysqli_fetch_object($list);
    mysqli_close($link);
    return $obj->total;
}
//分页数据
function data($sql){
    $link=con();
    $result  = mysqli_query($link,$sql); //获取所有的数据
    $arr=array();
    while($t_result = mysqli_fetch_array($result,MYSQLI_ASSOC)){
        $arr[]=$t_result;
    }
    mysqli_free_result($result);
    mysqli_close($link);
    return $arr;
}
function page($count,$listRows,$curPage,$rowsmaxPage =null,$search=null){
    $search = isset($search)?$search:"";
    $rowsmaxPage = isset($rowsmaxPage)?$rowsmaxPage:10;
    $totalpage =ceil($count/$listRows);
    //存储页码字符串
    $pageNumString = "";
    if($curPage <=($rowsmaxPage/2)){
        $begin =1;
        $end = $totalpage>=$rowsmaxPage?$rowsmaxPage:$totalpage;
    }else{
        $end = $curPage +($rowsmaxPage/2)>$totalpage?$totalpage:$curPage +($rowsmaxPage/2);
        $begin =$end -$rowsmaxPage-1<=1?1:$end -$rowsmaxPage-1;
    }
    //实现上一页
    $prev = $curPage -1<=1?1:$curPage -1;
    $pageNumString .="<li><p>".$count." Record ".$curPage."/".$totalpage." PAGE</li>";
    $pageNumString .="<li><a href='".$_SERVER['PHP_SELF']."?curPage=1&search=".$search."'>首页</a></li>";
    $pageNumString .="<li><a href='".$_SERVER['PHP_SELF']."?curPage=$prev&search=$search'>&laquo;</a></li>";

    //根据起始页与终止页将当前页面的页码显示出来
    for($i=$begin;$i<=$end;$i++){
        //使用if实现高亮显示当前点击的页码
        if($curPage == $i){
            $pageNumString .= "<li><a class=\"active\" href='".$_SERVER['PHP_SELF']."?curPage=$i&search=$search'>$i</a></li>";
        }else{
            $pageNumString .= "<li><a href='".$_SERVER['PHP_SELF']."?curPage=$i&search=$search'>$i</a></li>";
        }
    }
    //实现下一页
    $next = $curPage +1 >=$totalpage?$totalpage:$curPage +1;
    $pageNumString .="<li><a href='".$_SERVER['PHP_SELF']."?curPage=$next&search=$search'>&raquo;</a></li>";
    $pageNumString .="<li><a href='".$_SERVER['PHP_SELF']."?curPage=$totalpage&search=$search'>尾页</a></li>";
    $pageNumString .="<li><input type='hidden' value='$totalpage' id='max'>
                           <input type='text' class='jump'  value=''>&nbsp;
                           <a onclick='javascript:jumpPage();' style='margin-left: 3px;'>GO</a> 
                        
                      </li>";
    return $pageNumString;
}
//定义一个最大的页码数
$rowsmaxPage = 6;
// 默认列表每页显示行数
$listRows = 10;
//总记录数SQL
isset($_REQUEST["search"])?($search= $_REQUEST["search"]):$search="";
$where = "1=1 ";
$where .=" and concat_ws('',p.mCode,p.mDesc1,g.mPrice) like '%".trim($search)."%' ";
$countsql = "SELECT count(*) as total FROM t_product as p  
             LEFT JOIN t_product_pricegroup as g on p.T_Product_ID=g.T_Product_ID and g.mItem=1 where $where";
$count = taotalCount($countsql);
//当前页
$curPage = isset($_GET['curPage'])?($_GET['curPage']>ceil($count/$listRows)?ceil($count/$listRows):$_GET['curPage']):1;
//分页数据
$offset = ($curPage-1)*$listRows;
$sql = "SELECT mCode,mDesc1,mPrice FROM t_product as p  
            LEFT JOIN t_product_pricegroup as g on p.T_Product_ID=g.T_Product_ID and g.mItem=1 where $where
            limit $offset,$listRows";
$data = data($sql);
//分页样式
$page = page($count,$listRows,$curPage,$rowsmaxPage,$search);
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>食品维护</title>
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>
    <meta content="yes" name="apple-mobile-web-app-capable"/>
    <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
    <meta content="telephone=no" name="format-detection"/>
    <link rel="stylesheet" href="css/aaa@qq.com@hash"/>
    <link rel="stylesheet" href="css/yui.css"/>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/ydui.js"></script>
</head>
<script >
    function jumpPage(){
        var curPage = document.getElementsByClassName("jump")[0].value;
        var maxpage = document.getElementById("max").value;
        if(isNaN(curPage) || Number(curPage)<1){
            curPage = 1;
        }
        if(Number(curPage)>Number(maxpage)){
            curPage = maxpage;
            if(curPage==0){
                curPage=1;
            }
        }
        window.location="<?php echo $_SERVER['PHP_SELF'];?>?curPage="+curPage+"&search=<?php echo $search;?>";
    }
    function doSubmitForm() {
        var form = document.getElementById('form');
        form.submit();
    }
</script>
<style>
    .page {
        text-align: center;
    }
    .page li {
        display: inline;
    }
    .page li a {
        color: black;
        padding: 6px 8px;
        text-decoration: none;
        transition: background-color .3s;
        border: 1px solid #ddd;
        margin: 2px;
        border-radius: 2px;
    }
    ul.page li .active {
        background-color: #4CAF50;
        color: white;
        border: 1px solid #4CAF50;
        display: inline-flex;
    }
    ul.page li a:hover:not(.active) {
        background-color: #ddd;
    }
    .jump{
        width: 30px;
        margin-left: 5px;
        height: 27px;
        border: 1px solid #ccc;
        border-radius: 3px;
    }
    .jump:focus{
        border: solid 1px #0f9ae0;
    }

</style>
<body>
<section class="g-flexview">
    <header class="m-navbar">
        <div class="navbar-center"><span class="navbar-title">食品</span></div>
    </header>
    <form action="index.php" method="post" id="form">
        <div style="position: relative;top: 8px;z-index: 5000;height: 43px;" >
            <div class="input-group" style="width: 200px;float: right;right: 0;">
                <input type="text" class="form-control" value="<?php echo $search;?>" name="search">
                <span class="input-group-addon" style="background: #5cb85c;"onclick='doSubmitForm()'>search</span>
            </div>
        </div>
    </form>
   <section class="g-scrollview">
        <div class="m-cell demo-small-pitch">
            <?php  foreach ($data as $k=>$vo ){?>
            <div class="cell-item" style="width: 100%">
                <div class="cell-right cell-arrow" >
                    <div style="width: 25%;text-align: left;"><?PHP echo $vo["mCode"] ?></div>
                    <div style="width: 50%;text-align: left;"><?php echo $vo["mDesc1"]?></div>
                    <div style="width: 22%;text-align: left;" ><?php echo $vo["mPrice"]?></div>
                </div>
            </div>
            <?php }?>
        </div>
        <ul class="page">
           <?php echo $page;?>
        </ul>
    </section>

</section>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</body>
</html>