smarty模板循环镶嵌

test2.htm
<html>
  <head>
  <title>巢状循环测试</title>
  </head>
  <body>
<{section name=sec1 loop=$row_array}>
  <table width="322" border="1" align="center" cellpadding="3" cellspacing="0">
  
          <tr>
          <td colspan="2"><{$row_array[sec1].cat_id}></td>
          </tr>
                <{section name=sec2 loop=$row_array[sec1].post}>
                  <tr>
                  <td width="160">post_title:<{$row_array[sec1].post[sec2].post_title}> </td>
                  <td width="160">post_date:<{$row_array[sec1].post[sec2].post_date}></td>
                  </tr>
                <{/section}>
  
  </table>
<{/section}>
</body>
</html>

test2.php

<?php
require_once('./include/db_fns.php');
include_once("./Smarty/libs/Smarty.class.php"); //包含Smarty类文件

$smarty = new Smarty(); //建立Smarty实例对象$Smarty
$smarty->template_dir = "./templates/dedecms";//设置模板目录
$smarty->compile_dir = "templates/templates_c"; //设置编译目录

$smarty->assign("template_url", "./");
$smarty->assign("$site_url", "http://www.gotop.net.ru");
$smarty->assign("$site_name", "文章管理系统");
$smarty->left_delimiter = "<{"; //设置左边界符
$smarty->right_delimiter = "}>"; //设置右边界符

$db_conn = db_connect();

$query = "SELECT cat_id,cat_name FROM categories ORDER BY cat_ID DESC";
$result = mysql_query($query);

while($row = mysql_fetch_array($result))
    {
        $query2="SELECT post_id, post_title, post_date
                FROM post
                WHERE post.post_category =$row[cat_id]
                AND post_status <> 'unpublish'
                ORDER BY post_date DESC LIMIT 5"; //这里加一个 LIMIT 5 不要用什么$i来控制
                              
        $result2=mysql_query($query2);
        while($row2 = mysql_fetch_array($result2))
            {
                $post[]=array('post_id'=>$row2['post_id'],
                              'post_title'=>$row2['post_title'],
                              'post_date'=>date('m-d',strtotime($row2[post_date]))
                              );
                        }                               
           
        $row_array[] = array('cat_id'=>$row['cat_id'],
                             'cat_name'=>$row['cat_name'],
                             'post'=>$post                         
                             );
                              
                unset($post);
                //$post[]在使用完后要注销,因为使用赋值语句对$post进行赋值时不会
                //将它清空,而是将新数组作为它的一个元素增加,为了不产生副作用,这里使用unset()是必需的.
                //如果不注销,第二次循环的结果会包含第一循环的数据,如分类2的文章跑到分类1中来。
                                                                                                       
    }

    echo"</br>row_array数组:</br>";
    print_r($row_array);//打出来看看有没有数据

$smarty->assign("row_array", $row_array);
$smarty->display("test2.htm");

?>
<select>
<option value="0">选择地区</option>
{section name=sec loop=$quyu}
<option value="{$quyu[sec].id}">{$quyu[sec].name}</option>

{foreach name=fenqu item=item from=$quyu[sec].fenqu}
<option value="{$item.id}">{$item.name}</option>
{/foreach}
{/section}
</select>

弄了很久才自己弄出来了,这个东西以后还是要经常使用的-_-||

    [quyu] => Array
        (
            [0] => Array
                (
                    [0] => 1
                    [id] => 1
                    [1] => 西湖区
                    [name] => 西湖区
                    [fenqu] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 1
                                    [id] => 1
                                    [1] => 西湖城区沿线
                                    [name] => 西湖城区沿线
                                )

                            [1] => Array
                                (
                                    [0] => 2
                                    [id] => 2
                                    [1] => 西湖景区
                                    [name] => 西湖景区
                                )

                         )

                )

            [1] => Array
                (
                    [0] => 2
                    [id] => 2
                    [1] => 上城区
                    [name] => 上城区
                    [fenqu] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 7
                                    [id] => 7
                                    [1] => 吴山广场/河坊街
                                    [name] => 吴山广场/河坊街
                                )

                            [1] => Array
                                (
                                    [0] => 8
                                    [id] => 8
                                    [1] => 解放路沿线
                                    [name] => 解放路沿线
                                )

                            [2] => Array
                                (
                                    [0] => 9
                                    [id] => 9
                                    [1] => 清泰街沿线
                                    [name] => 清泰街沿线
                                )

                            [3] => Array
                                (
                                    [0] => 10
                                    [id] => 10
                                    [1] => 湖滨路/平海路
                                    [name] => 湖滨路/平海路
                                )

                            [4] => Array
                                (
                                    [0] => 11
                                    [id] => 11
                                    [1] => 江城路沿线
                                    [name] => 江城路沿线
                                )

                        )

                )

            [2] => Array
                (
                    [0] => 3
                    [id] => 3
                    [1] => 下城区
                    [name] => 下城区
                    [fenqu] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 12
                                    [id] => 12
                                    [1] => 武林广场/杭百
                                    [name] => 武林广场/杭百
                                )

                            [1] => Array
                                (
                                    [0] => 13
                                    [id] => 13
                                    [1] => 凤起路沿线
                                    [name] => 凤起路沿线
                                )

                            [2] => Array
                                (
                                    [0] => 14
                                    [id] => 14
                                    [1] => 朝晖地区
                                    [name] => 朝晖地区
                                )

                            [3] => Array
                                (
                                    [0] => 15
                                    [id] => 15
                                    [1] => 庆春路沿线
                                    [name] => 庆春路沿线
                                )

                        )

                )

            [3] => Array
                (
                    [0] => 4
                    [id] => 4
                    [1] => 拱墅区
                    [name] => 拱墅区
                    [fenqu] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 16
                                    [id] => 16
                                    [1] => 信义坊
                                    [name] => 信义坊
                                )

                        )

                )

            [4] => Array
                (
                    [0] => 5
                    [id] => 5
                    [1] => 江干区
                    [name] => 江干区
                    [fenqu] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 17
                                    [id] => 17
                                    [1] => 城东
                                    [name] => 城东
                                )

                        )

                )

            [5] => Array
                (
                    [0] => 6
                    [id] => 6
                    [1] => 滨江区
                    [name] => 滨江区
                    [fenqu] => Array
                        (
                        )

                )

            [6] => Array
                (
                    [0] => 7
                    [id] => 7
                    [1] => 近郊
                    [name] => 近郊
                    [fenqu] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 18
                                    [id] => 18
                                    [1] => 萧山
                                    [name] => 萧山
                                )

                        )

                )

        )

)
posted @ 2012-06-18 16:35  Thinking In Code  阅读(1439)  评论(0编辑  收藏  举报