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...
n00b0101Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NerdsOfTechTechnology ScientistCommented:
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

0
NerdsOfTechTechnology ScientistCommented:
-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

0
NerdsOfTechTechnology ScientistCommented:
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

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

NerdsOfTechTechnology ScientistCommented:
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

0
dukeofgamingCommented:
...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.
0
NerdsOfTechTechnology ScientistCommented:
alright I'll try my best at smarty syntax here... I hope this does the trick:
{foreach from=$zarray key=k1 item=v1}
 <h5>{$k1}</h5>
 <table>
 {foreach from=$v1.zid key=k2 item=v2}
  <tr>
   <td>{$v2}</td>
   <td>{$v1.count}</td>
  </tr>
 {/foreach}
 </table>
{/foreach}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NerdsOfTechTechnology ScientistCommented:
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
0
NerdsOfTechTechnology ScientistCommented:
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

0
dukeofgamingCommented:
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*)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.