Question

I am having an array created in PHP and assigning it to smarty template. The below example has 2 sets of data having 6 elements in each. Number of sets might vary dynamically.

 $formData = array (

    [homeTeam1] => homeTeam1
    [awayTeam1] => awayTeam1
    [homeTeamScore1] => homeTeamScore1
    [awayTeamScore1] => awayTeamScore1
    [gamePoints1] => gamePoints1
    [gameTime1] => gameTime1

    [homeTeam2] => homeTeam2
    [awayTeam2] => awayTeam2
    [homeTeamScore2] => homeTeamScore2
    [awayTeamScore2] => awayTeamScore2
    [gamePoints2] => gamePoints2
    [gameTime2] => gameTime2
)

These are somse HTML input fields created in PHP and wanted to display them in the Form for using edit.

I am using the below code to display those input fields in the form.

{foreach from=$formData key='formEl' item='value'}
<tr>
    <td>$formEl</td>
</tr>
{/foreach}

With the above code, the html input fields are displayed horizontally below one after other.

Current Output:

homeTeam1
awayTeam1
homeTeamScore1
awayTeamScore1
gamePoints1
gameTime1

Expected Output:

homeTeam1  awayTeam1  homeTeamScore1  awayTeamScore1  gamePoints1  gameTime1

I could not find the way to achieve this using {section} loop. I would be thankful for any help as I could not find any working example too.

Was it helpful?

Solution

If you want to display all the elements in one row just use this :

<tr>
{foreach from=$formData key='formEl' item='value'}
    <td>{$formEl}</td>
{/foreach}
</tr>

If you want to display each set of elements in one row namely 6 by row use this :

<tr>
{foreach name='myLoop' from=$formData key='formEl' item='value'}    
    {if $smarty.foreach.myLoop.index % 6 == 0 && $smarty.foreach.myLoop.index != 0}    
        </tr><tr>
    {/if}
    <td> {$smarty.foreach.myLoop.iteration}</td>
{/foreach}
</tr>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top