一个实例php mysql模板分页类
<?php教程ajax只支持utf-8格式,不能支持gb2312编码格式,所以经常遇到gb2312的编码的程序使用ajax就出现乱码,刚找到一种解决方案是
/*
* 模板分页类,源于easp教程的数据库教程分页方法,算是easp分页的的php独立版
* 支持动态和静态分页方式
* * page([总记录数=1],[分页大小=20],[当前页=1],[显示页数=6],[分页参数='page'],[分页链接=当前页面],[是否静态=false])
* 动态:
* 简单用法:
* $page = new page(50);
* $page->setpager('zjj','<div class="newpager">共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
* echo $page->getpager('zjj');
* 全参数用法:
* $page = new page(50,20,1,6,'page','prrr.php',false);
* $page->setpager('zjj','<div class="newpager">共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
* echo $page->getpager('zjj');
* 静态:
* $page = new page(50,20,1,6,'page','prrr{page}.html',true);
* $page->setpager('zjj','<div class="newpager">共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
* echo $page->getpager('zjj');
*/
class page {
private $page_size; //每页显示的条目数
private $total_size; //总条目数
private $current_page; //当前被选中的页
private $sub_pages; //每次显示的页数
private $total_pages; //总页数
private $page_tpl = array (); // 分页模板
private $pageparam;
private $pagelink;
private $static;
function __construct($total_size = 1, $page_size = 20, $current_page = 1, $sub_pages = 6, $pageparam = 'page', $pagelink = '', $static = false) {
$this->page_size = intval ( $page_size );
$this->total_size = intval ( $total_size );
if (! $current_page) {
$this->current_page = 1;
} else {
$this->current_page = intval ( $current_page );
}
$this->total_pages = ceil ( $total_size / $page_size );
$this->sub_pages = intval ( $sub_pages );
$this->pageparam = $pageparam;
$this->pagelink = (empty ( $pagelink ) ? $_server ["php_self"] : $pagelink);
$this->static = $static;
$this->page_tpl ['default'] = array ('tpl' => '<div class="pager">{first}{prev}{liststart}{list}{listend}{next}{last} 跳转到{jump}页</div>', 'config' => array () );
}
public function __set($param, $value) {
$this->$param = $value;
}
public function __get($param) {
return $this->$param;
}
/*
__destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
*/
function __destruct() {
unset ( $page_size ); //每页显示的条目数
unset ( $total_size ); //总条目数
unset ( $current_page ); //当前被选中的页
unset ( $sub_pages ); //每次显示的页数
unset ( $total_pages ); //总页数
unset ( $page_tpl ); // 分页模板
unset ( $pageparam ); //分页参数,默认page
unset ( $pagelink );
unset ( $static );
}
private function urlparameters($url = array()) {
foreach ( $url as $key => $val ) {
if ($key != $this->pageparam)
$arg [] = $key . '=' . $val;
}
$arg [] = $this->pageparam . '=*';
if ($this->static)
return str_replace ( '{page}', '*', $this->pagelink );
else
return $this->pagelink . '?' . implode ( '&', $arg );
}
public function setpager($tpl_name = 'default', $tpl = '', $config = array()) {
if (empty ( $tpl ))
$tpl = $this->page_tpl ['default'] ['tpl'];
if (empty ( $config ))
$config = $this->page_tpl ['default'] ['config'];
$this->page_tpl [$tpl_name] = array ('tpl' => $tpl, 'config' => $config );
}
public function getpager($tpl_name = 'default') {
$this->getcurrentpage ();
return $this->pager ( $this->page_tpl [$tpl_name] );
}
public function getcurrentpage() {
$this->current_page = ($_get [$this->pageparam] <= intval ( $this->total_pages ) ? ($_get [$this->pageparam] < 1 ? 1 : $_get [$this->pageparam]) : intval ( $this->total_pages ));
}
public function pager($page_tpl = '') {
if (empty ( $page_tpl ))
$page_tpl = $this->page_tpl ['default'];
$cfg = array ('recordcount' => intval ( $this->total_size ), 'pageindex' => intval ( $this->current_page ), 'pagecount' => intval ( $this->total_pages ), 'pagesize' => intval ( $this->page_size ), 'listlong' => intval ( $this->sub_pages ), 'listsidelong' => 2, 'list' => '*', 'currentclass' => 'current', 'link' => $this->urlparameters ( $_get ), 'first' => '«', 'prev' => '‹', 'next' => '›', 'last' => '»', 'more' => '...', 'disabledclass' => 'disabled', 'jump' => 'input', 'jumpplus' => '', 'jumpaction' => '', 'jumplong' => 50 );
if (! empty ( $page_tpl ['config'] )) {
foreach ( $page_tpl ['config'] as $key => $val ) {
if (array_key_exists ( $key, $cfg ))
$cfg [$key] = $val;
}
}
$tmps教程tr = $page_tpl ['tpl'];
$pstart = $cfg ['pageindex'] - (($cfg ['listlong'] / 2) + ($cfg ['listlong'] % 2)) + 1;
$pend = $cfg ['pageindex'] + $cfg ['listlong'] / 2;
if ($pstart < 1) {
$pstart = 1;
$pend = $cfg ['listlong'];
}
if ($pend > $cfg ['pagecount']) {
$pstart = $cfg ['pagecount'] - $cfg ['listlong'] + 1;
$pend = $cfg ['pagecount'];
}
if ($pstart < 1)
$pstart = 1;
for($i = $pstart; $i <= $pend; $i ++) {
if ($i == $cfg ['pageindex'])
$plist .= '<span class="' . $cfg ['currentclass'] . '" >' . str_replace ( '*', $i, $cfg ['list'] ) . '</span> ';
else
$plist .= ' <a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '"> ' . str_replace ( '*', $i, $cfg ['list'] ) . '</a> ';
}
if ($cfg ['listsidelong'] > 0) {
if ($cfg ['listsidelong'] < $pstart) {
for($i = 1; $i <= $cfg ['listsidelong']; $i ++) {
$pliststart .= '<a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '">' . str_replace ( '*', $i, $cfg ['list'] ) . '</a> ';
}
$pliststart .= ($cfg ['listsidelong'] + 1) == $pstart ? '' : $cfg ['more'] . ' ';
} else {
if ($cfg ['listsidelong'] >= $pstart && $pstart > 1) {
for($i = 1; $i <= ($pstart - 1); $i ++) {
$pliststart .= '<a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '">' . str_replace ( '*', $i, $cfg ['list'] ) . '</a> ';
}
}
}
if (($cfg ['pagecount'] - $cfg ['listsidelong']) > $pend) {
$plistend = ' ' . $cfg ['more'] . $plistend;
for($i = (($cfg ['pagecount'] - $cfg ['listsidelong']) + 1); $i <= $cfg ['pagecount']; $i ++) {
$plistend .= ' <a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '"> ' . str_replace ( '*', $i, $cfg ['list'] ) . ' </a> ';
}
} else {
if (($cfg ['pagecount'] - $cfg ['listsidelong']) <= $pend && $pend < $cfg ['pagecount']) {
for($i = ($pend + 1); $i <= $cfg ['pagecount']; $i ++) {
$plistend .= ' <a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '"> ' . str_replace ( '*', $i, $cfg ['list'] ) . ' </a> ';
}
}
}
}
if ($cfg ['pageindex'] > 1) {
$pfirst = ' <a href="' . str_replace ( '*', '1', $cfg ['link'] ) . '">' . $cfg ['first'] . '</a> ';
$pprev = ' <a href="' . str_replace ( '*', $cfg ['pageindex'] - 1, $cfg ['link'] ) . '">' . $cfg ['prev'] . '</a> ';
} else {
$pfirst = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['first'] . '</span> ';
$pprev = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['prev'] . '</span> ';
}
if ($cfg ['pageindex'] < $cfg ['pagecount']) {
$plast = ' <a href="' . str_replace ( '*', $cfg ['pagecount'], $cfg ['link'] ) . '">' . $cfg ['last'] . '</a> ';
$pnext = ' <a href="' . str_replace ( '*', $cfg ['pageindex'] + 1, $cfg ['link'] ) . '">' . $cfg ['next'] . '</a> ';
} else {
$plast = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['last'] . '</span> ';
$pnext = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['next'] . '</span> ';
}
switch (strtolower ( $cfg ['jump'] )) {
case 'input' :
$pjumpvalue = 'this.value';
$pjump = '<input type="text" size="3" title="请输入要跳转到的页数并回车"' . (($cfg ['jumpplus'] == '') ? '' : ' ' . $cfg ['jumpplus']);
$pjump .= ' onkeydown="网页特效:if(event.charcode==13||event.keycode==13){if(!isnan(' . $pjumpvalue . ')){';
$pjump .= ($cfg ['jumpaction'] == '' ? ((strtolower ( substr ( $cfg ['link'], 0, 11 ) ) == 'javascript:') ? str_replace ( '*', $pjumpvalue, substr ( $cfg ['link'], 12 ) ) : " document.location.href='" . str_replace ( '*', ''+' . $pjumpvalue . '+'', $cfg ['link'] ) . '';') : str_replace ( "*", $pjumpvalue, $cfg ['jumpaction'] ));
$pjump .= '}return false;}" />';
break;
case 'select' :
$pjumpvalue = "this.options[this.selectedindex].value";
$pjump = '<select ' . ($cfg ['jumpplus'] == '' ? ' ' . $cfg ['jumpplus'] . ' onchange="javascript:' : $cfg ['jumpplus']);
$pjump .= ($cfg ['jumpaction'] == '' ? ((strtolower ( substr ( $cfg ['link'], 0, 11 ) ) == 'javascript:') ? str_replace ( '*', $pjumpvalue, substr ( $cfg ['link'], 12 ) ) : " document.location.href='" . str_replace ( '*', ''+' . $pjumpvalue . '+'', $cfg ['link'] ) . '';') : str_replace ( "*", $pjumpvalue, $cfg ['jumpaction'] ));
$pjump .= '" title="请选择要跳转到的页数"> ';
if ($cfg ['jumplong'] == 0) {
for($i = 0; $i <= $cfg ['pagecount']; $i ++) {
$pjump .= '<option value="' . $i . '"' . (($i == $cfg ['pageindex']) ? ' selected="selected"' : '') . ' >' . $i . '</option> ';
}
} else {
$pjumplong = intval ( $cfg ['jumplong'] / 2 );
$pjumpstart = ((($cfg ['pageindex'] - $pjumplong) < 1) ? 1 : ($cfg ['pageindex'] - $pjumplong));
$pjumpstart = ((($cfg ['pagecount'] - $cfg ['pageindex']) < $pjumplong) ? ($pjumpstart - ($pjumplong - ($cfg ['pagecount'] - $cfg ['pageindex'])) + 1) : $pjumpstart);
$pjumpstart = (($pjumpstart < 1) ? 1 : $pjumpstart);
$j = 1;
for($i = $pjumpstart; $i <= $cfg ['pageindex']; $i ++, $j ++) {
$pjump .= '<option value="' . $i . '"' . (($i == $cfg ['pageindex']) ? ' selected="selected"' : '') . '>' . $i . '</option> ';
}
$pjumplong = $cfg ['pagecount'] - $cfg ['pageindex'] < $pjumplong ? $pjumplong : $pjumplong + ($pjumplong - $j) + 1;
$pjumpend = $cfg ['pageindex'] + $pjumplong > $cfg ['pagecount'] ? $cfg ['pagecount'] : $cfg ['pageindex'] + $pjumplong;
for($i = $cfg ['pageindex'] + 1; $i <= $pjumpend; $i ++) {
$pjump .= '<option value="' . $i . '">' . $i . '</option> ';
}
}
$pjump .= '</select>';
break;
}
$patterns = array ('/{recordcount}/', '/{pagecount}/', '/{pageindex}/', '/{pagesize}/', '/{list}/', '/{liststart}/', '/{listend}/', '/{first}/', '/{prev}/', '/{next}/', '/{last}/', '/{jump}/' );
$replace = array ($cfg ['recordcount'], $cfg ['pagecount'], $cfg ['pageindex'], $cfg ['pagesize'], $plist, $pliststart, $plistend, $pfirst, $pprev, $pnext, $plast, $pjump );
$tmpstr = chr ( 13 ) . chr ( 10 ) . preg_replace ( $patterns, $replace, $tmpstr ) . chr ( 13 ) . chr ( 10 );
unset ( $cfg );
return $tmpstr;
}
}
?>
服务器端传送的数据仍是gb2312编码,客户端用js将汉字转变成utf8编码显示在页面
方法一json
一,服务器端json数据用php教程的iconv函数转换:iconv('gb2312', 'utf8', "被转换字符串,输出到浏览器");
客户端获取utf8数据,再转成gb2312:
function gb2utf8(data){//gb编码是,ie通过二进制码utf8->gbk转为中文
var glbencode = [];
gb2utf8_data = data;
execscript("gb2utf8_data = midb(gb2utf8_data, 1)", "vbscript");
var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g|>,"%$2%$1").replace(/%([a-z].)%(.{2})/g|>,"@$1$2");
t=t.split("@");
var i=0,j=t.length,k;
while(++i<j)>
k=t.substring(0,4);
if(!glbencode[k]) {
gb2utf8_char = eval("0x"+k);
execscript("gb2utf8_char = chr(gb2utf8_char)", "vbscript");
glbencode[k]=escape(gb2utf8_char).substring(1,6);
}
t=glbencode[k]+t.substring(4);
}
gb2utf8_data = gb2utf8_char = null;
return unescape(t.join("%"));
}
二,header("content-type", "application/x-www-form-urlencoded; charset=gbk"); //输出头标,设置为gbk编码
三,在ajax请求数据前调用上面的方法指定请求使用的字符集:xmlhttp.setrequestheader( "content-type", "application/x-www-form-urlencoded;charset=gbk");
方案二
search.php
<?php
header("content-type: text/html; charset=gb2312");
include './search.htm';
?>
search.htm
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>高级搜索</title>
</head>
<body>
<h3>高级搜索</h3>
<form method="post" action="">
学校类型:
<select name="schooltype">
<option value="">全部</option>
<option value="1">小学</option>
<option value="2">初中</option>
</select>
学校名称:
<select name="sid" id="sid">
<option value="">请选择学校</option>
</select>
</form>
<script type="text/网页特效">
function ajax(settings) {
var xhr = window.activexobject ? new activexobject("microsoft.xmlhttp") : new xmlhttprequest(), successed = false;
xhr.open(settings.type, settings.url);
if(settings.type == 'post')
xhr.setrequestheader('content-type', 'application/x-www-form-urlencoded');
xhr.send((!settings.cache ? 'time=' + new date().gettime() + '&' : '') + settings.data);
settings.loader();
settimeout(function() {
if(!successed) {
alert('resquest timeout!');
xhr.abort();
}
}, settings.timeout);
xhr.onreadystatechange = function() {
if (xhr.readystate == 4 && xhr.status == 200) {
settings.callback(xhr.responsetext.replace(/(^s*)|(s*$)/g, ""));
}
successed = true;
}
}
function a(t) {
ajax({
type: 'post',
url: 'ajax.php',
data: 'schooltype=' + t,
timeout: 8000,
cache: true,
loader: function() {},
callback: function(d) {
var arr = eval(d);
if(typeof(arr) == 'object') {
var obj, option;
document.getelementbyid('sid').innerhtml = '';
for(var i = 0; obj = arr; i ++) {
option = document.createelement('option');
option.value = obj[0];
option.innerhtml = txt2utf8(obj[1], '&#');
document.getelementbyid('sid').appendchild(option);
}
}
}
})
}
function txt2utf8(string, prefix){
for(var i=0,utf8=[];i<string.length;utf8.push((prefix||'u')+string.charcodeat(i++)));
return utf8.join('');
}
a(0);
</script>
</body>
</html>ajax.php
<?php
header("content-type: text/html; charset=gb2312");
$schooltype = !empty($_post['schooltype']) ? $_post['schooltype'] : 0;
switch($schooltype) {
case 0:
echo "[['40', '太平溪镇花栗包完全小学'],['41', '太平溪镇长岭黑龙江希望小学'],['42', '乐天溪镇初级中学'],['43', '乐天溪镇莲沱初级中学']]";
break;
case 1:
echo "[['40', '太平溪镇花栗包完全小学'],['41', '太平溪镇长岭黑龙江希望小学']]";
break;
case 2:
echo "[['42', '乐天溪镇初级中学'],['43', '乐天溪镇莲沱初级中学']]";
break;
default:
break;
}
?>
本款文件上传类,默认是上传单文件的,我们只要修改$inputname ='files'为你的表单名就可以方便的实现批量文件上传了。 $savename = ''保存文件名, $alowexts = array()设置允许上传的类型,$savepath = ''保存路径。
*/
代码如下 | 复制代码 |
class upload function __construct($inputname ='files', $savepath = '', $savename = '', $alowexts = array(),$maxsize = 1024000) if(!make_dir($this->savepath)) if(move_uploaded_file($tmp_name,$this->savepath.$savename) || @copy($tmp_name,$this->savepath.$savename)) function isuploadedfile($file) //去掉系统自带的反斜线 function error() } |
//使用方法
new upload();
代码如下 | 复制代码 |
*/ $url = strtolower(trim($_get['url'])); $url = getshorturl($url); if (empty($pn)) $pn = 1; switch ($lm) { <body> function文件 <?php 源码下载地址 http://down.111cn.net/s/2010/1025/21415.html |
如果需要更新cookie以让其储存新值,只需要将其原值覆盖即可。因此,即使你已经在之前的页面中刚刚发送cookie,仍可以将你的名字改为"jeff"。
*/
代码如下 | 复制代码 |
$y2k = mktime(0,0,0,1,1,2000); setcookie('name', 'jeff', $y2k); |
/*
上面是个简单的cookie实例,下面我们来看个复杂的cookie 来作用户登录验证实例代码。
*/
代码如下 | 复制代码 |
function verify() function cookie($set = true) |
相关文章
- 我们这里介绍php与KindEditor编辑器使用时如何利用KindEditor编辑器的分页功能实现文章内容分页,KindEditor编辑器在我们点击分页时会插入代码,我们只要以它为分切符,就...2016-11-25
- 最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
- 本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
深入解析WordPress中加载模板的get_template_part函数
这篇文章主要介绍了WordPress中加载模板的get_template_part函数,其中重点讲解了其函数钩子的使用,需要的朋友可以参考下...2016-01-14- Vue.js通过简洁的API提供高效的数据绑定和灵活的组件系统.这篇文章主要介绍了vue.js 表格分页ajax 异步加载数据的相关资料,需要的朋友可以参考下...2016-10-20
- mysql安装成功后有几个默认的配置模板,列表如下: my-huge.cnf : 用于高端产品服务器,包括1到2GB RAM,主要运行mysql my-innodb-heavy-4G.ini : 用于只有innodb的安装,最多有4GB RAM,支持大的查询和低流量 my-large.cnf : 用于...2015-03-15
Springboot如何使用mybatis实现拦截SQL分页
这篇文章主要介绍了Springboot使用mybatis实现拦截SQL分页,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-19- 在php中要实现分页比起asp中要简单很多了,我们核心就是直接获取当前页面然后判断每页多少再到数据库中利用limit就可以实现分页查询了,下面我来详细介绍分页类实现程序...2016-11-25
- 本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
- 这篇文章主要为大家详细介绍了AngularJS实现分页显示数据库信息效果的相关资料,感兴趣的小伙伴们可以参考一下...2016-07-06
- 1、简介Smarty是一个使用PHP写出来的模板PHP模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。简单的讲,目...2014-05-31
- 这篇文章主要介绍了基于jquery实现表格无刷新分页,功能实现了前端排序功能,增加了前端搜索功能,感兴趣的小伙伴们可以参考一下...2016-01-08
- 这篇文章主要为大家详细介绍了vue实现页面打印自动分页的两种方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-29
- 这篇文章主要为大家详细介绍了Ionic默认的Tabs模板使用实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-09-01
- 这篇文章主要介绍了Springboot使用thymeleaf动态模板实现刷新,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-08-31
- 这篇文章主要为大家详细介绍了vue.js表格分页示例,ajax异步加载数据...2016-10-20
- 有时候我们从数据库获取的数据量太大,而我们不需要一次性显示那么多的时候,我们就要对数据进行分页处理了,让每页显示不同的数据。...2020-06-25
- 这篇文章主要为大家详细介绍了原生js实现分页效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-09-24
- 最近,在做测试开发平台的时候,需要对测试用例的列表进行后端分页,在实际去写代码和测试的过程中,发现这里面还是有些细节的,故想复盘一下...2021-09-29
- 这篇文章主要介绍了WordPress中获取所使用的模板的页面ID的简单方法,通过这个方法来获取页面的链接是比较方便的,需要的朋友可以参考下...2016-01-04