<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                <table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><th colspan="3" align="center">Smarty - the compiling PHP template engine</th></tr><tr><td width="25%" align="left" valign="bottom"><a href="language.function.for.html" accesskey="P">Prev</a></td> <td width="50%" align="center" valign="bottom">Chapter 7. Built-in Functions[第七章.內置函數]</td> <td width="25%" align="right" valign="bottom"><a href="language.function.function.html" accesskey="N">Next</a></td></tr></table> # {foreach},{foreachelse}遍歷 **Table of Contents目錄**[@index](#)[@iteration](#)[@first](#)[@last](#)[@show](#)[@total](#) {foreach} is used for looping over arrays of data. {foreach} has a simpler and cleaner syntax than the {section} loop, and can also loop over associative arrays. {foreach $arrayvar as $itemvar} {foreach $arrayvar as $keyvar=>$itemvar} <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> This foreach syntax does not accept any named attributes. This syntax is new to Smarty 3, however the Smarty 2.x syntax {foreach from=$myarray key="mykey" item="myitem"} is still supported.<br/> foreach語法不能接受任何屬性名,這是Smarty3新增的語法,但Smarty2.x中的{foreach from=$myarray key="mykey" item="myitem"}語法仍受支持。</td> </tr></table> * {foreach} loops can be nested. * The array variable, usually an array of values, determines the number of times {foreach} will loop. You can also pass an integer for arbitrary loops. * {foreachelse} is executed when there are no values in the array variable. * {foreach} properties are @index, @iteration, @first, @last, @show, @total. * Instead of specifying the key variable you can access the current key of the loop item by {$item@key} (see examples below). {foreach}用來遍歷數據數組,{foreach}與[{section}](#)循環相比更簡單、語法更干凈,也可以用來遍歷關聯數組。 {foreach $arrayvar as $itemvar} {foreach $arrayvar as $keyvar=>$itemvar} {foreach}循環可以嵌套; 數組變量通常是(另)一個數組的值,用來指導循環的次數,你可以為循環傳遞一個整數; 當數組變量無值時執行{foreachelse}; {foreach}的屬性是@index、@iteration、@first、@last、@show、@total; 可以用循環項目中的當前鍵({$item@key})代替鍵值變量(參見下例)。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> The $var@property syntax is new to Smarty 3, however when using the Smarty 2 {foreach from=$myarray key="mykey" item="myitem"} style syntax, the $smarty.foreach.name.property syntax is still supported.<br/> $var@property是Smarty3的新語法,但使用Smarty2的{foreach from=$myarray key="mykey" item="myitem"}風格語法時$smarty.foreach.name.property語法仍然受支持。</td> </tr></table> <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> Although you can retrieve the array key with the syntax {foreach $myArray as $myKey=&gt; $myValue}, the key is always available as $myValue@key within the foreach loop. <br/> 雖然你可以用{foreach $myArray as $myKey=&gt; $myValue}檢索數組鍵,但foreach循環中的$myValue@key仍然有效。</td> </tr></table> **Option Flags: ** | Name | Description | |-----|-----| | from | Disables caching of the {foreach} loop | **選項標記:** | 名稱 | 描述 | |-----|-----| | from | {foreach}循環不能緩存 | <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN2730" id="AEN2730"/> <b><span class="PROGRAMLISTING">Example 7.30. A simple {foreach} loop</span><br/> 例 7-30. {foreach}循環的簡單例子</b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">&lt;?php$arr = array('red', 'green', 'blue');$smarty-&gt;assign('myColors', $arr);?&gt; Template to output $myColors in an un-ordered list&lt;ul&gt;{foreach $myColors as $color} &lt;li&gt;{$color}&lt;/li&gt;{/foreach}&lt;/ul&gt; The above example will output:&lt;ul&gt; &lt;li&gt;red&lt;/li&gt; &lt;li&gt;green&lt;/li&gt; &lt;li&gt;blue&lt;/li&gt;&lt;/ul&gt;</pre></td></tr></table></div></td></tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <p><b>Example 7.31. Demonstrates the an additional key variable<br/> 例 7-31. 加了鍵變量的演示</b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <p>&lt;?php<br/> $people = array('fname' =&gt; 'John', 'lname' =&gt; 'Doe', 'email' =&gt; 'j.doe@example.com');<br/> $smarty-&gt;assign('myPeople', $people);<br/> ?&gt;<br/><br/> Template to output $myArray as key/value pairs. //鍵值對 <br/> &lt;ul&gt;<br/> {foreach $myPeople as $value}<br/> &lt;li&gt;{$value@key}: {$value}&lt;/li&gt;<br/> {/foreach}<br/> &lt;/ul&gt;<br/><br/> The above example will output:</p> <p>&lt;ul&gt;<br/> &lt;li&gt;fname: John&lt;/li&gt;<br/> &lt;li&gt;lname: Doe&lt;/li&gt;<br/> &lt;li&gt;email: j.doe@example.com&lt;/li&gt;<br/> &lt;/ul&gt;</p> </td></tr></table></div></td></tr></table> **Example 7.32. {foreach} with nested item and key 例 7-32. 嵌套了item和key的{foreach}** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> Assign an array to Smarty, the key contains the key for each looped value.<br/> &lt;?php<br/>$smarty-&gt;assign('contacts', array(array('phone' =&gt; '555-555-1234',<br/>'fax' =&gt; '555-555-5678',<br/>'cell' =&gt; '555-555-0357'),<br/>array('phone' =&gt; '800-555-4444',<br/>'fax' =&gt; '800-555-3333',<br/>'cell' =&gt; '800-555-2222')<br/>));<br/>?&gt;<br/><br/>The template to output $contact.<br/>{* key always available as a property *} //{*鍵常常是個有效屬性*} <br/>{foreach $contacts as $contact}<br/>{foreach $contact as $value}<br/>{$value@key}: {$value}<br/>{/foreach}<br/>{/foreach}<br/>{* accessing key the PHP syntax alternate *} //{* 交替php語法訪問鍵 *} <br/>{foreach $contacts as $contact}<br/>{foreach $contact as $key =&gt; $value}<br/>{$key}: {$value}<br/>{/foreach}<br/>{/foreach}<br/><br/>Either of the above examples will output:<br/>phone: 555-555-1234<br/>fax: 555-555-5678<br/>cell: 555-555-0357<br/>phone: 800-555-4444<br/>fax: 800-555-3333<br/>cell: 800-555-2222 </p> </td> </tr></table> **Example 7.33. Database example with {foreachelse} 例 7-33. {foreachelse}的演示例子 ** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> A database (PDO) example of looping over search results. This example is looping over a PHP iterator instead of an array().<br/><span class="STYLE2">一個遍歷數據庫(PDO)搜索結果的例子,本例是用php迭代器代替數組遍歷。</span><br/> &lt;?php <br/>include('Smarty.class.php'); <br/>$smarty = new Smarty; <br/>$dsn = 'mysql:host=localhost;dbname=test'; <br/>$login = 'test'; <br/>$passwd = 'test'; <br/>// setting PDO to use buffered queries in mysql is <br/>// important if you plan on using multiple result cursors <br/>// in the template.<br/>//如果你打算在模版中使用多重查詢結果游標,那么使用mysql時設置PDO的緩沖查詢是非常重要的。<br/> $db = new PDO($dsn, $login, $passwd, array( <br/> PDO::MYSQL_ATTR_USE_BUFFERED_QUERY =&gt; true)); <br/> $res = $db-&gt;prepare("select * from users"); <br/> $res-&gt;execute(); <br/> $res-&gt;setFetchMode(PDO::FETCH_LAZY); <br/> // assign to smarty <br/> $smarty-&gt;assign('res',$res); <br/> $smarty-&gt;display('index.tpl');?&gt;<br/> ?&gt;<br/><br/> {foreach $res as $r} <br/> {$r.id} <br/> {$r.name}<br/> {foreachelse}<br/> .. no results .. <br/> {/foreach}</p> </td> </tr></table> The above is assuming the results contain the columns named id and name. What is the advantage of an iterator vs. looping over a plain old array? With an array, all the results are accumulated into memory before being looped. With an iterator, each result is loaded/released within the loop. This saves processing time and memory, especially for very large result sets. 上述(例子)假設了結果中包含了id、name列。 迭代器PK遍歷簡樸老舊數組的優勢在哪?對于數組,循環結束前所有結果都累積到內存中;對于迭代器,循環時每一結果都重復裝載/釋放的過程,這樣減少了處理時間和內存消耗,特別是對于巨型結果集。 ? @index index contains the current array index, starting with zero. 包含當前數組的下標,開始時為0。 **Example 7.34. index example 例 7-34. index例子 ** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> {* output empty row on the 4th iteration (when index is 3) *}<br/> &lt;table&gt;<br/> {foreach $items as $i}<br/>{if $i@index eq 3}<br/>{* put empty table row *}<br/>&lt;tr&gt;&lt;td&gt;nbsp;&lt;/td&gt;&lt;/tr&gt;<br/>{/if}<br/>&lt;tr&gt;&lt;td&gt;{$i.label}&lt;/td&gt;&lt;/tr&gt;<br/>{/foreach}<br/>&lt;/table&gt;</p></td> </tr></table> ? @iteration iteration contains the current loop iteration and always starts at one, unlike index. It is incremented by one on each iteration. iteration包含當前循環的迭代,總是以1開始,這點與index不同。每迭代一次值自動加1。 **Example 7.35. iteration example: is div by ****例 7-35. iteration例子 :is div by ** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> The "is div by" operator can be used to detect a specific iteration. Here we bold-face the name every 4th iteration.<br/> "is div by"操作符可能用來偵察特殊迭代,這里每迭代4次時name用粗黑字體顯示。 <br/> {foreach $myNames as $name}<br/>{if $name@iteration is div by 4}<br/>&lt;b&gt;{$name}&lt;/b&gt;<br/>{/if}<br/>{$name}<br/>{/foreach}</p></td> </tr></table> **Example 7.36. iteration example: is even/odd by ****例 7-36. iteration例子 :is even/odd by ** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> The "is even by" and "is odd by" operators can be used to alternate something every so many iterations.<br/> Choosing between even or odd rotates which one starts. Here we switch the font color every 3rd iteration.<br/> "is even by"和"is odd by"操作符可以用來在每多少迭代時交替什么什么...。<br/> 當奇數或偶數開始時選擇even或odd中的一個來交替,這里我們選擇每迭代3次時更改顯示顏色。</p> <p> <br/> {foreach $myNames as $name}<br/> {if $name@iteration is even by 3}<br/> &lt;span style="color: #000"&gt;{$name}&lt;/span&gt;<br/> {else}<br/> &lt;span style="color: #eee"&gt;{$name}&lt;/span&gt;<br/> {/if}<br/> {/foreach}<br/><br/><br/> This will output something similar to this:<br/> &lt;span style="color: #000"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #000"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #000"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #eee"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #eee"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #eee"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #000"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #000"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #000"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #eee"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #eee"&gt;...&lt;/span&gt;<br/> &lt;span style="color: #eee"&gt;...&lt;/span&gt;<br/> ...</p></td> </tr></table> ? @first first is TRUE if the current {foreach} iteration is the initial one. Here we display a table header row on the first iteration. 當{foreach}循環第一個時first為真。這里我們演示當第一次迭代表格頭所在行。 **Example 7.37. first property example ****例 7-37. first屬性的例子 ** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> {* show table header at first iteration *}<br/> &lt;table&gt;<br/> {foreach $items as $i}<br/>{if $i@first}<br/>&lt;tr&gt;<br/>&lt;th&gt;key&lt;/td&gt;<br/>&lt;th&gt;name&lt;/td&gt;<br/>&lt;/tr&gt;<br/>{/if}<br/>&lt;tr&gt;<br/>&lt;td&gt;{$i@key}&lt;/td&gt;<br/>&lt;td&gt;{$i.name}&lt;/td&gt;<br/>&lt;/tr&gt;<br/>{/foreach}<br/>&lt;/table&gt;</p></td> </tr></table> ? @last last is set to TRUE if the current {foreach} iteration is the final one. Here we display a horizontal rule on the last iteration. 當{foreach}迭代到最后時last為真。這里我們演示當迭代到最后時顯示一條橫線。 **Example 7.38. last property example ****例 7-38. last屬性的例子 ** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> {* Add horizontal rule at end of list *}<br/> {foreach $items as $item}<br/>&lt;a href="#{$item.id}"&gt;{$item.name}&lt;/a&gt;{if $prod@last}&lt;hr&gt;{else},{/if}<br/>{foreachelse}<br/>... no items to loop ...<br/>{/foreach}</p></td> </tr></table> ? @show The show* show* property can be used after the execution of a {foreach} loop to detect if data has been displayed or not. show is a boolean value. *show*屬性用在檢測{foreach}循環是否無數據顯示,show是個布爾值(true or false)。 **Example 7.39. show property example ****例 7-39. showt屬性的例子 ** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>&lt;ul&gt;<br/> {foreach $myArray as $name}<br/>&lt;li&gt;{$name}&lt;/li&gt;<br/>{/foreach}<br/>&lt;/ul&gt;<br/>{if $name@show} do something here if the array contained data {/if}</p></td> </tr></table> ? @total total contains the number of iterations that this {foreach} will loop. This can be used inside or after the {foreach}. total包含{foreach}循環的總數(整數),可以用在{forach}里面或后面。 **Example 7.40. total property example ****例 7-39. total屬性的例子 ** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> {* show number of rows at end *} //{* 最后顯示總行數 *} <br/> {foreach $items as $item}<br/>{$item.name}&lt;hr/&gt;<br/>{if $prod@last}<br/>&lt;div id="total"&gt;{$prod@total} items&lt;/div&gt;<br/>{/if}<br/>{foreachelse}<br/>... no items to loop ...<br/>{/foreach}</p></td> </tr></table> 參見[{section}](#)和[{for}](#)。 <table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="language.function.for.html" accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="language.function.function.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">{for}<br/> 循環</td><td width="34%" align="center" valign="top"><a href="language.builtin.functions.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">{function}<br/> 函數</td></tr></table>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看