【问题标题】:Smarty / PHP: Why is it not possible to use two foreach loops in one table with Smarty?Smarty / PHP:为什么 Smarty 不能在一张表中使用两个 foreach 循环?
【发布时间】:2012-04-18 11:25:59
【问题描述】:

Smarty 是否可以在一张表中使用两个不同的 foreach 循环? 我尝试了一切,但它似乎不起作用。 $numrow 数组有 10 个结果,但在所有 10 行中只显示一个并且相同。

这是 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.';
    }

【问题讨论】:

  • 好吧,那么我怎样才能在一张表中显示两个数组的结果,而不弄乱?
  • {foreach} 循环可以嵌套 来源: smarty.net/docsv2/en/language.function.foreach
  • 是的,我自己找到并删除了评论,我猜为时已晚:P 你可以嵌套它们
  • 通过嵌套,您可以在 foreach 循环中显示来自其他 php associativa 数组的数组,所以有人可以解释一下它是如何与我的代码一起工作的吗?我对 smarty 不是很有经验
  • $categories 包含什么?我想重点是您在$categories 的每次迭代中都在迭代相同的变量$numrows - 数据永远不会改变,所以我希望看到$categories 中的许多项目与这个相同的行代码。通常$numrows 将来自$categories 的某些属性...

标签: php smarty


【解决方案1】:
 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}

顺便说一句,您不是在循环中覆盖您分配的类别吗? (将最后一个assign放在while循环之外)

【讨论】:

  • 你的意思是:$this->view->assign("categories", $categories);在while循环之外?
  • 是的。我改变了我的帖子。可以试试吗?
【解决方案2】:

尝试在你的 foreach 循环中使用不同的名称:

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

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

我自己没试过,但看看有什么不同

(顺便说一句,记得在特殊的 html 字符后加一个分号:应该是   而不是 &nbsp)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多