Link to home
Start Free TrialLog in
Avatar of n00b0101
n00b0101

asked on

PHP Smarty Foreach Loop

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...
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

you have to reference the key of zid as the index of count in forloop (sibling key setup) [3 levels nested]

$zarray is the name I placed on your array; please change this to your array name.

=nerdsoftech
foreach ($zarray as $k1 => $v1){
 echo '<h5>' . $k1 . '</h5>';
 echo '<table>';
 echo '<tr>';
 $v2 = $v1['zid'];
 foreach ($v2 as $k3 => $v3){
  echo '<td>' . $v3 . '</td>';
  echo '<td>' . $v1['count'][$k3] . '</td>'; // use key of sibling key [k3] from forloop
 }
 echo "</tr>";
 echo "</table>";
}

Open in new window

-corrected (fixed </tr> placement)
-added more descriptive comment of strategy
foreach ($zarray $k1 => $v1){
 echo "<h5>$k1</h5>";
 echo "<table>";
 $v2 = $v1['zid'];
 foreach ($v2 as $k3 => $v3){
  echo "<tr>";
  echo '<td>' . $v3 . '</td>';
  echo '<td>' . $v1['count'][$k3] . '</td>'; // use sibling key [k3] of zid from forloop to index the count subarray
  echo "</tr>";
 }
 echo "</table>";
}

Open in new window

normalized quotes
foreach ($zarray $k1 => $v1){
 echo '<h5>' . $k1 . '</h5>';
 echo '<table>';
 $v2 = $v1['zid'];
 foreach ($v2 as $k3 => $v3){
  echo '<tr>';
  echo '<td>' . $v3 . '</td>';
  echo '<td>' . $v1['count'][$k3] . '</td>'; // use sibling key [k3] of zid from forloop to index the count subarray
  echo '</tr>';
 }
 echo '</table>';
}

Open in new window

fixed foreach line 1
foreach ($zarray as $k1 => $v1){
 echo '<h5>' . $k1 . '</h5>';
 echo '<table>';
 $v2 = $v1['zid'];
 foreach ($v2 as $k3 => $v3){
  echo '<tr>';
  echo '<td>' . $v3 . '</td>';
  echo '<td>' . $v1['count'][$k3] . '</td>'; // use sibling key [k3] of zid from forloop to index the count subarray
  echo '</tr>';
 }
 echo '</table>';
}

Open in new window

...and he uses smarty, so he probably is looking a way to do it without raw PHP.

Here is the documentation for the foreach loops, which can be nested (seems that it is what you need): http://www.smarty.net/manual/en/language.function.foreach.php

This might also help: http://www.smarty.net/forums/viewtopic.php?p=6740

The other way of doing it is storing in a string what NerdsOfTech is echoing and assigning it to a smarty variable, but that beats the purpose of smarty.
ASKER CERTIFIED SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try replacing line 7 of my sniplet with:
   <td>{$v1.count.$k2}</td>

or

   <td>{$v1.count.k2}</td>

see if that does the trick
Also, if you have template security disabled then you could do raw PHP from smarty directly with my previous code:

>:)
{php}
foreach ($zarray as $k1 => $v1){
 echo '<h5>' . $k1 . '</h5>';
 echo '<table>';
 echo '<tr>';
 $v2 = $v1['zid'];
 foreach ($v2 as $k3 => $v3){
  echo '<td>' . $v3 . '</td>';
  echo '<td>' . $v1['count'][$k3] . '</td>'; // use key of sibling key [k3] from forloop
 }
 echo "</tr>";
 echo "</table>";
}
{/php}

Open in new window

Cool, I didn't know that one could actually use PHP in Smarty templates.

(Sad however, how some people keep using smarty insead of nice 3-layered patterns such as MVC *whistles*)