11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 19 • TEMPLATING WITH SMARTY{foreach $states as $key => $item}{$key}: {$item}{/foreach}This returns the following:OH: OhioCA: CaliforniaNY: New YorkAlthough the foreach function is indeed useful, you should definitely take a moment to learn aboutthe functionally similar yet considerably more powerful section function, introduced later in thissection.The foreachelse FunctionThe foreachelse function is used in conjunction with foreach, and operates much like the default tagdoes for strings, producing some alternative output if the array is empty. An example of a template usingforeachelse follows:{foreach $states as $key => $item}{$key}: $item}{foreachelse}No states matching your query were found.{/foreach}Note that foreachelse does not use a closing bracket; rather, it is embedded within foreach, muchlike an elseif is embedded within an if function.The section FunctionThe section function operates in a fashion much like an enhanced for/foreach, iterating over andoutputting a data array, although the syntax differs significantly. The term “enhanced” refers to the factthat it offers the same looping feature as the for/foreach constructs but also has numerous additionaloptions that allow you to exert greater control over the loop’s execution. These options are enabled viaseveral function parameters.Two parameters are required:name: Determines the name of the section. This is arbitrary and should be set towhatever you deem descriptive of the section’s purpose.loop: Sets the number of times the loop will iterate. This should be set to the samename as the array variable.Several optional parameters are also available:start: Determines the index position from which the iteration will begin. Forexample, if the array contains five values, and start is set to 3, the iteration will399

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!