PHP基于数组实现的分页函数实例,php数组分页实例
分页功能是PHP程序设计中非常常见的功能,不同于以往的,今天本文介绍的是PHP基于数组实现的分页函数。
关于数组的分页函数,用数组进行分页的好处是可以方便的进行联合多表查询,只需要将查询的结果放在数组中就可以了以下是数组分页的函数,函数page_array用于数组的分页,函数show_array用于分页函数的操作及显示,需要配合使用。两个函数通过全局变量$countpage发生联系,此变量用于跟踪总页码数。
具体实例代码如下:
<?php /** * 数组分页函数 核心函数 array_slice * 用此函数之前要先将数据库里面的所有数据按一定的顺序查询出来存入数组中 * $count 每页多少条数据 * $page 当前第几页 * $array 查询出来的所有数组 * order 0 - 不变 1- 反序 */ function page_array($count,$page,$array,$order){ global $countpage; #定全局变量 $page=(empty($page))?'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'])?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?page=1'> 首页 </a></span>"; $str.="<span><a href='$url?page={$uppage}'> 上一页 </a></span>"; $str.="<span><a href='$url?page={$nextpage}'>下一页 </a></span>"; $str.="<span><a href='$url?page={$countpage}'>尾页 </a></span>"; $str.='</div>'; return $str; } ?>
希望本文所述实例对大家的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. 文件上传表单
可以如下建立一个特殊的表单来支持文件上传:

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

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