Smarty巢状资料呈现(采用section),字段显示不完整

字体: | 打印

我在win下使用phpstudy做的服务器

为什么子数组的元素不能显示?
可以肯定的不是编码问题。
曾用英文做过测试,同样子数组元素只显示第一个字母。


我的模板(test.php):

CODE:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>巢状循环测试</title>
</head>
<body>
<table width="250" border="0" align="center" cellpadding="3" cellspacing="0">
<{ section name=sec1 loop=$forum }>
<tr>
<td colspan="2"><{ $forum[sec1].category_name }></td></tr>
<{ section name=sec2 loop=$forum[sec1].topic }>
<tr>
<td width="25"></td>
<td width="224"><{ $forum[sec1].topic[sec2].topic_name }></td></tr>
<{ /section }>
<{ /section }>
</table>
</body>
</html>


php文件(db_conn.php):

CODE:

<?php
   require "main.php";
   // 先建立第一层数组
   $category_arr = array();
   $db = new mysqli('localhost','hyweb','hypw','smarty_test');
   $sq = "set names gb2312";
   $db->query($sq);
   $sq1 = "select * from category";
   $rs1 = $db->query($sq1);
   if(!$rs1) die($db->error());
//  抓取第一层循环的资料
   while($item_category = $rs1->fetch_assoc()){
      // 建立第二层数组
      $topic_arr = array();
      $sq2 = "select topic_name from topic where category_id=".$item_category['category_id'];
      $db->query($sq);
      $rs2 = $db->query($sq2);
      if(!$rs2) die($db->error());
      while($item_topic = $rs2->fetch_assoc()){
         array_push($topic_arr,$item_topic['topic_name']);
         //echo $item_topic['topic_name'];
      }
      // 把第二层数组指定为第一层数组所抓取的数据中的一员
      $item_category['topic'] = $topic_arr;
      // 把第一层数据压入第一层数组
      array_push($category_arr,$item_category);
    }
    $tpl->assign("forum",$category_arr);
    $tpl->display("test3.htm");

    // 使用foreach遍历$category_arr中的topic元素(是数组) 测试成功。
/*
    foreach($category_arr as $sub_arr){
       foreach($sub_arr as $key=>$value){
          if(is_array($value)){
              foreach($value as $i=>$sub_va)
                  echo $i.'->'.$sub_va.'<br />';
           }else{
              echo $key.'->'.$value.'<br />';
           }
         }
      }
*/
?>

首先,我确定从数据库中读出数据,并且生成数组,内容如下:

category_id->1
category_name->公告板
topic    0->站务公告

category_id->2
category_name->文学专区
topic     0->好书欣赏
            1->奇文共读

category_id->3
category_name->计算机专区
topic      0->软件
             1->硬件

从浏览器打开db_conn.php 结果如下:
公告板
    ?
文学专区
    ?
    ?
计算机专区
    ?
    ?



为什么子数组的元素不能显示?
可以肯定的不是编码问题。
曾用英文做过测试,同样子数组元素只显示第一个字母。


2008-07-05 编辑

我又写了一个php文件,不从mysql中抓取数据。内容如下:

CODE:

<?php
   require "main.php";
   $forum = array(array("category_id"=>1,
     "category_name"=>"公告区",
     "topic"=>array(array("topic_id"=>1,"topic_name"=>"站务公告"))),
     array("category_id"=>2, "category_name"=>"文学专区",
     "topic"=>array(array("topic_id"=>2,"topic_name"=>"好书欣赏"),
              array("topic_id"=>3,"topic_name"=>"奇文共读"))),
     array("category_id"=>3,"category_name"=>"计算机专区",
     "topic"=>array(array("topic_id"=>4,"topic_name"=>"硬件外围"),
              array("topic_id"=>"5","topic_name"=>"软件讨论"))));

      $tpl->assign("forum",$forum);
      $tpl->display("test3.htm");
?>


显示结果完全正常。如下
公告区
    站务公告
文学专区
    好书欣赏
    奇文共读
计算机专区
    硬件外围
    软件讨论



无奈+郁闷ing


[ 本帖最后由 hyeme 于 2008-7-5 18:14 编辑 ]

我也来说两句 查看全部评论 相关评论

  • ShiningRay (2008-7-04 19:13:58)

    巢状是什么?
  • hyeme (2008-7-04 19:16:36)

    论坛程序中的讨论主题区就算是巢状资料
  • fhjr999 (2008-7-04 20:34:08)

    确定标签没有写错吗??我怎么感觉不太对。

    这是一个很正常的功能,2层循环而已,没道理做不了。
  • fhjr999 (2008-7-04 20:36:34)

    把你最后得到的那个数组,print_r一下看看。
  • hyeme (2008-7-05 06:50:52)

    我测试了内容如下:
    echo '<pre>';
    print_r($category_arr);
    echo '</pre>';
    结果如下:
    Array
    (
        [0] => Array
            (
                [category_id] => 1
                [category_name] => 公告板
                [topic] => Array
                    (
                         [0] => 站务公告  
                    )
            )
        [1] => Array
            (
                [category_id] => 2
                [category_name] => 文学专区
                [topic] => Array
                    (
                        [0] => 好书欣赏
                        [1] => 奇文共读
                    )
            )
        [2] => Array
            (
                [category_id] => 3
                [category_name] => 计算机专区
                [topic] => Array
                    (
                        [0] => 软件
                        [1] => 硬件
                    )
            )
    )

    能够完整展现。

    有这么个现象:如果php文件不从mysql中抓取数据,而是自身定义一个数组,则输出不会有这个问题。

    [ 本帖最后由 hyeme 于 2008-7-5 18:05 编辑 ]
  • netbuddy (2008-7-05 16:56:19)

    $item_category[0]

    array_push($topic_arr,$item_topic['topic_name']);
    这个把$topic_arr变成了2维数组

    $item_category['topic'] = $topic_arr;
    这个又把$item_category变成了3维的

    array_push($category_arr,$item_category);
    最后$category_arr变成了4维的
    感觉数组输出不对,你直接这样输出来瞧瞧


    一个category_id对应多个topic_name,这个应该是一个这样机构的2维数组
    $arr=arr['category_id']['topic_name']
    这样试试呢?
  • hyeme (2008-7-05 19:32:51)

    我研究了一下你的建议,给我了一个新思路。
    就是说这样构建数组:
    $category_arr(($category_id,$category_name,$topic1,$topic2),
    ($category_id,$category_name,$topic1,$topic2,$topic3),
    ($category_id,$category_name,$topic1,$topic2,$topic3))
    这样,数组就简单多了。但是在模板里处理起来比较麻烦,比如做内循环抓取每一个元素,就必须判断哪个是id,哪个是name,哪个是topic。

    首先数组结构是这样的:
    $category_arr(($category_id,$category_name,$topic($topic_id,$topic_name)),
              ( $category_id,$category_name,$topic($topic_id,$topic_name)),
              ( $category_id,$category_name,$topic($topic_id,$topic_name)))
    其次,构造这个数组的思路:
    1 从topic数据表循环抓出topic_name,并存放在数组topic中。
    2 把数组topic作为一个元素存放到$item_category数组中,$item_category数组共含有3个元素,分别是category_id, category_name, topic。
    3 最后把$item_category作为一个元素压入$category_arr数组中。
    4 这样执行一次1-3步,$category_arr数组就多一个元素。

    这样结构比较明朗,抓取方便。
  • suihr (2008-7-08 09:28:42)

    学习
  • hyeme (2008-7-08 14:16:29)

    谢了老兄,小弟就是看手册学着来的。呵呵,研究了一下你给的代码,受益匪浅啊。逻辑确实清晰多了呵呵,我把简单的事情整复杂了。
    交个朋友吧
  • 袁过 (2008-7-08 23:58:38)

    //PHP代码
            $sonnav=array();
            $menu=assignsome('menu','module',"parentid=-1 order by df desc");//从表module里按parentid=-1条件读出父栏目数组,把数组注册成samrty中的menu变量,返回查询后的数组$menu
            for($i=0;$i<count($menu);$i++){
                    $sonnav[$i]=getsomeinfo('module',"parentid='".$menu[$i][id]."' order by id asc");//从表module中读出二级子栏目
            }
            $ttt->assign('sonnav',$sonnav);//把子栏目注册成smarty变量

    //smarty模板文件
    <{section name=li loop=$menu}>
    <li class="menu2" ><{$menu[li].zh}>        
    <div class="list">
    <{section name=lii loop=$sonnav[$smarty.section.li.index]}>
    <a href="<{$sonnav[$smarty.section.li.index][lii].links}>" class="cWhite" target="main"><{$sonnav[$smarty.section.li.index][lii].zh}></a><br />
    <{/section}>
    </div>
    </li>
    <{/section}>

    道理就是这样的...

    [ 本帖最后由 袁过 于 2008-7-9 00:00 编辑 ]