2012-04-18 65 views
3

Smarty是否可以在一个表中使用两个不同的foreach循环? 我尝试了一切,但它似乎并没有工作。 $ numrow数组有10个结果,但在所有10行中只显示一个结果。Smarty/PHP:为什么Smarty不能在一张表中使用两个foreach循环?

这是Smarty的常见问题吗?

<table width="500"> 

{foreach from=$categories item=category} 
    {if $category.fcType == 'm'} 
    <tr> 
     <td><strong>{$category.fcName}</strong></td> 
     <td>&nbsp</td> 
    {else} 
    <tr> 
     <td><a href="http://localhost/ZendFramework/forum/category?c={$category.fcID}">{$category.fcName}</a></td> 
     <td>{$category.fcDescription}</td> 
    {foreach from=$numrows item=numrow} 
     <td>{$numrow}</td> 
    {/foreach} 
    {/if} 

    {if !empty($category.fuUsername)} 
     <td>Nieuwste topic toegevoegd door {$category.fuUsername} op {$category.topic_date}</td> 
    {else} 
     <td>&nbsp</td> 
    {/if} 
    </tr>   
{/foreach} 

</table> 

PHP:

$getCat = isset($_GET['c']) ? $_GET['c'] : '';; 
    $getCat = htmlentities(mysql_real_escape_string($getCat));; 

    $query_cat = " 
    SELECT 
     forum_categories.fcID 
     ,forum_categories.fcName 
     ,forum_categories.fcDescription 
     ,forum_categories.fcParent 
     ,forum_categories.fcType 
     ,forum_users.fuUsername 
     ,DATE_FORMAT(forum_topics.ftDate,'%d-%m-%Y %H:%i:%s') AS topic_date 
    FROM 
     forum_categories 
    LEFT JOIN 
     forum_topics 
    ON 
     forum_topics.fcID = forum_categories.fcID 
    LEFT JOIN 
     forum_users 
    ON 
     forum_topics.fuID = forum_users.fuID  
    GROUP BY 
     forum_categories.fcID 
    ORDER BY 
     forum_categories.fcPos 
    "; 
    $exec_cat = mysql_query($query_cat); 
    if (($exec_cat) and mysql_num_rows($exec_cat)) 
    { 
     while($category = mysql_fetch_assoc($exec_cat)) 
     {   
      $query_count = " 
      SELECT 
       forum_topics.ftID 
      FROM 
       forum_categories 
      INNER JOIN 
       forum_topics 
      ON 
       forum_categories.fcID = forum_topics.fcID 
      WHERE forum_categories.fcID = '".$category['fcID']."' 
      "; 
      $exec_count = mysql_query($query_count); 
      $numrows = mysql_num_rows($exec_count); 
      $numrows[] = $numrows; 

      echo $numrows; 

      $this->view->assign("numrows", $numrows); 


      $categories[] = $category; 
      $this->view->assign("categories", $categories); 
     } 

    } 
    else 
    { 
     echo 'Er zijn nog geen categorieen aanwezig in de database.'; 
    } 
+0

好吧,那么如何在一张表中显示两个数组的结果而不会变得凌乱? – Eric1978 2012-04-18 11:32:18

+0

** {foreach}循环可以嵌套** *来源:* http://www.smarty.net/docsv2/en/language.function.foreach – Quasdunk 2012-04-18 11:32:23

+0

是的,我发现它自己,并删除了评论,太晚我猜: P你可以嵌套它们 – Bono 2012-04-18 11:33:19

回答

2
while($category = mysql_fetch_assoc($exec_cat)) 
    {   
     $query_count = " 
     SELECT 
      forum_topics.ftID 
     FROM 
      forum_categories 
     INNER JOIN 
      forum_topics 
     ON 
      forum_categories.fcID = forum_topics.fcID 
     WHERE forum_categories.fcID = '".$category['fcID']."' 
     "; 
     $exec_count = mysql_query($query_count); 
     $category['numrows'] = mysql_num_rows($exec_count); 
     $categories[] = $category; 
    } 
$this->view->assign("categories", $categories); 

而在你的Smarty TPL的:{foreach from=$category.numrows item=numrow}

难道你不BTW覆盖您的类别在循环分配? (在while循环之外放置最后一个赋值)

+0

您的意思是:$ this-> view-> assign(“categories”,$类别); while循环之外? – Eric1978 2012-04-18 11:54:44

+0

是的。我改变了我的帖子。你能试一下吗? – SativaNL 2012-04-18 11:58:22

+0

完美!!!!!非常感谢你,我尝试了很长时间:) – Eric1978 2012-04-18 12:01:36

0

尝试在您的foreach赋予不同的名称循环:

{foreach name = "loop1" from=$categories item='category'} 

{foreach name = "loop2" from=$numrows item='numrow'} 

我还没有尝试过自己,但看看它是否有任何区别

(btw,记得在特殊的html字符后面加上分号:应该是& nbsp;而不是& NBSP)

+0

中添加了我的PHP代码谢谢,但它没有解决问题 – Eric1978 2012-04-18 11:50:16

相关问题