Arrays or objects in php can all have for, foreach, while, and listeach loops out, and smarty also has this method. otherwise, php's array object data, smarty does not know how to display it. The usage of for, foreach, and while in php is a habit. at the beginning, the loop array in smarty is always forgotten.
Arrays or objects in php can all have for, foreach, while, and list each to loop out, and smarty also has this method, or else php's array object data, smarty does not know how to display it. The usage of for, foreach, and while in php is a habit. at the beginning, the loop array in smarty always forgets that it is better to remember it than to write it down. The variable symbol is bounded below, and I will use the default braces.
I. foreache, foreacheelse,/foreache method
1. it is another solution for processing loops outside the section (select different solutions based on different needs ). it is used to process a simple array (the element types in the array are the same). Its format is much simpler than section. The disadvantage is that it can only process a simple array.
2. foreach must be used in pairs with/foreach, and the from and item attributes must be specified.
3, foreach can be a trap, but name must be one
4. from: the name of the array to be cyclic, the subscript of the key array, the variable name of the currently processed item, and the name of the loop to be accessed.
5. the foreachelse statement is executed when the array object following the from statement has no value.
II. section, sectionelse,/section method
Section is used to traverse the data in the array. section labels must appear in pairs. the name and loop attributes must be set. the name can be any combination of letters, numbers, and underscores. nesting is allowed, but the nested name must be unique. the variable loop (usually an array) determines the number of times the loop is executed. to output a variable in a section loop, you must add the name variable included in the brackets after the variable. sectionelse is executed when the loop variable has no value.
The parameters that can be included in section are as follows:
1. name indicates the name of a single loop. required options
2. variable name of loop, required
3. KEY value starting from the start loop. the default value is from the first one,
4. step sets the number of hops in a loop. step = 2 only traverses elements with subscripts of 0, 2, and 4.
5. number of max cycles. the default value is 1,
6. does show the loop?
We can see from the above parameters that the subscript is not mentioned, and we often use the array subscript. However, smarty contains.
The following arrayname is the name of a single loop in the section.
A, $ smarty. section. arrayname. index array subscript, the root foreach key is similar
B, $ smarty. section. arrayname. index_prev the subscript root of the previous loop $ smarty. section. arrayname. loop is similar
C, $ smarty. section. arrayname. index_next subscript of the next loop
D, $ smarty. section. arrayname. iteration.
E, $ smarty. section. arrayname. first
F, $ smarty. section. arrayname. last loop
G, $ smarty. section. arrayname. show whether the loop is displayed
H, $ smarty. section. arrayname. total number of cycles
Section to get the values in the array object.
{Section name = arrayname loop = $ array}
{$ Array [arrayname. index]}, {$ array [arrayname]} are equivalent. it can be seen from the beginning. How important is a-h above?
3. Application example
1. one-dimensional array
View copy print?
- Array (
- "0" => 'home ',
- '1' => 'Who ',
- '2' => 'tank ',
- '3' => 'wh'
- );
- $ This-> tpl-> assign ("onearray", $ this-> onearray );
array ( "0" => 'home', '1' => 'who', '2'=> 'tank', '3'=> 'what' );$this->tpl->assign("onearray", $this->onearray);
A), foreach to read
View copy print?
- {Foreach from = $ onearray kkey = k item = value}
- One-Dimensional key = {$ k} one-dimensional value = {$ value}
- {Foreachelse}
- Nothing
- {/Foreach}
{Foreach from = $ onearray key = k item = value} One-Dimensional key = {$ k} one-dimensional value = {$ value}
{Foreachelse} nothing {/foreach}
The displayed result is
One-Dimensional key = 0 one-dimensional value = home
One-Dimensional key = 1-dimensional value = who
One-Dimensional key = 2-dimensional value = tank
One-Dimensional key = 3-dimensional value = what
B), section to read
View copy print?
- {Section name = one loop = $ onearray start = 0 step = 1}
- Index = {$ smarty. section. one. index },
- Index_prev = {$ smarty. section. one. index_prev },
- Index_next = {$ smarty. section. one. index_next },
- First = {$ smarty. section. one. first },
- Last = {$ smarty. section. one. last },
- Iteration = {$ smarty. section. one. iteration },
- Total = {$ smarty. section. one. total },
- Value = {$ onearray [one]}
- {Sectionelse}
- Nothing
- {/Section}
{section name=one loop=$onearray start=0 step=1} index={$smarty.section.one.index}, index_prev={$smarty.section.one.index_prev}, index_next={$smarty.section.one.index_next}, first={$smarty.section.one.first}, last={$smarty.section.one.last}, iteration ={$smarty.section.one.iteration}, total={$smarty.section.one.total}, value={$onearray[one]}
{sectionelse} nothing {/section}
The displayed result is
Index = 0, index_prev =-1, index_next = 1, first = 1, last =, iteration = 1, total = 4, value = home
Index = 1, index_prev = 0, index_next = 2, first =, last =, iteration = 2, total = 4, value = who
Index = 2, index_prev = 1, index_next = 3, first =, last =, iteration = 3, total = 4, value = tank
Index = 3, index_prev = 2, index_next = 4, first =, last = 1, iteration = 4, total = 4, value = what
2. two-dimensional array
View copy print?
- Array (
- "Test" => 'home ',
- '2' => 'Who ',
- Array (
- "Shanghai ",
- "Born" => "Anhui ",
- "Name" => "submarine Condor"
- ),
- Array (
- "1583456 ",
- "Fax" = & gt; "12345678 ",
- "Cell" => "13256478414"
- )
- );
- $ This-> tpl-> assign ("twoarray", $ this-> twoarray );
Array ("test" => 'home', '2' => 'wh', array ("Shanghai", "born" => "Anhui ", "name" => "sea bottom e"), array ("1583456", "fax" => "12345678", "cell" => "13256478414 ")); $ this-> tpl-> assign ("twoarray", $ this-> twoarray );
A), foreach
View copy print?
- {Foreach from = $ twoarray kkey = k item = value}
- {If is_array ($ value )}
- {Foreach from = $ value key = tk item = TV}
- Two-dimensional tkey = {$ tk} two-dimensional value = {$ TV}
- {Foreachelse}
- The two-dimensional array is empty.
- {/Foreach}
- {Else}
- One-Dimensional key = {$ k} one-dimensional value = {$ value}
- {/If}
- {Foreachelse}
- Nothing
- {/Foreach}
{Foreach from = $ twoarray key = k item = value} {if is_array ($ value )} {foreach from = $ value key = tk item = TV} two-dimensional tkey = {$ tk} two-dimensional value = {$ TV}
{Foreachelse} the two-dimensional array is null {/foreach} {else} One-Dimensional key = {$ k} one-dimensional value = {$ value}
{/If} {foreachelse} nothing {/foreach}
The result is as follows:
One-Dimensional key = test one-dimensional value = home
One-Dimensional key = 2-dimensional value = who
Two-dimensional tkey = 0 Two-dimensional value = Shanghai
Two-dimensional tkey = born two-dimensional value = Anhui
Two-dimensional tkey = name two-dimensional value = sea table
Two-dimensional tkey = 0 Two-dimensional value = 1583456
Two-dimensional tkey = fax two-dimensional value = 12345678
2d tkey = cell 2d value = 13256478414
B), section
View copy print?
- {Section loop = $ twoarray name = two}
- {If is_array ($ twoarray [two])}
- {Section loop = $ twoarray [two] name = aaa}
- Two-dimensional tkey = {$ smarty. section. aaa. index} two-dimensional value = {$ twoarray [two] [aaa]}
- {Sectionelse}
- The two-dimensional array is empty.
- {/Section}
- {Else}
- One-Dimensional key = {$ smarty. section. two. index} one-dimensional value = {$ twoarray [two]}
- {/If}
- {Sectionelse}
- Nothing
- {/Section}
{Section loop = $ twoarray name = two} {if is_array ($ twoarray [two])} {section loop = $ twoarray [two] name = aaa} two-dimensional tkey = {$ smarty. section. aaa. index} two-dimensional value = {$ twoarray [two] [aaa]}
{Sectionelse} the two-dimensional array is null {/section} {else} one-dimensional key ={$ smarty. section. two. index} one-dimensional value ={$ twoarray [two]}
{/If} {sectionelse} nothing {/section}
The result is as follows:
One-Dimensional key = 0 one-dimensional value =
One-Dimensional key = 1-dimensional value =
One-Dimensional key = 2-dimensional value = who
Two-dimensional tkey = 0 Two-dimensional value = Shanghai
Two-dimensional tkey = 1 two-dimensional value =
Two-dimensional tkey = 2 two-dimensional value =
If it is a one-dimensional array with subscripts, and it is not in the order of 0, 1, 2, it will not be able to get anything. Can this be understood as the reason for foreach's existence. However, generally, the data retrieved from the database is a two-dimensional array, and the outer layer starts with 0. Let's look at the example below.
View copy print?
- Array (
- Array (
- 'Name' => 'tank ',
- 'Sex' => 'male ',
- 'Old' => '28'
- ),
- Array (
- 'Name' => 'job ',
- 'Sex' => 'female ',
- 'Old' => '123'
- )
- );
Array ('name' => 'tank', 'sex' => 'male', 'old' => '28 '), array ('name' => 'job', 'sex' => Female, 'old' => '123 '));
Section loop
View copy print?
- {Section loop = $ twoarray name = two}
- Name = {$ twoarray [two]. name}, sex = {$ twoarray [two]. sex}, old = {$ twoarray [two]. old}
- {Sectionelse}
- Nothing
- {/Section}
{section loop=$twoarray name=two} name={$twoarray[two].name},sex={$twoarray[two].sex},old={$twoarray[two].old}
{sectionelse} nothing {/section}
Display result
Name = tank, sex = male, old = 28
Name = joyce, sex = female, old = 111.