Hello folks.

I have a simple task with Smarty output included.

I have an array : Array ( [0] => Array ( [username] => User1 [password] => MQbQTIE9T36Hc ) [1] => Array ( [username] => User2 [password] => K4MoQo.N9gF9M ) [2] => Array ( [username] => User3 [password] => 9XZN7gSAewrdE ) [3] => Array ( [username] => Mitko [password] => $apr1$TxRL5...$uZDBZgo6cTho/crXj.CGi. ) [4] => Array ( [username] => Gosho [password] => $apr1$ZhNRV/..$k8bMjUi7nstUVgI7P3/9p/ ) )

How can I put it into template for example :

<table>
 <td>
       <td>Username</td>
       <td>Passsword</td>
 </tr>
 <tr>
       <td> User1</td>
       <td> MQbQTIE9T36Hc </td>
 </tr>
 .....
</table>

Thanks in advance.

    Use a [man]foreach[/man] loop on the main array, realizing that the value in each iteration will be an array itself, e.g.:

    <table>
    <?php
    foreach($array as $value)
    {
       echo "<tr>\n";
       echo "<td>".$value['username']."</td>\n";
       echo "<td>".$value['password']."</td>\n";
       echo "</tr>\n";
    }
    ?>
    </table>
    

      Hi.

      I want to print the array in Smarty template using Smarty functions ...

        You'll have to hope a Smarty expert responds, then, as I've never worked with it.

          Put this in your TPL file:

          <table>
          {foreach from=$array item='value'}
              <tr>
                  <td>{$value.username}</td>
                  <td>{$value.password}</td>
              </tr>
          {/foreach}
          </table>
          

          Then from PHP:

          $smarty->assign('array', $array);
          $smarty->display('my_template.tpl');
          

          If speed becomes an issue, consider the Smarty-compatible TemplateLite

            Thanks mate, that work perfect 😉

              Write a Reply...