페이징의 원리는 무엇입니까?

不言
풀어 주다: 2023-03-28 16:26:01
원래의
2782명이 탐색했습니다.

데이터 페이징에는 다음 조건이 필요합니다.

1. 페이징에 참여하는 총 항목 수 [$msg_count], 이 값은 데이터베이스 쿼리를 통해 얻을 수 있습니다.

2. 각 페이지에 표시되는 항목 수 [$pagesize ], 이 값은 사용자가 정의합니다.

3. 현재 페이지의 페이지 번호[$page], 이 값은 주소 표시줄을 통해 전송 및 수신됩니다.

4. 총 페이지 수는 다음에서 계산할 수 있습니다. 위의 정보 [$pagecount], 여기서 사용해야 합니다 ceil();

【$pagecount = ceil($msg_count/$pagesize);】

5. 데이터베이스 쿼리는 SQL 문에서 [limit]를 사용하여 실현합니다. 데이터 변경:

예:

조건 제한 $startnum, $pagesize;

및 $startnum = ($page-1)*$pagesize;

예:

/**
 * 取得上次的过滤条件
 * @param   string  $param_str  参数字符串,由list函数的参数组成
 * @return  如果有,返回array('filter' => $filter, 'sql' => $sql);否则返回false
 */
function get_filter($param_str = '')
{
    $filterfile = basename(PHP_SELF, '.php');//string basename ( string $path [, string $suffix ] )                             返回路径中的文件名部分如果文件名是以 suffix 结束的,那这一部分也会被去掉。 
    if ($param_str)
    {
        $filterfile .= $param_str;
    }
    if (isset($_GET['uselastfilter']) && isset($_COOKIE['ECSCP']['lastfilterfile'])
        && $_COOKIE['ECSCP']['lastfilterfile'] == sprintf('%X', crc32($filterfile)))
    {
        return array(
            'filter' => unserialize(urldecode($_COOKIE['ECSCP']['lastfilter'])),
            'sql'    => base64_decode($_COOKIE['ECSCP']['lastfiltersql'])
        );
    }
    else
    {
        return false;
    }
}
로그인 후 복사
/**
 * 保存过滤条件
 * @param   array   $filter     过滤条件
 * @param   string  $sql        查询语句
 * @param   string  $param_str  参数字符串,由list函数的参数组成
 */
function set_filter($filter, $sql, $param_str = '')
{
    $filterfile = basename(PHP_SELF, '.php');
    if ($param_str)
    {
        $filterfile .= $param_str;
    }
    setcookie('ECSCP[lastfilterfile]', sprintf('%X', crc32($filterfile)), time() + 600);
    setcookie('ECSCP[lastfilter]',     urlencode(serialize($filter)), time() + 600);
    setcookie('ECSCP[lastfiltersql]',  base64_encode($sql), time() + 600);
}
로그인 후 복사
/**
 * 供货商资源管理
 * @param    bool    $is_pagtion
 * @return    array   $arr
 */
function suppliers_resource_manage($is_pagtion=true)
{
    global $db,$ecs;
    $result = get_filter();
    if ($result === false)
    {
        $aiax = isset($_GET['is_ajax']) ? $_GET['is_ajax'] : 0;
        /* 过滤信息 */
        $filter['sort_by'] = empty($_REQUEST['sort_by']) ? 'r.resource_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['resource_id'] = empty($_REQUEST['resource_id'])  ? '' : $_REQUEST['resource_id'];
        $filter['admin_name'] = empty($_REQUEST['admin_name'])  ? '' : trim($_REQUEST['admin_name']);
        $filter['resource_name'] = empty($_REQUEST['resource_name'])  ? '' : trim($_REQUEST['resource_name']);
        $filter['admin_id']  = isset($_REQUEST['admin_id'])?intval($_REQUEST['admin_id']):'-1';
        $filter['resource_type'] = empty($_REQUEST['resource_type']) ? '-1' : intval($_REQUEST['resource_type']);
        $filter['resource_status'] = empty($_REQUEST['resource_status']) ? '-1' : intval($_REQUEST['resource_status']);
        $filter['resource_rank'] = empty($_REQUEST['resource_rank']) ? '-1' : intval($_REQUEST['resource_rank']);
        $filter['resource_key'] = empty($_REQUEST['resource_key'])  ? '' : trim($_REQUEST['resource_key']);
        $filter['menuid'] = 7; 
        $where = 'WHERE 1 ';
        /* 分页大小 */
        $filter[&#39;page&#39;] = empty($_REQUEST[&#39;page&#39;]) || (intval($_REQUEST[&#39;page&#39;]) <= 0) ? 1 : intval($_REQUEST[&#39;page&#39;]);

        if (isset($_REQUEST[&#39;page_size&#39;]) && intval($_REQUEST[&#39;page_size&#39;]) > 0)
        {
            $filter[&#39;page_size&#39;] = intval($_REQUEST[&#39;page_size&#39;]);
        }
        elseif (isset($_COOKIE[&#39;ECSCP&#39;][&#39;page_size&#39;]) && intval($_COOKIE[&#39;ECSCP&#39;][&#39;page_size&#39;]) > 0)
        {
            $filter[&#39;page_size&#39;] = intval($_COOKIE[&#39;ECSCP&#39;][&#39;page_size&#39;]);
        }
        else

        {
            $filter[&#39;page_size&#39;] = 15;
        }
        
        if(!empty($filter[&#39;resource_id&#39;]))
        {
            $resource_id = $filter[&#39;resource_id&#39;] == -1 ? 0 : $filter[&#39;resource_id&#39;];
            $where .= " and r.resource_id=&#39;".$resource_id."&#39;";
        }
        if($filter[&#39;resource_name&#39;]){
            $where .= " AND r.resource_name like &#39;%".mysql_like_quote($filter[&#39;resource_name&#39;])."%&#39; ";
        }
        if($filter[&#39;admin_id&#39;] > -1)
        {
            $where .= " AND r.admin_id = &#39;".$filter[&#39;admin_id&#39;]."&#39; ";
        }
        if ($filter[&#39;resource_type&#39;] > -1) 
        {
            $where .= " AND r.resource_type = &#39;" . $filter[&#39;resource_type&#39;] . "&#39;";
        }
        if ($filter[&#39;resource_status&#39;] > -1) 
        {
            $where .= " AND r.resource_status = &#39;" . $filter[&#39;resource_status&#39;] . "&#39;";
        }
        if ($filter[&#39;resource_rank&#39;] > -1) 
        {
            $where .= " AND r.resource_rank = &#39;" . $filter[&#39;resource_rank&#39;] . "&#39;";
        }
        if($filter[&#39;resource_key&#39;]){
            $where .= " AND r.remark like &#39;%".mysql_like_quote($filter[&#39;resource_key&#39;])."%&#39; ";
        }
        /* 记录总数 */
        $sql = "SELECT COUNT(r.resource_id) FROM " . $ecs->table(&#39;suppliers_resource&#39;)." as r 
                    LEFT JOIN ".$ecs->table(&#39;admin_user&#39;) . " as u
                        ON u.user_id = r.admin_id  " . $where;
        $filter[&#39;record_count&#39;]   = $db->getOne($sql);
        $filter[&#39;page_count&#39;]     = $filter[&#39;record_count&#39;] > 0 ? ceil($filter[&#39;record_count&#39;] / $filter[&#39;page_size&#39;]) : 1;
        /* 查询 */
        $sql = "SELECT r.resource_id, u.user_name as admin_name, r.resource_name, r.resource_link, r.resource_type, r.resource_status, r.resource_rank, r.remark, r.add_time FROM " . $ecs->table(&#39;suppliers_resource&#39;) . " as r 
                    LEFT JOIN " . $ecs->table(&#39;admin_user&#39;) . " as u ON u.user_id = r.admin_id  " . $where;

        $sort_by  = $filter[&#39;sort_by&#39;];
        $sort_order = $filter[&#39;sort_order&#39;];
        $sql .="GROUP BY r.resource_id ORDER BY " . $sort_by . " " . $sort_order;
        if($is_pagtion)
        {
            $sql .=" LIMIT ".($filter[&#39;page&#39;] - 1)*$filter[&#39;page_size&#39;].",".$filter[&#39;page_size&#39;];
        }
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result[&#39;sql&#39;];
        $filter = $result[&#39;filter&#39;];
    }
    $query=$sql;

    $row = $db->getAll($sql);
    /* 格式话数据 */
    foreach ($row AS $key => $value)
    {   
        if ($row[$key][&#39;resource_type&#39;] == 1) {
            $row[$key][&#39;resource_type&#39;] = &#39;中模&#39;;
        }elseif ($row[$key][&#39;resource_type&#39;] == 2) {
            $row[$key][&#39;resource_type&#39;] = &#39;泳装&#39;;
        }elseif ($row[$key][&#39;resource_type&#39;] == 3) {
            $row[$key][&#39;resource_type&#39;] = &#39;阿里&#39;;
        }elseif ($row[$key][&#39;resource_type&#39;] == 2) {
            $row[$key][&#39;resource_type&#39;] = &#39;17网&#39;;
        }
        if ($row[$key][&#39;resource_status&#39;] == 1) {
            $row[$key][&#39;resource_status&#39;] = &#39;已审核&#39;;
        }elseif ($row[$key][&#39;resource_status&#39;] == 2) {
            $row[$key][&#39;resource_status&#39;] = &#39;已弃用&#39;;
        }
        if ($row[$key][&#39;resource_rank&#39;] == 1) 
        {
            $row[$key][&#39;resource_rank&#39;] = &#39;A&#39;;
        }elseif ($row[$key][&#39;resource_rank&#39;] == 2) {
            $row[$key][&#39;resource_rank&#39;] = &#39;B&#39;;
        }elseif ($row[$key][&#39;resource_rank&#39;] == 3) {
            $row[$key][&#39;resource_rank&#39;] = &#39;C&#39;;
        }
        if(strpos($row[$key][&#39;resource_link&#39;], &#39;http://&#39;) === false)
        {
            if (strpos($row[$key][&#39;resource_link&#39;], &#39;https://&#39;) === false) 
            {
                $row[$key][&#39;resource_link&#39;] = substr_replace($row[$key][&#39;resource_link&#39;], &#39;http://&#39;, 0, 0);
            }
        }
    }
    $arr = array(&#39;result&#39; => $row, &#39;filter&#39; => $filter, &#39;page_count&#39; => $filter[&#39;page_count&#39;], &#39;record_count&#39; => $filter[&#39;record_count&#39;],&#39;query&#39;=>$query);
    return $arr;
}
로그인 후 복사

관련 권장 사항:

php의 페이징 원리 예

PHP 페이징 원리에 대한 자세한 설명

위 내용은 페이징의 원리는 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!