thinkphp的问题弄了半天了,还是不知道,各位大神帮忙看看吧
控制器中的代码:
public function testt(){
//$title="练习册";
//$content='<br>'.$_GET['name'];
//$this->assign('title',$title);
$User = M('name');
$list = $User->select();
$this->assign('list',$list);
$this->assign("title","SMARTY测试");
$this->display();
}
模板中的代码:
{$title}<br />
<hr />
<foreach name="list" item="vo">
{$vo.age}{$vo.name}<hr />
</foreach>
重要去掉foreach部分就正常,否则就会出显这样
syntax error, unexpected ')', expecting T_VARIABLE or '$' D:\AppServ\www\App\admin\Runtime\Cache\6e178638460c8ba2282739c247c2ce14.php 第 10 行.
cache文件中的代码为:
<body>
<?php echo ($title); ?><br />
<hr />
<?php if(is_array($)): foreach($ as $key=>$): echo ($vo["age"]); echo ($vo["name"]); ?><hr /><?php endforeach; endif; ?>
</body>
看来好久也没有头绪,怎么办啊?
thinkphp foreach
[解决办法]
list是空 你自己在检查下
[解决办法]
public function _foreach($attr,$content) {
static $_iterateParseCache = array();
//如果已经解析过,则直接返回变量值
$cacheIterateId = md5($attr.$content);
if(isset($_iterateParseCache[$cacheIterateId]))
return $_iterateParseCache[$cacheIterateId];
$tag = $this->parseXmlAttr($attr,'foreach');
$name = $tag['name'];
$item = $tag['item'];
$key = !empty($tag['key'])?$tag['key']:'key';
$name = $this->autoBuildVar($name);
$parseStr = '<?php if(is_array('.$name.')): foreach('.$name.' as $'.$key.'=>$'.$item.'): ?>';
$parseStr .= $this->tpl->parse($content);
$parseStr .= '<?php endforeach; endif; ?>';
$_iterateParseCache[$cacheIterateId] = $parseStr;
if(!empty($parseStr)) {
return $parseStr;
}
return ;
}
public function parseXmlAttr($attr,$tag) {
//XML解析安全过滤
$attr = str_replace('&','___', $attr);
$xml = '<tpl><tag '.$attr.' /></tpl>';
$xml = simplexml_load_string($xml);
if(!$xml) {
throw_exception(L('_XML_TAG_ERROR_').' : '.$attr);
}
$xml = (array)($xml->tag->attributes());
$array = array_change_key_case($xml['@attributes']);
if($array) {
$attrs = explode(',',$this->tags[strtolower($tag)]['attr']);
if(isset($this->tags[strtolower($tag)]['must'])){
$must = explode(',',$this->tags[strtolower($tag)]['must']);
}else{
$must = array();
}
foreach($attrs as $name) {
if( isset($array[$name])) {
$array[$name] = str_replace('___','&',$array[$name]);
}elseif(false !== array_search($name,$must)){
throw_exception(L('_PARAM_ERROR_').':'.$name);
}
}
return $array;
}
}