Home >php教程 >php手册 >改造的volist

改造的volist

WBOY
WBOYOriginal
2016-06-07 11:45:391042browse

1.修复 指定的key不为所循环的key bug
2.给每个循环体加了个 所指定的id加下划线i 的序号变量(id_i), 并且变量从1开始
3.初测通过.不正之处望指正.邮箱:xiatsing@china.com.cn
public function _volist($attr,$content) {
static $_iterateParseCache = array();
//如果已经解析过,则直接返回变量值
$cacheIterateId = md5($attr.$content);
if(isset($_iterateParseCache[$cacheIterateId]))
return $_iterateParseCache[$cacheIterateId];
$tag = $this->parseXmlAttr($attr,'volist');
$name = $tag['name'];
$id = $tag['id'];
$empty = isset($tag['empty'])?$tag['empty']:'';
$key = !empty($tag['key'])?$tag['key']:'i';
$mod = isset($tag['mod'])?$tag['mod']:'2';
// 允许使用函数设定数据集 {$vo.name}
$parseStr = ' if(0===strpos($name,':')) {
$parseStr .= '$_result='.substr($name,1).';';
$name = '$_result';
}else{
$name = $this->autoBuildVar($name);
}
$parseStr .= 'if(is_array('.$name.')): $'. $id .'_i = 1;';
if(isset($tag['length']) && '' !=$tag['length'] ) {
$parseStr .= ' $__LIST__ = array_slice('.$name.','.$tag['offset'].','.$tag['length'].',true);';
}elseif(isset($tag['offset']) && '' !=$tag['offset']){
$parseStr .= ' $__LIST__ = array_slice('.$name.','.$tag['offset'].',null,true);';
}else{
$parseStr .= ' $__LIST__ = '.$name.';';
}
$parseStr .= 'if( count($__LIST__)==0 ) : echo "'.$empty.'" ;';
$parseStr .= 'else: ';
$parseStr .= 'foreach($__LIST__ as $'.$key.'=>$' . $id . '): ';
$parseStr .= '$mod = ($'.$key.' % '.$mod.' );';
$parseStr .= '++$' . $id .'_i;?>';
$parseStr .= $this->tpl->parse($content);
$parseStr .= '';
$_iterateParseCache[$cacheIterateId] = $parseStr;

if(!empty($parseStr)) {
return $parseStr;
}
return ;
}

AD:真正免费,域名+虚机+企业邮箱=0元

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