Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using smarty for my site, and I'm trying to loop through an array to print out table rows...

The array looks like this:

Array
(
    [TM98800G] => Array
        (
            [zid] => Array
                (
                    [0] => 90001
                    [1] => 90002
                    [2] => 90003
                    [3] => 90004
                    [4] => 90005
                )

            [count] => Array
                (
                    [0] => 10
                    [1] => 10
                    [2] => 20
                    [3] => 25
                    [4] => 15
                )

        )
    [TM76654G] => Array
        (
            [zid] => Array
                (
                    [0] => 90301
                    [1] => 90302
                    [2] => 90303
                    [3] => 90304
                    [4] => 90305
                )

            [count] => Array
                (
                    [0] => 25
                    [1] => 25
                    [2] => 20
                    [3] => 35
                    [4] => 45
                )

        )
)

I'm trying to loop through this and print out tables:

<h5>TM98800G </h5>

<table>
<tr>
  <td>90001</td>
  <td>10</td>
</tr>

<tr>
  <td>90002</td>
  <td>10</td>
</tr>

<tr>
  <td>90003</td>
  <td>20</td>
</tr>

<tr>
  <td>90004</td>
  <td>25</td>
</tr>

<tr>
  <td>90005</td>
  <td>15</td>
</tr>
</table>

<h5>TM76654G</h5>
<table>
<tr>
  <td>90301</td>
  <td>25</td>
</tr>

<tr>
  <td>90302</td>
  <td>25</td>
</tr>

<tr>
  <td>90303</td>
  <td>20</td>
</tr>

<tr>
  <td>90304</td>
  <td>35</td>
</tr>

<tr>
  <td>90305</td>
  <td>45</td>
</tr>
</table>

I tried nested foreach statements and played with sections, but I can't figure out how to loop through it correctly...

share|improve this question

2 Answers

up vote 3 down vote accepted
{foreach from=$array key=header item=table}
<h5>{$header}</h5>

<table>
    {foreach from=$table.zid key=k item=zid}
    <tr>
        <td>{$zid}</td>
        <td>{$table.count.$k}</td>
    </tr>
    {/foreach}
</table>
{/foreach}

Should do it I think.

share|improve this answer
Sadly, no... I get this: syntax error: unrecognized tag: $table.count[$k] – TwixxyKit Apr 1 '10 at 18:12
@Knock What version of Smarty are you using? – anomareh Apr 1 '10 at 18:13
Taking out the backticks worked! Thanks a bunch :) – TwixxyKit Apr 1 '10 at 18:13
@Knock yeah just realized. Forgot you only need the backticks if you're embedding a var in quotes. No prob. – anomareh Apr 1 '10 at 18:15
Note that if you are using Smarty3, you can simply use {foreach $array as $table} – Andrew Moore Apr 1 '10 at 18:30
foreach($array as $heading => $attrs) {
    echo "<h5>" . $heading . "</h5>";
    echo "<table>";
    for($i=0; $i < count($attrs['zid']); $i++) { // will work as long 
                                                 // as zid and count will 
                                                 // always be the same size
        echo "<tr>";
        echo "<td>" . $attrs['zid'][$i] . "</td>";
        echo "<td>" . $attrs['count'][$i] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}
share|improve this answer
1  
That's not smarty. – Chad Birch Apr 1 '10 at 18:03
yeah, I know how to do it in php, just having trouble figuring out the smarty way... – TwixxyKit Apr 1 '10 at 18:06
Ah, i'm sorry, i just figured i'd throw a general solution out there – Paul Woolcock Apr 1 '10 at 18:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.