• 分页Model


    <?php
    namespace AdminModel;
    
    use ThinkModel;
    
    <?php
    class PageModel extends Model
    {
        /*
         *此函数分页函数。传入表名和当前页,返回 总页数,总条数 底标的分页的数组
        */
        public function index($tableName, $page, $where, $prefix_page, $sql = '', $add_one_flag = false)
        {
            $tables = M($tableName);
            //第一返回总条数
            if ($sql) {
                $tmp = $tables->query($sql);
                $data['count'] = $tmp[0]['count_num'];
            } else {
                if ($where) {
                    $data['count'] = $tables->where($where)->count();
                } else {
                    $data['count'] = $tables->count();
                }
            }
            if ($add_one_flag === true) {
                $data['count'] += 1;
            }
    
            $page_step = C('PAGE_NORMAL_COUNT');
            //第二返回总页数
            $data['page_all'] = (int) ceil($data['count'] / $page_step);
    
            //第三返回底标的分页数组
            $page_start = $page - 2;
            if ($data['page_all'] > 5) {
                if ($page_start <= 0) {
                    $page_start = 1;
                    $page_end = 5;
                } else {
                    $page_end = $page + 2;
                    if ($page_end >= $data['page_all']) {
                        $page_end = $data['page_all'];
                        $page_start = $page_end - 4;
                    }
                }
            } else {
                $page_start = 1;
                $page_end = $data['page_all'];
            }
            $data['page_start'] = $page_start;
            $data['page_end'] = $page_end;
            //第四返回当前页
            $data['page'] = $page;
            //第五 返回上一页和下一页
            $prev_page = $page - 1;
            if ($prev_page <= 0) {
                $prev_page = 1;
            }
            $next_page = $page + 1;
            if ($next_page >= $data['page_all']) {
                $next_page = $data['page_all'];
            }
    
            $data['prev_page'] = $prev_page;
            $data['next_page'] = $next_page;
            $data['prefix_page'] = $prefix_page;
    
            // 自动获取参数,避免分页丢失
            $query_string = $_SERVER['QUERY_STRING'];
            // setlog($query_string);
            if ($query_string) {
                if (strpos($query_string, 'page') === false) {
                    // setlog('111');
                    $data['tail'] = '&' . $query_string;
                } else {
                    // setlog('222');
                    // 去掉page
                    $pos = strpos($query_string, '&');
                    if ($pos === false) {
                        $data['tail'] = '';
                        // setlog('333');
                    } else {
                        // setlog('444');
                        $my_query_string = substr($query_string, $pos);
                        $arr = explode('&',$my_query_string);
                        $new_query_string = '';
                        foreach($arr as $k => &$v) {
                            if (strpos($v, 'page') === false && $v) {
                                $new_query_string .= '&'.$v;
                            }
                        }
                        // setlog($new_query_string);
                        $data['tail'] = $new_query_string;
                    }
                }
            }
            return $data;
        }
    }
    
    

    php

    $allPage = $model->index('room', $page, $where);
    $this->assign('allPage', $allPage);
    

    html

    <div class="layui-card-body ">
        <div class="page">
            <div>
                <a href="__ACTION__?page=1{$allPage['prefix_page']}{$allPage.tail}">首页</a>
                <a class="prev" href="__ACTION__?page={$allPage['prev_page']}{$allPage['prefix_page']}{$allPage.tail}">上一页</a>
                <php> for ($i = $allPage['page_start']; $i <= $allPage['page_end']; $i++) {
                    if ($i == $allPage['page']) { </php>
                <span class="current">
                    <php> echo $i; </php>
                </span>
                <php> } else { </php>
                <a class="num" href="__ACTION__?page=<php>echo $i;</php>{$allPage['prefix_page']}{$allPage.tail}">
                    <php> echo $i; </php>
                </a>
                <?php
                         }
                  }
            ?>
                <a class="next" href="__ACTION__?page={$allPage['next_page']}{$allPage['prefix_page']}{$allPage.tail}">下一页</a>
                <a href="__ACTION__?page={$allPage['page_all']}{$allPage['prefix_page']}{$allPage.tail}">尾页</a>
            </div>
        </div>
    </div>
    
  • 相关阅读:
    C# Lambda表达式
    .NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy
    SQL中的case when then else end用法
    WPF gif图片不动问题解决
    async(C# 参考)
    File类 ReadAllBytes() ReadAllLines() ReadAllText()
    二维码生成的常用数据格式
    在chrome console加入jquery库
    Reflector反编译WinForm程序重建项目资源和本地资源
    使用Settings.settings存储用户的个性化配置
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/11799779.html
Copyright © 2020-2023  润新知