?
Solved

PHP Smarty Foreach Loop

Posted on 2010-04-01
9
Medium Priority
?
488 Views
Last Modified: 2013-12-12
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...
0
Comment
Question by:n00b0101
  • 7
  • 2
9 Comments
 
LVL 21

Expert Comment

by:NerdsOfTech
ID: 29372737
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
 
LVL 21

Expert Comment

by:NerdsOfTech
ID: 29373086
-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
 
LVL 21

Expert Comment

by:NerdsOfTech
ID: 29373198
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 21

Expert Comment

by:NerdsOfTech
ID: 29373290
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
 
LVL 2

Expert Comment

by:dukeofgaming
ID: 29379056
...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
 
LVL 21

Accepted Solution

by:
NerdsOfTech earned 2000 total points
ID: 29382136
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
 
LVL 21

Expert Comment

by:NerdsOfTech
ID: 29387930
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
 
LVL 21

Expert Comment

by:NerdsOfTech
ID: 29388211
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
 
LVL 2

Expert Comment

by:dukeofgaming
ID: 29388819
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question