php codeigniter框架分页类
codeigniter 具有非常容易使用的分页类。在本教程中我会做一个从数据库教程返回一组结果并分页这些结果的简单例子。我将使用最新版本的 ci。分页类并没有修改(至少我认为没有),用最新的稳定版框架总是好的
调用方法
//创建分页
$config = array();
$this->load->library('hpages');
$config['base_url'] = "channel/lists/c{$slug}/{page}";
$config['total_rows'] = intval($total);
$config['per_page'] = $pagesize;
$config['uri_segment'] = 1;
$config['num_links'] = 3;
$config['underline_uri_seg'] = 1; //下划线uri中页数所在的位置
$this->hpages->init($config);
$this->template['lists'] = $list;
$this->template['pagestr'] = $this->hpages->create_links(1);
php教程文件代码
<?php if (! defined('basepath')) exit('access denied!');
/**
* file_name : hpages.php
* 浩海网络 前台 分页类
*
* @package haohailuo
* @author by laurence.xu <haohailuo@163.com>
* @copyright copyright (c) 2010, haohailuo, inc.
* @link http://www.haohailuo.com
* @since version 1.0 $id$
* @version wed dec 08 12:21:17 cst 2010
* @filesource
*/
class hpages {var $base_url = ''; //基本链接地址
var $total_rows = ''; //数据总数
var $per_page = 10; //每页条数
var $num_links = 2; //要显示的左右链接的个数
var $cur_page = 1; //当前页数
var $first_link = '‹ first'; //首页字符
var $next_link = '>'; //下一页的字符
var $prev_link = '<'; //上一页的字符
var $last_link = 'last ›'; //末页的字符
var $uri_segment = 3; //分页数所在的uri片段位置
var $full_tag_open = ''; //分页区域开始的html标签
var $full_tag_close = ''; //分页区域结束的后html标签
var $first_tag_open = ''; //首页开始的html标签
var $first_tag_close = ' '; //首页结束的html标签
var $last_tag_open = ' '; //末页开始的html标签
var $last_tag_close = ''; //末页结束的html标签
var $cur_tag_open = ' <b>';//当前页开始的...
var $cur_tag_close = '</b>'; //当前页结束的...
var $next_tag_open = ' '; //下一页开始的.....
var $next_tag_close = ' '; //下一页结束的.....
var $prev_tag_open = ' '; //上一页开始的.....
var $prev_tag_close = ''; //上一页结束的.....
var $num_tag_open = ' '; //“数字”链接的打开标签。
var $num_tag_close = ''; //“数字”链接的结束标签。
var $page_query_string = false;
var $query_string_segment = 'per_page';
var $page_mode = 'default'; //default for add page at the end? if include {page}, will replace it for current page.
var $underline_uri_seg = -1; //存在下划线时,页码所在数组下标位置
var $custom_cur_page = 0; //自定义当前页码,存在此值是,系统将不自动判断当前页数,默认不启用
function __construct() {
$this->hpages();
}
/**
* constructor
*
* @access public
*/
function hpages() {
if (file_exists(apppath.'config/pagination.php')) {
require_once(apppath.'config/pagination.php');
foreach ($config as $key=>$val) {
$this->{$key} = $val;
}
}
log_message('debug', "hpages class initialized");
}
/**
* 初始化参数
*
* @see init()
* @author laurence.xu <haohailuo@163.com>
* @version wed dec 08 12:26:07 cst 2010
* @param <array> $params 待初始化的参数
*/
function init($params = array()) {
if (count($params) > 0) {
foreach ($params as $key => $val) {
if (isset($this->$key)) {
$this->$key = $val;
}
}
}
}
/**
* 创建分页链接
*
* @see create_links()
* @author laurence.xu <haohailuo@163.com>
* @version wed dec 08 15:02:27 cst 2010
* @param <boolean> $show_info 是否显示总条数等信息
* @return <string> $output
*/
function create_links($show_info = false, $top_info = false) {
//如果没有记录或者每页条数为0,则返回空
if ($this->total_rows == 0 || $this->per_page == 0) {
return '';
}//计算总页数
$num_pages = ceil($this->total_rows / $this->per_page);//只有一页,返回空
if ($num_pages == 1 && !$show_info) {
return '';
}
$ci =& get_instance();//获取当前页编号
if ($ci->config->item('enable_query_strings') === true || $this->page_query_string === true) {
if ($ci->input->get($this->query_string_segment) != 0) {
$this->cur_page = $ci->input->get($this->query_string_segment);// prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
} else {
if (intval($this->custom_cur_page) > 0) {
$this->cur_page = (int) $this->custom_cur_page;
}else{
$uri_segment = $ci->uri->segment($this->uri_segment, 0);
if ( !empty($uri_segment) ) {
$this->cur_page = $uri_segment;
//如果有下划线
if ($this->underline_uri_seg >= 0) {
if (strpos($this->cur_page, '-') !== false) {
$arr = explode('-', $this->cur_page);
}else {
$arr = explode('_', $this->cur_page);
}
$this->cur_page = $arr[$this->underline_uri_seg];
unset($arr);
}
// prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
}
}
//echo $this->cur_page;exit;
//左右显示的页码个数
$this->num_links = (int)$this->num_links;if ($this->num_links < 1) {
show_error('your number of links must be a positive number.');
}if ( ! is_numeric($this->cur_page) || $this->cur_page < 1) {
$this->cur_page = 1;
}
//如果当前页数大于总页数,则赋值给当前页数最大值
if ($this->cur_page > $num_pages) {
$this->cur_page = $num_pages;
}$uri_page_number = $this->cur_page;
if ($ci->config->item('enable_query_strings') === true || $this->page_query_string === true) {
$this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
} else {
$this->base_url = rtrim($this->base_url, '/') .'/';
}
if (strpos($this->base_url, "{page}") !== false) {
$this->page_mode = 'replace';
}
$output = $top_output = '';
//数据总量信息
if ($show_info) {
$output = " 共<b>".$this->total_rows ."</b>条记录 <span style='color:#ff0000;font-weight:bold'>{$this->cur_page}</span>/<b>".$num_pages."</b>页 每页<b>{$this->per_page}</b>条 ";
}
//数据信息,显示在上面,以供提醒
if ($top_info) {
$top_output = " 共 <b>".$this->total_rows ."</b> 条记录 第<span style='color:#ff0000;font-weight:bold'>{$this->cur_page}</span>页/共<b>".$num_pages."</b>页 ";
}
//判断是否要显示首页
if ($this->cur_page > $this->num_links+1) {
$output .= $this->first_tag_open.'<a href="'.$this->makelink().'">'.$this->first_link.'</a>'.$this->first_tag_close;
}
//显示上一页
if ($this->cur_page != 1) {
$j = $this->cur_page - 1;
if ($j == 0) $j = '';
$output .= $this->prev_tag_open.'<a href="'.$this->makelink($j).'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
}
//显示中间页
for ($i=1; $i <= $num_pages; $i++){
if ($i < $this->cur_page-$this->num_links || $i > $this->cur_page+$this->num_links) {
continue;
}
//显示中间页数
if($this->cur_page == $i){
$output .= $this->cur_tag_open.$i.$this->cur_tag_close; //当前页
}else {
$output .= $this->num_tag_open.'<a href="'.$this->makelink($i).'">'.$i.'</a>'.$this->num_tag_close;
}
}
//显示下一页
if ($this->cur_page < $num_pages) {
$k = $this->cur_page + 1;
$output .= $this->next_tag_open.'<a href="'.$this->makelink($k).'">'.$this->next_link.'</a>'.$this->next_tag_close;
}
//显示尾页
if (($this->cur_page + $this->num_links) < $num_pages) {
$output .= $this->last_tag_open.'<a href="'.$this->makelink($num_pages).'">'.$this->last_link.'</a>'.$this->last_tag_close;
}$output = preg_replace("#([^:])//+#", "1/", $output);
// add the wrapper html if exists
$output = $this->full_tag_open.$output.$this->full_tag_close;if ($top_info) {
return array($output, $top_output);
}else {
return $output;
}
}
/**
* 创建链接url地址
*
* @param <string> $str
* @return <string>
*/
function makelink($str = '') {
if($this->page_mode == 'default') {
return $this->_forsearch($this->base_url.$str);
} else {
$url = $this->base_url;
if ($str == 1) {
$url = str_replace('/{page}', '', $this->base_url);
}
$url = str_replace("{page}", $str, $url);
return $this->_forsearch($url);
}
}
/**
* 处理url地址
*
* @see _forsearch()
* @author laurence.xu <haohailuo@163.com>
* @version wed dec 08 14:33:58 cst 2010
* @param <string> $string pinfo
* @return <string>
*/
function _forsearch($string) {
$length = strlen($string) - 1;
if($string{$length} == '/') {
$string = rtrim($string, '/');
}
return site_url($string);
return $string;
}
}// end pagination class
/* end of file hpages.php */
/* location: ./system/libraries/hpages.php */
<!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=utf-8" />
<title>ajax php教程多文件上传代码</title>
<script>
(function(){
var d = document, w = window;/**
* get element by id
*/
function get(element){
if (typeof element == "string")
element = d.getelementbyid(element);
return element;
}/**
* attaches event to a dom element
*/
function addevent(el, type, fn){
if (w.addeventlistener){
el.addeventlistener(type, fn, false);
} else if (w.attachevent){
var f = function(){
fn.call(el, w.event);
};
el.attachevent('on' + type, f)
}
}
/**
* creates and returns element from html chunk
*/
var toelement = function(){
var div = d.createelement('div');
return function(html){
div.innerhtml = html;
var el = div.childnodes[0];
div.removechild(el);
return el;
}
}();function hasclass(ele,cls){
return ele.classname.match(new regexp('(s|^)'+cls+'(s|$)'));
}
function addclass(ele,cls) {
if (!hasclass(ele,cls)) ele.classname += " "+cls;
}
function removeclass(ele,cls) {
var reg = new regexp('(s|^)'+cls+'(s|$)');
ele.classname=ele.classname.replace(reg,' ');
}// getoffset function copied from jquery lib (http://jquery.com/)
if (document.documentelement["getboundingclientrect"]){
// get offset using getboundingclientrect
// http://ejohn.org/blog/getboundingclientrect-is-awesome/
var getoffset = function(el){
var box = el.getboundingclientrect(),
doc = el.ownerdocument,
body = doc.body,
docelem = doc.documentelement,
// for ie
clienttop = docelem.clienttop || body.clienttop || 0,
clientleft = docelem.clientleft || body.clientleft || 0,
// in internet explorer 7 getboundingclientrect property is treated as physical,
// while others are logical. make all logical, like in ie8.
zoom = 1;
if (body.getboundingclientrect) {
var bound = body.getboundingclientrect();
zoom = (bound.right - bound.left)/body.clientwidth;
}
if (zoom > 1){
clienttop = 0;
clientleft = 0;
}
var top = box.top/zoom + (window.pageyoffset || docelem && docelem.scrolltop/zoom || body.scrolltop/zoom) - clienttop,
left = box.left/zoom + (window.pagexoffset|| docelem && docelem.scrollleft/zoom || body.scrollleft/zoom) - clientleft;
return {
top: top,
left: left
};
}
} else {
// get offset adding all offsets
var getoffset = function(el){
if (w.jquery){
return jquery(el).offset();
}
var top = 0, left = 0;
do {
top += el.offsettop || 0;
left += el.offsetleft || 0;
}
while (el = el.offsetparent);
return {
left: left,
top: top
};
}
}function getbox(el){
var left, right, top, bottom;
var offset = getoffset(el);
left = offset.left;
top = offset.top;
right = left + el.offsetwidth;
bottom = top + el.offsetheight;
return {
left: left,
right: right,
top: top,
bottom: bottom
};
}/**
* crossbrowser mouse coordinates
*/
function getmousecoords(e){
// pagex/y is not supported in ie
// http://www.quirksmode.org/dom/w3c_css教程om.html
if (!e.pagex && e.clientx){
// in internet explorer 7 some properties (mouse coordinates) are treated as physical,
// while others are logical (offset).
var zoom = 1;
var body = document.body;
if (body.getboundingclientrect) {
var bound = body.getboundingclientrect();
zoom = (bound.right - bound.left)/body.clientwidth;
}return {
x: e.clientx / zoom + d.body.scrollleft + d.documentelement.scrollleft,
y: e.clienty / zoom + d.body.scrolltop + d.documentelement.scrolltop
};
}
return {
x: e.pagex,
y: e.pagey
};}
/**
* function generates unique id
*/
var getuid = function(){
var id = 0;
return function(){
return 'valumsajaxupload' + id++;
}
}();function filefrompath(file){
return file.replace(/.*(/|)/, "");
}function getext(file){
return (/[.]/.exec(file)) ? /[^.]+$/.exec(file.tolowercase()) : '';
}/**
* cross-browser way to get xhr object
*/
var getxhr = function(){
var xhr;
return function(){
if (xhr) return xhr;
if (typeof xmlhttprequest !== 'undefined') {
xhr = new xmlhttprequest();
} else {
var v = [
"microsoft.xmlhttp",
"msxml2.xmlhttp.5.0",
"msxml2.xmlhttp.4.0",
"msxml2.xmlhttp.3.0",
"msxml2.xmlhttp.2.0"
];
for (var i=0; i < v.length; i++){
try {
xhr = new activexobject(v[i]);
break;
} catch (e){}
}
}return xhr;
}
}();// please use ajaxupload , ajax_upload will be removed in the next version
ajax_upload = ajaxupload = function(button, options){
if (button.jquery){
// jquery object was passed
button = button[0];
} else if (typeof button == "string" && /^#.*/.test(button)){
button = button.slice(1);
}
button = get(button);
this._input = null;
this._button = button;
this._disabled = false;
this._submitting = false;
// variable changes to true if the button was clicked
// 3 seconds ago (requred to fix safari on mac error)
this._justclicked = false;
this._parentdialog = d.body;
if (window.jquery && jquery.ui && jquery.ui.dialog){
var parentdialog = jquery(this._button).parents('.ui-dialog');
if (parentdialog.length){
this._parentdialog = parentdialog[0];
}
}
this._settings = {
// location of the server-side upload script
action: 'upload.php',
// file upload name
name: 'userfile',
// additional data to send
data: {},
// submit file as soon as it's selected
autosubmit: true,
// the type of data that you're expecting back from the server.
// html and xml are detected automatically.
// only useful when you are using json data as a response.
// set to "json" in that case.
responsetype: false,
// location of the server-side script that fixes safari
// hanging problem returning "connection: close" header
closeconnection: '',
// class applied to button when mouse is hovered
hoverclass: 'hover',
// when user selects a file, useful with autosubmit disabled
onchange: function(file, extension){},
// callback to fire before file is uploaded
// you can return false to cancel upload
onsubmit: function(file, extension){},
// fired when file upload is completed
// warning! do not use "false" string as a response!
oncomplete: function(file, response) {}
};// merge the users options with our defaults
for (var i in options) {
this._settings[i] = options[i];
}
this._createinput();
this._rerouteclicks();
}
// assigning methods to our class
ajaxupload.prototype = {
setdata : function(data){
this._settings.data = data;
},
disable : function(){
this._disabled = true;
},
enable : function(){
this._disabled = false;
},
// removes instance
destroy : function(){
if(this._input){
if(this._input.parentnode){
this._input.parentnode.removechild(this._input);
}
this._input = null;
}
},
/**
* creates invisible file input above the button
*/
_createinput : function(){
var self = this;
var input = d.createelement("input");
input.setattribute('type', 'file');
input.setattribute('name', this._settings.name);
var styles = {
'position' : 'absolute'
,'margin': '-5px 0 0 -175px'
,'padding': 0
,'width': '220px'
,'height': '30px'
,'fontsize': '14px'
,'opacity': 0
,'cursor': 'pointer'
,'display' : 'none'
,'zindex' : 2147483583 //max zindex supported by opera 9.0-9.2x
// strange, i expected 2147483647
// doesn't work in ie :(
//,'direction' : 'ltr'
};
for (var i in styles){
input.style[i] = styles[i];
}
// make sure that element opacity exists
// (ie uses filter instead)
if ( ! (input.style.opacity === "0")){
input.style.filter = "alpha(opacity=0)";
}
this._parentdialog.appendchild(input);addevent(input, 'change', function(){
// get filename from input
var file = filefrompath(this.value);
if(self._settings.onchange.call(self, file, getext(file)) == false ){
return;
}
// submit form when value is changed
if (self._settings.autosubmit){
self.submit();
}
});
// fixing problem with safari
// the problem is that if you leave input before the file select dialog opens
// it does not upload the file.
// as dialog opens slowly (it is a sheet dialog which takes some time to open)
// there is some time while you can leave the button.
// so we should not change display to none immediately
addevent(input, 'click', function(){
self.justclicked = true;
settimeout(function(){
// we will wait 3 seconds for dialog to open
self.justclicked = false;
}, 2500);
});
this._input = input;
},
_rerouteclicks : function (){
var self = this;
// ie displays 'access denied' error when using this method
// other browsers just ignore click()
// addevent(this._button, 'click', function(e){
// self._input.click();
// });
var box, dialogoffset = {top:0, left:0}, over = false;
addevent(self._button, 'mouseo教程ver', function(e){
if (!self._input || over) return;
over = true;
box = getbox(self._button);
if (self._parentdialog != d.body){
dialogoffset = getoffset(self._parentdialog);
}
});
// we can't use mouseout on the button,
// because invisible input is over it
addevent(document, 'mousemove', function(e){
var input = self._input;
if (!input || !over) return;
if (self._disabled){
removeclass(self._button, self._settings.hoverclass);
input.style.display = 'none';
return;
}
var c = getmousecoords(e);if ((c.x >= box.left) && (c.x <= box.right) &&
(c.y >= box.top) && (c.y <= box.bottom)){
input.style.top = c.y - dialogoffset.top + 'px';
input.style.left = c.x - dialogoffset.left + 'px';
input.style.display = 'block';
addclass(self._button, self._settings.hoverclass);
} else {
// mouse left the button
over = false;
var check = setinterval(function(){
// if input was just clicked do not hide it
// to prevent safari bug
if (self.justclicked){
return;
}
if ( !over ){
input.style.display = 'none';
}
clearinterval(check);
}, 25);
removeclass(self._button, self._settings.hoverclass);
}
});
},
/**
* creates iframe with unique name
*/
_createiframe : function(){
// unique name
// we cannot use gettime, because it sometimes return
// same value in safari :(
var id = getuid();
// remove ie6 "this page contains both secure and nonsecure items" prompt
// http://tinyurl.com/77w9wh
var iframe = toelement('<iframe src="网页特效:false;" name="' + id + '" />');
iframe.id = id;
iframe.style.display = 'none';
d.body.appendchild(iframe);
return iframe;
},
/**
* upload file without refreshing the page
*/
submit : function(){
var self = this, settings = this._settings;
if (this._input.value === ''){
// there is no file
return;
}
// get filename from input
var file = filefrompath(this._input.value);// execute user event
if (! (settings.onsubmit.call(this, file, getext(file)) == false)) {
// create new iframe for this submission
var iframe = this._createiframe();
// do not submit if user function returns false
var form = this._createform(iframe);
form.appendchild(this._input);// a pretty little hack to make uploads not hang in safari. just call this
// immediately before the upload is submitted. this does an ajax call to
// the server, which returns an empty document with the "connection: close"
// header, telling safari to close the active connection.
// http://blog.airbladesoftware.com/2007/8/17/note-to-self-prevent-uploads-hanging-in-safari
if (settings.closeconnection && /applewebkit|msie/.test(navigator.useragent)){
var xhr = getxhr();
// open synhronous connection
xhr.open('get', settings.closeconnection, false);
xhr.send('');
}
form.submit();
d.body.removechild(form);
form = null;
this._input = null;
// create new input
this._createinput();
var todeleteflag = false;
addevent(iframe, 'load', function(e){
if (// for safari
iframe.src == "javascript:'%3chtml%3e%3c/html%3e';" ||
// for ff, ie
iframe.src == "javascript:'<html></html>';"){
// first time around, do not delete.
if( todeleteflag ){
// fix busy state in ff3
settimeout( function() {
d.body.removechild(iframe);
}, 0);
}
return;
}
var doc = iframe.contentdocument ? iframe.contentdocument : frames[iframe.id].document;// fixing opera 9.26
if (doc.readystate && doc.readystate != 'complete'){
// opera fires load event multiple times
// even when the dom is not ready yet
// this fix should not affect other browsers
return;
}
// fixing opera 9.64
if (doc.body && doc.body.innerhtml == "false"){
// in opera 9.64 event was fired second time
// when body.innerhtml changed from false
// to server response approx. after 1 sec
return;
}
var response;
if (doc.xmldocument){
// response is a xml document ie property
response = doc.xmldocument;
} else if (doc.body){
// response is html document or plain text
response = doc.body.innerhtml;
if (settings.responsetype && settings.responsetype.tolowercase() == 'json'){
// if the document was sent as 'application/javascript' or
// 'text/javascript', then the browser wraps教程 the text in a <pre>
// tag and performs html encoding on the contents. in this case,
// we need to pull the original text content from the text node's
// nodevalue property to retrieve the unmangled content.
// note that ie6 only understands text/html
if (doc.body.firstchild && doc.body.firstchild.nodename.touppercase() == 'pre'){
response = doc.body.firstchild.firstchild.nodevalue;
}
if (response) {
response = window["eval"]("(" + response + ")");
} else {
response = {};
}
}
} else {
// response is a xml document
var response = doc;
}
settings.oncomplete.call(self, file, response);
// reload blank page, so that reloading main page
// does not re-submit the post. also, remember to
// delete the frame
todeleteflag = true;
// fix ie mixed content issue
iframe.src = "javascript:'<html></html>';";
});
} else {
// clear input to allow user to select same file
// doesn't work in ie6
// this._input.value = '';
d.body.removechild(this._input);
this._input = null;
// create new input
this._createinput();
}
},
/**
* creates form, that will be submitted to iframe
*/
_createform : function(iframe){
var settings = this._settings;
// method, enctype must be specified here
// because changing this attr on the fly is not allowed in ie 6/7
var form = toelement('<form method="post" enctype="multipart/form-data"></form>');
form.style.display = 'none';
form.action = settings.action;
form.target = iframe.name;
d.body.appendchild(form);
// create hidden input element for each data key
for (var prop in settings.data){
var el = d.createelement("input");
el.type = 'hidden';
el.name = prop;
el.value = settings.data[prop];
form.appendchild(el);
}
return form;
}
};
})();
</script>
</head><body>
<p id="errorremind"></p>
<input id="unloadpic" type="button" value="上传图片" />
<ol id="uploadedname"></ol><script type="text/javascript" src="../js/ajaxupload.js"></script>
<script type="text/javascript">
window.onload = function(){
var obtn = document.getelementbyid("unloadpic");
var oshow = document.getelementbyid("uploadedname");
var oremind = document.getelementbyid("errorremind");
new ajaxupload(obtn,{
action:"file_upload.php",
name:"upload",
onsubmit:function(file,ext){
if(ext && /^(jpg|jpeg|png|gif)$/.test(ext)){
//ext是后缀名
obtn.value = "正在上传…";
obtn.disabled = "disabled";
}else{
oremind.style.color = "#ff3300";
oremind.innerhtml = "不支持非图片格式!";
return false;
}
},
oncomplete:function(file,response){
obtn.disabled = "";
obtn.value = "再上传一张图片";
oremind.innerhtml = "";
var newchild = document.createelement("li");
newchild.innerhtml = file;
oshow.appendchild(newchild);
}
});
};
</script>
</body>
</html>
<?php #file_upload.php 2009-11-06
$file_path = '../../../uploads/';
$file_up = $file_path.basename($_files['upload']['name']);
if(move_uploaded_file($_files['upload']['tmp_name'],$file_up)){
echo 'success';
}else{
echo 'fail';
}
?>
一. 什么是memcached
memcached 是一个高性能的分布式内存对象缓存系统,用于动态web应用以减轻数据库教程负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。
相信很多人都用过缓存,在 .net 中也有内置的缓存机制,还有很多第三方工具如apache,nginx等可以做静态资源的缓存,同时我们也可以制定自己的缓存机制,缓存数据库查询的数据以减少对数据库的频繁操作。但是很多时候我们总是感觉这些缓存总不尽人意, memcached可以解决你不少的烦恼问题。最少在我的学习中解决了我不少问题,所以决定记录下来分享。
memcached基于一个存储键/值对的hashmap。其守护进程是用c写的,但是客户端可以用任何语言来编写(本文使用c#作为例子),并通过memcached协议与守护进程通信。可 能这些东西都太高深了,我们暂不做研究。
二. 分布式缓存
其实 memcached作为一个分布式缓存数据服务,但是每个服务之间根本没有进行相互通信,这里可能与我理解的分布式有点区别,可能是我才疏学浅,也可能是每个人思考问题的角度不同。memcached 客户端就是通过一种分布式算法将数据保存到不同的memcached服务器上,将数据进行缓存。分布式缓存,可以而知memcached可以进行大数据量的缓存。这点可以弥补我们之前很多人都遇到的将数据缓存到应用服务器上,而且只能缓存少量数据,否则对应用服务器的影响非常大。
memcached应用机制图:
这个图是有点简陋了,但是问题还是能够描述的清楚的,缓存机制的基本原理就是先查询数据保存到memcached中,地址在此请求就直接从memcached缓存中取数据,这样就可以减少对服务器请求压力。
1.正常的touch创建word
2.fopen 打开word
3.fwrite 写入word 并保存
这样会出现一个问题 如果写入的东西里面含有html代码的话,它将直接写入word而不是 排版了
这个问题 需要在输出html 代码头部加一段代码
$headert='<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/tr/rec-html40">';
$footer="</html>";
比如你的内容是$text;
那么写入的时候$text=$header.$text.$footer;
这样的话fck里面的东西就能按排版的样式输出了!
方法一
<?php
$word= new com("word.application") or die("unable to
create word document");
print "loaded word, version{$word->version}n";
$word->visible =0;
$word->documents->add();
//设置边距 这个有错误
// $word->selection->agesetup->rightmargin ='3"';
//设置字体 这
$word->selection->font->name ='helvetica';
//设置字号
$word->selection->font->size = 8;
//设置颜色
$word->selection->font->colorindex= 13; //wddarkred= 13
//输出到文档
$word->selection->typetext("hello world ");
$range = $word->activedocument->range(0,0);
$table_t =$word->activedocument->tables->add($range,3,4);
$table_t->cell(1,2)->range->insertafter('aaa');
//保存
//$word->sections->add(1);
$word->documents[1]->saveas(dirname(__file__)."/create_test.doc");
//退出
$word->quit();
?>
方法二
<?php
class word
{
function start()
{
ob_start();
print'<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/tr/rec-html40">';
}
function save($path)
{
print "</html>";
$data = ob_get_contents();
ob_end_clean();
$this->wirtefile ($path,$data);
}
function wirtefile ($fn,$data)
{
$fp=fopen($fn,"wb");
fwrite($fp,$data);
fclose($fp);
}
}
?>
调用方法
$word=new word;
$word->start();
echo $cout;
$wordname="word/".time().".doc";
$word->save($wordname);//保存word并且结束
1. 数组
php教程的数组其实是一个关联数组,或者说是哈希表。php不需要预先声明数组的大小,可以用直接赋值的方式来创建数组。例如:
//最传统,用数字做键,赋值
$state[0]="beijing";
$state[1]="hebei";
$state[2]="tianjin";
//如果键是递增的数字,则可以省略
$city[]="shanghai";
$city[]="tianjin";
$city[]="guangzhou";
//用字符串做键
$capital["china"]="beijing";
$capital["japan"]="tokyo";
用array()来创建数组会更加方便一点,可以将数组元素作为array的参数传递给他,也可以用=>运算符创建关联数组。例如:
$p=array(1,3,5,7);
$capital=array(“china”=>”beijing”, “japan=>”tokyo”);
array其实是一种语法结构,而不是函数。和array类似,还有一个list(),它可以用来提取数组中的值,并给多个变量赋值。例如:
list($s,$t)=$city;
echo $s,' ',$t;
输出结果:shanghai tianjin
注意,list方法只能用于由数字索引的数组。
php内建了一些常用的数组处理函数,具体可以参考手册。常用的函数举例如下,count或者sizeof可以得到数组的长度,array_merge 可以合并两个,或者多个数组,array_push(pop)可以像堆栈一样使用数组。
相关文章
- 我们这里介绍php与KindEditor编辑器使用时如何利用KindEditor编辑器的分页功能实现文章内容分页,KindEditor编辑器在我们点击分页时会插入代码,我们只要以它为分切符,就...2016-11-25
- 最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
- 本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
- Vue.js通过简洁的API提供高效的数据绑定和灵活的组件系统.这篇文章主要介绍了vue.js 表格分页ajax 异步加载数据的相关资料,需要的朋友可以参考下...2016-10-20
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
- 这篇文章主要介绍了基于jquery实现表格无刷新分页,功能实现了前端排序功能,增加了前端搜索功能,感兴趣的小伙伴们可以参考一下...2016-01-08
- 这篇文章主要为大家详细介绍了vue实现页面打印自动分页的两种方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-29
- 这篇文章主要为大家详细介绍了vue.js表格分页示例,ajax异步加载数据...2016-10-20
- 有时候我们从数据库获取的数据量太大,而我们不需要一次性显示那么多的时候,我们就要对数据进行分页处理了,让每页显示不同的数据。...2020-06-25
- 最近,在做测试开发平台的时候,需要对测试用例的列表进行后端分页,在实际去写代码和测试的过程中,发现这里面还是有些细节的,故想复盘一下...2021-09-29
- 这篇文章主要为大家详细介绍了原生js实现分页效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-09-24
- 这篇文章主要为大家详细分享了laypage分页控件使用实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-05-20
- ireport默认都是自动分页数据超出页面长度就会自动分到下一页,但有时候业务需要一页只显示固定几行这时候就需要自定义条数了。下面看具体操作吧...2021-10-26
- 这篇文章主要为大家详细介绍了MySQL分页优化,内容思路很详细,有意对MySQL分页优化的朋友可以参考一下...2016-04-22
- 这篇文章主要介绍了EasyUI Pagination 分页的两种做法小结的相关资料,需要的朋友可以参考下...2016-07-25
- 这篇文章主要介绍了基于BootStrap的前端分页带省略号和上下页效果,需要的朋友可以参考下...2017-05-22
- 这篇文章主要介绍了编写PHP脚本来实现WordPress中评论分页的功能的方法,包括上一页下一页和导航式分页功能的添加,需要的朋友可以参考下...2015-12-14