smarty的foreach问题

数组:Array( [0] => Array ( [newsid] => 3 [newtitle] => 政治新闻 [news] => asdqwd [newstime] => 2009-06-25 [id] => 2 [cid] => 46 ) [1] => Array ( [newsid] => 5 [newtitle] => 娱乐新闻 [news] => qwerzxvew [newstime] => 2009-06-25 [id] => 2 [cid] => 48 ) [2] => Array ( [newsid] => 4 [newtitle] => 体育新闻 [news] => cvbrt [newstime] => 2009-06-25 [id] => 2 [cid] => 52 ) [3] => [4] => )
index.tpl
<!-- 1 -->
<!--{foreach from=$items item="item" name="mainitem" key="k"}-->
<!--{assign var="i" value=$k%2+1}-->
<div class="db_centz_<!--{$i}--> fl">
<!-- dao -->
<div class="db_centz_<!--{$i}-->_1 fl more">
<p class="db_centz_<!--{$i}-->_1p fl"><!--{$item.cname}--></p>
<p class="db_centz_<!--{$i}-->_2p fr"><a href="#">more»</a></p>

</div>
<!-- dao -->
<div></div>

<!-- ul -->
<div class="db_centz_<!--{$i}-->_2 fl glk">
<!--{foreach from=$new item=newa}-->
<li><span>·</span><a href="#"><!--{$newa.newtitle}--></a></li>
<!--{/foreach}-->
</div>
<!-- ul -->
</div>
<!--{/foreach}-->
<!-- 1 -->
现有效果

我想问的是:我只想把标题为“政治新闻”放到新闻栏里,把“娱乐新闻”放到娱乐栏里,把“体育新闻”放到体育栏里!请问怎么写呢?
是的,我取出来的时候就是在变量里,这个是从一个自定义的函数里返回的...我现在不知道怎么把它再模板上输出出来...我想把数据库里的新闻分配到每个栏目里面...只输出一个可以做到了,全部的就出不来了!今天是第5天了,依然没有解决这个问题!

a.php

$items=array( array 

                          ( 'newsid' => 3, 'newtitle' => '政治新闻', 'news' => 'asdqwd', 'newstime' => '2009-06-25', 'id'=> 2, 'cid' => 46 

        ),

           array ( 'newsid' => 5, 'newtitle' => '娱乐新闻', 'news' => 'qwerzxvew', 'newstime' => '2009-06-25', 'id' => 2, 'cid' => 48

     ), 

    array ( 'newsid' => 4, 'newtitle' => '体育新闻', 'news' => 'cvbrt', 'newstime' => '2009-06-25', 'id' => 2, 'cid' => 52

     ), 

    '', 

    '' 

   );

$smarty->assign('item',$items);

$smarty->display('a.tpl');

a.tpl

<table border="1" bordercolor="#0000CC" cellpadding="0" cellspacing="0">

  <tr>

    <td>新闻   <a href="#">more</a></td>

    <td>娱乐  <a href="#">more</a></td>

  </tr>

  <tr>

 {section name=i loop=$item}

  {if $item[i].newtitle == '政治新闻'}

    <td>{$item[i].newtitle}<br /></td>

  {elseif $item[i].newtitle == '娱乐新闻'}

     <td>{$item[i].newtitle}<br /></td>

  {/if}

    {/section}

  </tr>

  <tr>

    <td>体育新闻    <a href="#">more</a></td>

    <td>游戏新闻    <a href="#">more</a></td>

  </tr>

  <tr>

 {section name=i loop=$item}

  {if $item[i].newtitle == '体育新闻'}

    <td>{$item[i].newtitle}<br /></td>

  {elseif $item[i].newtitle == '游戏新闻'}

     <td>{$item[i].newtitle}<br /></td>

  {/if}

    {/section}

  </tr>

</table>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-06-30
其实很简单

可以将这个数组赋值给一个数组变量

$arr = Array( [0] => Array ( [newsid] => 3 [newtitle] => 政治新闻 [news] => asdqwd [newstime] => 2009-06-25 [id] => 2 [cid] => 46 ) [1] => Array ( [newsid] => 5 [newtitle] => 娱乐新闻 [news] => qwerzxvew [newstime] => 2009-06-25 [id] => 2 [cid] => 48 ) [2] => Array ( [newsid] => 4 [newtitle] => 体育新闻 [news] => cvbrt [newstime] => 2009-06-25 [id] => 2 [cid] => 52 ) [3] => [4] => )

然后将里面的相关的数据取出来
比如你现在要 政治新闻
在 $ms->assign("item",$arr[0]);这样生成!
其它的依然是这样就可以了!

----------补充----
你说的---> "我想把数据库里的新闻分配到每个栏目里面...只输出一个可以做到了,全部的就出不来了" 想表达什么意思?

既然你现在这个地方己经分好类别了,那新闻也就是相关类别的新闻了
比如,你现在有 政治新闻 那么在这个类别里面全部都是关于 政治新闻 的相关新闻了!
第2个回答  2009-06-28
在 foreach 中加入 if 判断,可以参考如下程序段:
<{-- if $newsa.newtitle== "政治新闻" --}>
//执行相关操作
<{-- /if --}>

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网