php smarty 循环遍历数组实现代码
在用循环的时候,加入二位数组中有空的数据。要怎么样才能在循环的时候过滤掉空的数据
在循环体内判断,为空就跳过本次循环不显示数据 继续之后的循环
例如:
foreach ($数组 as $value){
if (empty($value))
continue;
}
PHP代码如下 /** @ 文章分类 含二级分类 @ param int $rootnum -- 一级分类数量 @ param int $childnum -- 二级分类数量 @ 返回值 array @ date 2011.2.24 */ function temp_articletreecate($rootnum,$childnum){ if(!isnumber($rootnum)){ $rootnum = 10; } if(!isnumber($childnum)){ $childnum = 10; } $category = array(); $parent_sql = "SELECT cateid,catename FROM ".TABLE_PREFIX."articlecate WHERE parentid=0 AND depth=0 AND flag=1 ORDER BY orders ASC"; if(intval($rootnum)>0){ $parent_sql.=" LIMIT $rootnum"; } $parent_cate = $GLOBALS['db']->getall($parent_sql); foreach($parent_cate as $parent_key => $parent_value){ //子类数组名为 childcategory 根据情况自定义名称 $category[] = array('cateid'=>$parent_value['cateid'],'catename'=>$parent_value ['catename'],'childcategory'=>array()); //读取子类 $child_sql = "SELECT cateid,catename FROM ".TABLE_PREFIX."articlecate WHERE parentid=".$parent_value['cateid']." AND flag=1 ORDER BY orders ASC"; if(intval($childnum)>0){ $child_sql.=" LIMIT $childnum"; } $child_cate = $GLOBALS['db']->getall($child_sql); foreach($child_cate as $child_key => $child_value){ $category[count($category)-1]['childcategory'][] = array('cateid'=>$child_value ['cateid'],'catename'=>$child_value['catename']); } } return $category; }
php教程 smarty foreach 函数**/
$tpl = new Smarty(); $tpl->template_dir = $cfg['path']['template']; $tpl->compile_dir = $cfg['path']['root'] . 'www.phprm.com/111cn/'; $tpl->compile_check = $cfg['debug']; $tpl->debugging = false; $tpl->caching = 0; $tpl->cache_lifetime = 6000; $tpl->left_delimiter = '<!--{'; $tpl->right_delimiter = '}-->';
//配置smarty
$Db = new Db(); try{ $query = $Db->query($sql); if( $Db->rows( $query ) ) { $array = $Db->fetch( $query,0 ); foreach( $array as $v =>$_v ) { $List[$v]['id'] = $_v[0]; $List[$v]['cntitle'] = $_v[2]; $List[$v]['i'] = $i++; } } }catch( Execption $e ){ MessAge('调用参失败!'); }
//php 处理与程序
tpl->assign('jsMenu_Deng', $List);
//下面为smarty模板处理foreach
<!--{foreach from=$jsMenu_Deng item=item key=key}--> subcat[<!--{$item.i}-->] = new Array("<!--{$item.cntitle}-->","<!--{$item.upid} -->","<!--{$item.id}-->"); <!--{/foreach}--> var onecount=<!--{$item.i}-->;
//其实很简单就是把数据用php保存到数据,然后再由smarty foreach 来实现。
本文地址:http://www.phprm.com/frame/38812.html
转载随意,但请附上文章地址:-)