{"id":199514,"date":"2025-05-03T14:11:47","date_gmt":"2025-05-03T06:11:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/199514\/"},"modified":"2025-05-03T14:11:47","modified_gmt":"2025-05-03T06:11:47","slug":"ecshop%e6%a8%a1%e6%9d%bf%e7%b3%bb%e7%bb%9f%e6%8e%a7%e5%88%b6%e6%a0%87%e7%ad%be%e4%bb%8b%e7%bb%8d%e8%af%b4%e6%98%8e","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/199514\/","title":{"rendered":"ECSHOP\u6a21\u677f\u7cfb\u7edf\u63a7\u5236\u6807\u7b7e\u4ecb\u7ecd\u8bf4\u660e"},"content":{"rendered":"<p> ECSHOP\u6a21\u677f\u7cfb\u7edf\u63a7\u5236\u6807\u7b7e\u4ecb\u7ecd\u8bf4\u660e\uff0c\u672c\u6587\u5c06\u4e3a\u60a8\u4ecb\u7ecdecshop\u4e2d\u57fa\u672c\u7684\u63a7\u5236\u51fd\u6570\u6807\u7b7e\u7684\u4f7f\u7528\u53c2\u6570\u548c\u65b9\u6cd5\uff0c\u5176\u4e2d\u5305\u62ecif\u6807\u7b7e\u3001foreach\u6807\u7b7e\u3001for\u6807\u7b7e\u7b49\uff0c\u5176\u5b9eSmarty \u4e2d\u7684 if \u8bed\u53e5\u548c php \u4e2d\u7684 if \u8bed\u53e5\u4e00\u6837\u7075\u6d3b\u6613\u7528\uff0c\u5e76\u589e\u52a0\u4e86\u51e0\u4e2a\u7279\u6027\u4ee5\u9002\u5b9c\u6a21\u677f\u5f15\u64ce\uff0c if\u5fc5\u987b\u4e8e\/if \u6210\u5bf9\u51fa\u73b0. \u53ef\u4ee5\u4f7f\u7528 else \u548c elseif \u5b50\u53e5\u3002<\/p>\n<h2> if,elseif,else<\/h2>\n<p> \u63cf\u8ff0\uff1a<\/p>\n<p> Smarty \u4e2d\u7684 if \u8bed\u53e5\u548c php \u4e2d\u7684 if \u8bed\u53e5\u4e00\u6837\u7075\u6d3b\u6613\u7528\uff0c\u5e76\u589e\u52a0\u4e86\u51e0\u4e2a\u7279\u6027\u4ee5\u9002\u5b9c\u6a21\u677f\u5f15\u64ce. if\u5fc5\u987b\u4e8e \/if \u6210\u5bf9\u51fa\u73b0. \u53ef\u4ee5\u4f7f\u7528 else \u548c elseif \u5b50\u53e5. \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6761\u4ef6\u4fee\u9970\u8bcd\uff1aeq\u3001ne\u3001neq\u3001gt\u3001lt\u3001lte\u3001le\u3001gte\u3001ge\u3001is even\u3001is odd\u3001is not even\u3001is not odd\u3001not\u3001mod\u3001div by\u3001even by\u3001odd by\u3001==\u3001!=\u3001&gt;\u3001&lt;\u3001&lt;=\u3001&gt;=. \u4f7f\u7528\u8fd9\u4e9b\u4fee\u9970\u8bcd\u65f6\u5fc5\u987b\u548c\u53d8\u91cf\u6216\u5e38\u91cf\u7528\u7a7a\u683c\u683c\u5f00\u3002<\/p>\n<p> \u4f8b\u5b50\uff1a<\/p>\n<pre>\r\n{if $name eq \"Fred\"}\r\nWelcome Sir.\r\n{elseif $name eq \"Wilma\"}\r\nWelcome Ma'am.\r\n{else}\r\nWelcome, whatever you are.\r\n{\/if}\r\n{* an example with \"or\" logic *}\r\n{if $name eq \"Fred\" or $name eq \"Wilma\"}\r\n...\r\n{\/if}\r\n{* same as above *}\r\n{if $name == \"Fred\" || $name == \"Wilma\"}\r\n...\r\n{\/if}\r\n{* the following syntax will NOT work, conditional qualifiers\r\nmust be separated from surrounding elements by spaces *}\r\n{if $name==\"Fred\" || $name==\"Wilma\"}\r\n...\r\n{\/if}\r\n{* parenthesis are allowed *}\r\n{if ( $amount &lt; 0 or $amount &gt; 1000 ) and $volume &gt;= #minVolAmt#}\r\n...\r\n{\/if}\r\n{* you can also embed php function calls *}\r\n{if count($var) gt 0}\r\n...\r\n{\/if}\r\n{* test if values are even or odd *}\r\n{if $var is even}\r\n...\r\n{\/if}\r\n{if $var is odd}\r\n...\r\n{\/if}\r\n{if $var is not odd}\r\n...\r\n{\/if}\r\n{* test if var is divisible by 4 *}\r\n{if $var is div by 4}\r\n...\r\n{\/if}\r\n{* test if var is even, grouped by two. i.e.,\r\n0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc. *}\r\n{if $var is even by 2}\r\n...\r\n{\/if}\r\n{* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *}\r\n{if $var is even by 3}\r\n...\r\n{\/if}<\/pre>\n<h2> foreach,foreachelse<\/h2>\n<p> iteration\uff1a<\/p>\n<p> iteration \u7528\u4e8e\u663e\u793a\u5f53\u524d\u5faa\u73af\u7684\u6267\u884c\u6b21\u6570[\u5f85\u8003]<br \/> iteration \u603b\u662f\u4ece 1 \u5f00\u59cb\uff0c\u6bcf\u6267\u884c\u4e00\u6b21\u589e\u52a0 1.[\u5f85\u8003]<\/p>\n<p> first\uff1a<\/p>\n<p> \u5f53\u524d foreach \u5faa\u73af\u7b2c\u4e00\u6b21\u6267\u884c\u65f6 first \u88ab\u8bbe\u7f6e\u6210 true.<\/p>\n<p> last\uff1a<\/p>\n<p> \u5f53\u524d foreach \u5faa\u73af\u6267\u884c\u5230\u6700\u540e\u4e00\u904d\u65f6 last \u88ab\u8bbe\u7f6e\u6210 true.<\/p>\n<p> show\uff1a<\/p>\n<p> show \u662f foreach \u7684\u4e00\u4e2a\u53c2\u6570. \u53d6\u503c\u4e3a\u5e03\u5c14\u503c true \u6216 false. \u5982\u679c\u6307\u5b9a\u4e3a false \u8be5\u5faa\u73af\u4e0d\u663e\u793a\uff0c\u5982\u679c\u5faa\u73af\u6307\u5b9a\u4e86 foreachelse \u5b50\u53e5\uff0c\u8be5\u5b50\u53e5\u663e\u793a\u4e0e\u5426\u4e5f\u53d6\u51b3\u4e8e show \u7684\u53d6\u503c\u3002<\/p>\n<p> total\uff1a<\/p>\n<p> total \u7528\u4e8e\u663e\u793a\u5faa\u73af\u6267\u884c\u7684\u6b21\u6570\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u6216\u5faa\u73af\u6267\u884c\u540e\u8c03\u7528\u3002<\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td> \u5c5e\u6027<\/td>\n<td> \u7c7b\u578b<\/td>\n<td> \u662f\u5426\u5fc5\u987b<\/td>\n<td> \u7f3a\u7701\u503c<\/td>\n<td> \u63cf\u8ff0<\/td>\n<\/tr>\n<tr>\n<td> from<\/td>\n<td> string<\/td>\n<td> Yes<\/td>\n<td> n\/a<\/td>\n<td> \u5f85\u5faa\u73af\u6570\u7ec4\u7684\u540d\u79f0<\/td>\n<\/tr>\n<tr>\n<td> item<\/td>\n<td> string<\/td>\n<td> Yes<\/td>\n<td> n\/a<\/td>\n<td> \u5f53\u524d\u5904\u7406\u5143\u7d20\u7684\u53d8\u91cf\u540d\u79f0<\/td>\n<\/tr>\n<tr>\n<td> key<\/td>\n<td> string<\/td>\n<td> No<\/td>\n<td> n\/a<\/td>\n<td> \u5f53\u524d\u5904\u7406\u5143\u7d20\u7684\u952e\u540d<\/td>\n<\/tr>\n<tr>\n<td> name<\/td>\n<td> string<\/td>\n<td> No<\/td>\n<td> n\/a<\/td>\n<td> \u8be5\u5faa\u73af\u7684\u540d\u79f0\uff0c\u7528\u4e8e\u8bbf\u95ee\u8be5\u5faa\u73af<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> \u63cf\u8ff0\uff1a<\/p>\n<p> foreach \u662f\u9664 section \u4e4b\u5916\u5904\u7406\u5faa\u73af\u7684\u53e6\u4e00\u79cd\u65b9\u6848(\u6839\u636e\u4e0d\u540c\u9700\u8981\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6848)\u3002<\/p>\n<p> foreach \u7528\u4e8e\u5904\u7406\u7b80\u5355\u6570\u7ec4(\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u7684\u7c7b\u578b\u4e00\u81f4)\uff0c\u5b83\u7684\u683c\u5f0f\u6bd4 section \u7b80\u5355\u8bb8\u591a\uff0c\u7f3a\u70b9\u662f\u53ea\u80fd\u5904\u7406\u7b80\u5355\u6570\u7ec4\u3002<\/p>\n<p> foreach \u5fc5\u987b\u548c \/foreach \u6210\u5bf9\u4f7f\u7528\uff0c\u4e14\u5fc5\u987b\u6307\u5b9a from \u548c item \u5c5e\u6027\u3002<br \/> name \u5c5e\u6027\u53ef\u4ee5\u4efb\u610f\u6307\u5b9a(\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u7684\u7ec4\u5408)\u3002<br \/> foreach \u53ef\u4ee5\u5d4c\u5957\uff0c\u4f46\u5fc5\u987b\u4fdd\u8bc1\u5d4c\u5957\u4e2d\u7684 foreach \u540d\u79f0\u552f\u4e00\u3002<br \/> from \u5c5e\u6027(\u901a\u5e38\u662f\u6570\u7ec4)\u51b3\u5b9a\u5faa\u73af\u7684\u6b21\u6570\u3002<br \/> foreachelse \u8bed\u53e5\u5728 from \u53d8\u91cf\u6ca1\u6709\u503c\u7684\u65f6\u5019\u88ab\u6267\u884c\u3002<\/p>\n<p> \u4f8b\u5b501\uff1a<\/p>\n<pre>\r\n{* \u8be5\u4f8b\u5c06\u8f93\u51fa\u6570\u7ec4 $custid \u4e2d\u7684\u6240\u6709\u5143\u7d20\u7684\u503c *}\r\n{foreach from=$custid item=curr_id}\r\nid: {$curr_id}&lt;br&gt;\r\n{\/foreach}<\/pre>\n<p> \u8f93\u51fa\uff1a<\/p>\n<pre>\r\nid: 1000&lt;br&gt;\r\nid: 1001&lt;br&gt;\r\nid: 1002&lt;br&gt;<\/pre>\n<p> \u4f8b\u5b502:<\/p>\n<pre>\r\n{* The key contains the key for each looped value\r\nassignment looks like this:\r\n$smarty-&gt;assign(\"contacts\", array(array(\"phone\" =&gt; \"1\", \"fax\" =&gt; \"2\", \"cell\" =&gt; \"3\"),\r\narray(\"phone\" =&gt; \"555-4444\", \"fax\" =&gt; \"555-3333\", \"cell\" =&gt; \"760-1234\")));\r\n*}\r\n{* \u952e\u5c31\u662f\u6570\u7ec4\u7684\u4e0b\u6807\uff0c\u8bf7\u53c2\u770b\u5173\u4e8e\u6570\u7ec4\u7684\u89e3\u91ca *}\r\n{foreach name=outer item=contact from=$contacts}\r\n{foreach key=key item=item from=$contact}\r\n{$key}: {$item}&lt;br&gt;\r\n{\/foreach}\r\n{\/foreach}<\/pre>\n<p> \u8f93\u51fa\uff1a<\/p>\n<pre>\r\nphone: 1&lt;br&gt;\r\nfax: 2&lt;br&gt;\r\ncell: 3&lt;br&gt;\r\nphone: 555-4444&lt;br&gt;\r\nfax: 555-3333&lt;br&gt;\r\ncell: 760-1234&lt;br&gt;<\/pre>\n<p> foreach \u5faa\u73af\u6709\u81ea\u5df1\u7684\u53d8\u91cf\u540d\uff0c\u4f7f\u7528\u8be5\u53d8\u91cf\u540d\u53ef\u4ee5\u8bbf\u95ee\u8be5\u5faa\u73af. \u4f7f\u7528\u65b9\u6cd5\u4e3a{$smarty.foreach.foreachname.varname}\uff0c\u5176\u4e2d foreachname \u5373\u5728 foreach \u4e2d\u6307\u5b9a\u7684name \u5c5e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ECSHOP\u6a21\u677f\u7cfb\u7edf\u63a7\u5236\u6807\u7b7e\u4ecb\u7ecd&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-199514","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=199514"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199514\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=199514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=199514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=199514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}