rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » PHP

smarty循环嵌套

<?php
require("include/common.inc.php");  //引入数据库连接文件
require("include/smarty.inc.php");  //引入smarty实例文件
$sql="select clsid,clsname from clstable order by clsid asc";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))

        {    
  $clsname = $row['clsname'];
  //$clslist[$clsid-1]=$row;
  $article[] = array('clsname'=>$clsname,'news'=>array());
        $sqla="select title,content from article where clsid = '".$row['clsid']."' order by articleid desc";
  $resulta=mysql_query($sqla);
        while($rowa=mysql_fetch_array($resulta))
           {
            $article[count($article)-1][news][] = array('id'=>$rowa[clsid],'title'=>$rowa[title]);
           }  
  }

//smarty
    $smarty->assign("article",$article);//赋值函数
    $smarty->display("test.htm");//显示页面:里面是我们要用到的smarty模板
?>

test.htm(模版文件)

<html>
<head>
<title>Smarty测试</title>
</head>
<body>
{-section name=list loop=$article-}

{-$article[list].clsname-}<br />
<hr />

{-section name=aaa loop=$article[list].news-}
{-$article[list].news[aaa].title-}<br />
{-/section-}

<hr style="color:red"/>

{-/section-}
</body>
</html>

用了混写的

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="2">分类管理</td></tr>
<?php
$query = mysql_query("SELECT * FROM {$db_prefix}cates WHERE sid='0'");
while($rs=mysql_fetch_array($query))
{
?>
<tr align="center">
  <td width="3%" height="30" class="altbg1"><b>■</b></td>
  <td height="30" align="left" class="altbg1"><b><?php echo $rs['name'];?></b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="admincp.php?action=edit_class&cid=<?php echo $rs['cid'];?>"><img src="../images/icon_edit.gif" border="0" /></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="admincp.php?action=edit_class&ac=del&cid=<?php echo $rs['cid'];?>"><img src="../images/no.gif" border="0" /></a></td>
</tr>
<?php
$query1 = mysql_query("SELECT * FROM {$db_prefix}cates WHERE sid=".$rs['cid']."");
while($row=mysql_fetch_array($query1))
{
?>
<tr align="center">
  <td width="3%" height="25" class="altbg1">&nbsp;</td>
  <td height="25" align="left" class="altbg1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>●</b>&nbsp;&nbsp;&nbsp;<?php echo $row['name'];?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="admincp.php?action=edit_class&cid=<?php echo $row['cid'];?>"><img src="../images/icon_edit.gif" border="0" /></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="admincp.php?action=edit_class&ac=del&cid=<?php echo $row['cid'];?>"><img src="../images/no.gif" border="0" /></a></td>
</tr>
<?php }?>

<?php }?>
</table>

顶一下
(0)
踩一下
(0)