search
HomeBackend DevelopmentPHP ProblemCan php arrays be paginated?

Can php arrays be paginated?

Sep 17, 2019 pm 03:40 PM
php

Regarding the paging function of arrays, the advantage of using arrays for paging is that it is convenient to perform joint multi-table queries. You only need to put the query results in the array.

Can php arrays be paginated?

#The following are the functions of array paging. The function page_array is used for paging arrays. The function show_array is used for the operation and display of paging functions. They need to be used together. The two functions are connected through the global variable $countpage, which is used to track the total number of pages. (Recommended learning: PHP programming from entry to proficiency)

<?php  
/** 
 * 数组分页函数  核心函数  array_slice 
 * 用此函数之前要先将数据库里面的所有数据按一定的顺序查询出来存入数组中 
 * $count   每页多少条数据 
 * $page   当前第几页 
 * $array   查询出来的所有数组 
 * order 0 - 不变     1- 反序 
 */   
  
function page_array($count,$page,$array,$order){  
    global $countpage; #定全局变量  
    $page=(empty($page))?&#39;1&#39;:$page; #判断当前页面是否为空 如果为空就表示为第一页面   
       $start=($page-1)*$count; #计算每次分页的开始位置  
    if($order==1){  
      $array=array_reverse($array);  
    }     
    $totals=count($array);    
    $countpage=ceil($totals/$count); #计算总页面数  
    $pagedata=array();  
    $pagedata=array_slice($array,$start,$count);  
    return $pagedata;  #返回查询数据  
}  
/** 
 * 分页及显示函数 
 * $countpage 全局变量,照写 
 * $url 当前url 
 */  
function show_array($countpage,$url){  
     $page=empty($_GET[&#39;page&#39;])?1:$_GET[&#39;page&#39;];  
     if($page > 1){  
        $uppage=$page-1;  
  
     }else{  
        $uppage=1;  
     }  
  
     if($page < $countpage){  
        $nextpage=$page+1;  
  
     }else{  
            $nextpage=$countpage;  
     }  
         
        $str=&#39;<div style="border:1px; width:300px; height:30px; color:#9999CC">&#39;;  
    $str.="<span>共  {$countpage}  页 / 第 {$page} 页</span>";  
    $str.="<span><a href=&#39;$url?page=1&#39;>   首页  </a></span>";  
    $str.="<span><a href=&#39;$url?page={$uppage}&#39;> 上一页  </a></span>";  
    $str.="<span><a href=&#39;$url?page={$nextpage}&#39;>下一页  </a></span>";  
    $str.="<span><a href=&#39;$url?page={$countpage}&#39;>尾页  </a></span>";  
    $str.=&#39;</div>&#39;;  
    return $str;  
}  
?>

The above is the detailed content of Can php arrays be paginated?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),