Eric1978
Eric1978

Reputation: 83

Smarty / PHP: Why is it not possible to use two foreach loops in one table with Smarty?

Is it possible to use two different foreach loops in one table with Smarty? I tried everything but it just doesnt seem to work. The $numrow array has 10 results, but only shows one and the same in all 10 rows.

Is this a common problem with 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.';
    }

Upvotes: 3

Views: 1523

Answers (2)

SativaNL
SativaNL

Reputation: 541

 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);

And in your Smarty TPL: {foreach from=$category.numrows item=numrow}

Aren't you BTW overwriting your categories assign in your loop? (Place the last assign outside the while loop)

Upvotes: 2

periklis
periklis

Reputation: 10188

Try giving a different name in your foreach loops:

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

and

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

I haven't tried it myself, but see if it makes any difference

(btw, remember to put a semicolon after the special html chars: it should be &nbsp; instead of &nbsp)

Upvotes: 0

Related Questions