Smarty: Selection list using foreach and section loop

In this article, I will be explaining about foreach and section loop used in Smarty. I will be using these loops for creating a selection list.

We can create selection list from a custom Smarty function html_options.


<select name="user">
{html_options values=$id output=$names selected="4"}
</select>

Instead of html_options, we can also use loops to create the selection list. There are two loops in Smarty:
1) Section
2) Foreach

1) Section: Section is used for looping over arrays of data. The name of the section can be anything made up of letters, numbers and underscores. The loop variable (usually an array of values) determines the number of times the section will iterate. sectionelse is executed when there are no values in the loop variable.


<select name="country">
{section name="getCountry" loop="$country"}
<option value="{$country[getCountry].country_id}" {if $country[getCountry].country_id eq '3'} selected {/if}>{$country[getCountry].country_name} </option>
{/section}
</select>

2) Foreach: foreach loop is an alternative to section loop. foreach is used to loop over a single associative array.


<select name="country2">
{foreach item=country2 from=$country}
<option value="{$country2.country_id}" {if $country2.country_id eq '3'}selected{/if}> {$country2.country_name} </option>
{/foreach}
</select>

You need to view my previous article on using smarty. This will help you understand the file and folder structure that I am using.

Here is the complete code of index.php


<?php
require_once('classes/ConnectSmarty.class.php');

// create an object of the class included above
$smarty = new ConnectSmarty;

// assign options arrays (for using html_options)
$smarty->assign('id', array(1,2,3,4,5));
$smarty->assign('names', array('Mukesh','Rajneesh','Chandra','Mohan','Jain'));

// assign associative array (for the use of section and foreach)
$smarty->assign('country',array(
		 array('country_id'=>'1','country_name'=>'India'),
		 array('country_id'=>'2','country_name'=>'Pakistan'),
		 array('country_id'=>'3','country_name'=>'Nepal'),
		 array('country_id'=>'4','country_name'=>'Srilanka'),
		 array('country_id'=>'5','country_name'=>'Bangladesh')
		 ));

// display the content
$smarty->display('index.tpl');
?>

Here is the complete code of index.tpl


<html>
<head>
<title> Homepage </title>
</head>
<body>

Using html_options: 

<select name="user">
{html_options values=$id output=$names selected="4"}
</select>
<p></p>

Using section:

<select name="country">
{section name="getCountry" loop="$country"}
<option value="{$country[getCountry].country_id}" {if $country[getCountry].country_id eq '3'}selected{/if}>{$country[getCountry].country_name} </option>
{/section}
</select>
<p></p>

Using foreach:

<select name="country2">
{foreach item=country2 from=$country}
   <option value="{$country2.country_id}" {if $country2.country_id eq '3'}selected{/if}> {$country2.country_name} </option>
{/foreach}
</select>
</body>
</html>

Hope it helps.
Thanks.