search
Homephp教程php手册PHP基于数组实现的分页函数实例,php数组分页实例

PHP基于数组实现的分页函数实例,php数组分页实例

分页功能是PHP程序设计中非常常见的功能,不同于以往的,今天本文介绍的是PHP基于数组实现的分页函数。

关于数组的分页函数,用数组进行分页的好处是可以方便的进行联合多表查询,只需要将查询的结果放在数组中就可以了以下是数组分页的函数,函数page_array用于数组的分页,函数show_array用于分页函数的操作及显示,需要配合使用。两个函数通过全局变量$countpage发生联系,此变量用于跟踪总页码数。

具体实例代码如下:

<&#63;php
/**
 * 数组分页函数 核心函数 array_slice
 * 用此函数之前要先将数据库里面的所有数据按一定的顺序查询出来存入数组中
 * $count  每页多少条数据
 * $page  当前第几页
 * $array  查询出来的所有数组
 * order 0 - 不变   1- 反序
 */ 
function page_array($count,$page,$array,$order){
  global $countpage; #定全局变量
  $page=(empty($page))&#63;'1':$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['page'])&#63;1:$_GET['page'];
 if($page > 1){
   $uppage=$page-1;
 }else{
  $uppage=1;
 }
 if($page < $countpage){
   $nextpage=$page+1;

 }else{
   $nextpage=$countpage;
 }
    $str='<div style="border:1px; width:300px; height:30px; color:#9999CC">';
 $str.="<span>共 {$countpage} 页 / 第 {$page} 页</span>";
 $str.="<span><a href='$url&#63;page=1'>  首页 </a></span>";
 $str.="<span><a href='$url&#63;page={$uppage}'> 上一页 </a></span>";
 $str.="<span><a href='$url&#63;page={$nextpage}'>下一页 </a></span>";
 $str.="<span><a href='$url&#63;page={$countpage}'>尾页 </a></span>";
 $str.='</div>';
 return $str;
}
&#63;>

希望本文所述实例对大家的PHP程序设计能起到一定的借鉴作用。

怎用PHP数组实现文件分页


你没有>,里面的39章,专门一章来讲文件上传,非常详细,最简单的部分如下:

POST 方法上传

本特性可以使用户上传文本和二进制文件。用 PHP 的认证和文件操作函数,可以完全控制允许哪些人上传以及文件上传后怎样处理。 

PHP 能够接受任何来自符合 RFC-1867 标准的浏览器(包括 Netscape Navigator 3 及更高版本,打了补丁的 Microsoft Internet Explorer 3 或者更高版本)上传的文件。 

相关的设置: 请参阅 php.ini 的 file_uploads,upload_max_filesize,upload_tmp_dirpost_max_size 以及 max_input_time 设置选项。 

请注意 PHP 也支持 PUT 方法的文件上传,Netscape Composer 和 W3C 的 Amaya 客户端使用这种方法。请参阅对 PUT 方法的支持以获取更多信息。 

例 39.1. 文件上传表单

可以如下建立一个特殊的表单来支持文件上传: 

    



    
    
    
    Send this file: >
 

php怎实现查询oracle然后分页显示,最好是一个类加实例,了

oracle 实现分页要用到rownum 代码如下
select * from (select t.* rownum row_id form (select * from 表名) t ) where row_id>=1 and row_id这条SQL语句就可以实现分页查询,当然光有SQL还是不行,用以下的PHP 函数就可实现分页了。
/*分页函数*/
function page($page,$total,$phpfile,$pagesize=10,$pagelen=10,$str='')
{
$pagecode = '';//定义变量,存放分页生成的HTML
$page = intval($page);//避免非数字页码
$total = intval($total);//保证总记录数值类型正确
if(!$total) return array();//总记录数为零返回空数组
$pages = ceil($total/$pagesize);//计算总分页
//处理页码合法性
if($pageif($page>$pages) $page = $pages;
//计算查询偏移量
$offset = $pagesize*($page-1)+1;
//页码范围计算
$init = 1;//起始页码数
$max = $pages;//结束页码数
$pagelen = ($pagelen%2)?$pagelen:$pagelen+1;//页码个数
$pageoffset = ($pagelen-1)/2;//页码个数左右偏移量
//生成html
$pagecode='

';
$pagecode.="共".$total."条信息 第".$page."页/共".$pages."页\n";//第几页,共几页
//如果是第一页,则不显示第一页和上一页的连接
if($page!=1){
$pagecode.=" 首页 ";//第一页
$pagecode.="
上页 ";//上一页
}
//分页数大于页码个数时可以偏移
if($pages>$pagelen)
{
//如果当前页小于等于左偏移
if($page{
$init=1;
$max = $pagelen;
}else
{
//如......余下全文>>
来自UC浏览器  
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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),