Home » Smarty16 July 2008

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.

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.

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

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

Here is the complete code of index.tpl

Hope it helps.
Thanks.

Smarty

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Looking great.. but some error in code.. please verify