How can I autopopulate a table dynamically using PHP and Smarty?

I want to be able to create a table, setting a variable for the number of columns and using smarty have the table populated via an array.

Related tags: PHP

Credit: my own modified version of the solution found here http://www.phpinsider.com/smarty-forum/viewtopic.php?t1391

In the template file, use the following;

{if $data.sample_list}Sample table
{assign var="cols" value="5"}
{assign var="count" value="1"}
{section name=tr loop=$data.sample_list step=$cols}
{section name=td start=$smarty.section.tr.index max=$cols loop=$smarty.section.tr.index+$cols}
{if $count++ <= $data.sample_list_count}
{$data.sample_list[td].name}
{else}
{/if}
{/section}
{/section}
{/if}

Rating: 1

Propose a different solution...