{"id":22952,"date":"2024-11-21T10:36:56","date_gmt":"2024-11-21T02:36:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22952\/"},"modified":"2024-11-21T10:36:56","modified_gmt":"2024-11-21T02:36:56","slug":"thinkphp%e6%97%a0%e9%99%90%e6%9e%81%e5%88%86%e7%b1%bb%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22952\/","title":{"rendered":"thinkphp\u65e0\u9650\u6781\u5206\u7c7b\u7684\u65b9\u6cd5"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u884c\u4e1a\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u5728\u7f51\u7ad9\u5f00\u53d1\u4e2d\u4f7f\u7528\u540e\u53f0\u7ba1\u7406\u7cfb\u7edf\u5df2\u6210\u4e3a\u5fc5\u9700\u54c1\u3002\u7136\u800c\uff0c\u7f51\u7ad9\u5728\u7ba1\u7406\u8fc7\u7a0b\u4e2d\uff0c\u968f\u7740\u6570\u636e\u7684\u4e0d\u65ad\u79ef\u7d2f\uff0c\u5206\u7c7b\u8d8a\u6765\u8d8a\u591a\uff0c\u4f7f\u5f97\u5728\u9875\u9762\u4e0a\u5c55\u793a\u65f6\uff0c\u9700\u6309\u7167\u5206\u7c7b\u8fdb\u884c\u5f52\u7eb3\u6574\u7406\u3002\u5982\u6b64\u4e00\u6765\uff0c\u4fbf\u9700\u8981\u4f7f\u7528\u5230\u5206\u7c7b\u7ba1\u7406\u7684\u65b9\u6cd5\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528thinkphp\u6846\u67b6\u7684\u65e0\u9650\u6781\u5206\u7c7b\u65b9\u6cd5\uff0c\u6765\u5b9e\u73b0\u7f51\u7ad9\u5206\u7c7b\u7684\u7ba1\u7406\u3002<\/p>\n<ol>\n<li>\u4ec0\u4e48\u662fthinkphp\u6846\u67b6\uff1f<\/li>\n<\/ol>\n<p>thinkphp\u662f\u4e00\u6b3e\u4f18\u79c0\u7684\u6846\u67b6\u6280\u672f\uff0c\u7ee7\u627f\u4e86\u4f20\u7edf\u7f51\u7ad9\u5f00\u53d1\u6846\u67b6\u7684\u4f18\u70b9\uff0c\u540c\u65f6\u878d\u5408\u4e86\u5f53\u524d\u4e3b\u6d41\u7684\u6280\u672f\u65b9\u6cd5\u3002thinkphp\u6846\u67b6\u4e0d\u4ec5\u4ec5\u5177\u5907\u9ad8\u6548\u548c\u65b9\u4fbf\uff0c\u8fd8\u5177\u6709\u4f18\u79c0\u7684\u6269\u5c55\u6027\u548c\u517c\u5bb9\u6027\u3002\u5728\u4f7f\u7528thinkphp\u6846\u67b6\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u5e38\u89c4\u7684\u4ee3\u7801\u91cd\u590d\uff0c\u63d0\u9ad8\u4ee3\u7801\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<ol>\n<li>\u4ec0\u4e48\u662f\u65e0\u9650\u6781\u5206\u7c7b\uff1f<\/li>\n<\/ol>\n<p>\u65e0\u9650\u6781\u5206\u7c7b\u662f\u6307\u5206\u7c7b\u6811\u5f62\u7ed3\u6784\u7684\u65e0\u9650\u6269\u5c55\uff0c\u4e0d\u9650\u5c42\u6570\u3001\u4e0d\u9650\u6570\u91cf\u3002\u4e0e\u4f20\u7edf\u7684\u56fa\u5b9a\u5206\u7c7b\u65b9\u5f0f\u76f8\u6bd4\uff0c\u65e0\u9650\u6781\u5206\u7c7b\u5448\u73b0\u51fa\u66f4\u5f3a\u7684\u7075\u6d3b\u6027\u548c\u6570\u636e\u6574\u7406\u7684\u4fbf\u6377\u6027\u3002\u5728\u7f51\u7ad9\u4e2d\u4f7f\u7528\u65e0\u9650\u6781\u5206\u7c7b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66f4\u52a0\u65b9\u4fbf\u5730\u8fdb\u884c\u6dfb\u52a0\u3001\u4fee\u6539\u548c\u5220\u9664\u3002<\/p>\n<ol>\n<li>thinkphp\u6846\u67b6\u5982\u4f55\u5b9e\u73b0\u65e0\u9650\u6781\u5206\u7c7b\uff1f<\/li>\n<\/ol>\n<p>thinkphp\u6846\u67b6\u901a\u8fc7\u4f7f\u7528\u9012\u5f52\u7684\u65b9\u5f0f\u5b9e\u73b0\u65e0\u9650\u6781\u5206\u7c7b\u3002\u9012\u5f52\u662f\u4e00\u79cd\u6839\u636e\u5f53\u524d\u60c5\u51b5\u53cd\u590d\u8c03\u7528\u81ea\u8eab\u7684\u65b9\u6cd5\u3002\u5728\u65e0\u9650\u6781\u5206\u7c7b\u4e2d\uff0c\u6bcf\u4e2a\u5206\u7c7b\u7ed3\u70b9\u90fd\u6709\u81ea\u5df1\u7684id\u4e0e\u7236\u8282\u70b9pid\u3002\u901a\u8fc7\u5bf9pid\u7684\u4e0d\u65ad\u9012\u5f52\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u6240\u9700\u7684\u5206\u7c7b\u4fe1\u606f\u3002<\/p>\n<ol>\n<li>\u4ee3\u7801\u5b9e\u73b0<\/li>\n<\/ol>\n<p>(1) \u521b\u5efa\u6570\u636e\u5e93\u5e76\u521b\u5efa\u5206\u7c7b\u8868<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5047\u5b9a\u6709\u4e00\u4e2ablog\u7f51\u7ad9\uff0c\u9700\u8981\u5bf9\u6587\u7ae0\u6309\u7167\u6807\u7b7e\u8fdb\u884c\u5206\u7c7b\u7ba1\u7406\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5206\u7c7b\u8868\uff0c\u8868\u4e2d\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1aid\u3001name\u3001pid\u3002\u5176\u4e2d\uff0cid\u4e3a\u5206\u7c7b\u7684\u552f\u4e00\u6807\u8bc6\uff0cname\u4e3a\u5206\u7c7b\u7684\u540d\u79f0\uff0cpid\u4e3a\u5206\u7c7b\u7684\u7236\u8282\u70b9id\u3002<\/p>\n<p>(2) \u5728Controller\u4e2d\u6dfb\u52a0\u5206\u7c7b\u663e\u793a\u65b9\u6cd5<\/p>\n<p>public function category() {<\/p>\n<p>\/\/\u83b7\u53d6\u5206\u7c7b\u6570\u636e<\/p>\n<p>$categories = $this-&gt;getCategories();<\/p>\n<p>$this-&gt;assign(&#8216;categories&#8217;, $categories);<\/p>\n<p>return $this-&gt;fetch();<\/p>\n<p>}<\/p>\n<p>(3) \u5728Model\u4e2d\u6dfb\u52a0\u83b7\u53d6\u5206\u7c7b\u65b9\u6cd5<\/p>\n<p>public function getCategories($pid=0) {<\/p>\n<p>$result = $this-&gt;where(&#8216;pid&#8217;, $pid)-&gt;select();<\/p>\n<p>$categories = array();<\/p>\n<p>foreach ($result as $category) {<\/p>\n<pre>$category['sub'] = $this-&amp;gt;getCategories($category-&amp;gt;id);\n\n$categories[] = $category;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<\/p>\n<p>return $categories;<\/p>\n<p>}<\/p>\n<p>(4) \u5728View\u4e2d\u5c55\u793a\u5206\u7c7b\u6570\u636e<\/p>\n<\/p>\n<ul>\n<p>{volist name=&#8221;categories&#8221; id=&#8221;category&#8221;}<\/p>\n<\/p>\n<li>{$category.name}\n<pre>{if $category.sub}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n    {volist name=&#8221;category.sub&#8221; id=&#8221;sub&#8221;} <\/p>\n<li>{$sub.name}<\/li>\n<p> {\/volist}\n  <\/ul>\n<p> {\/if} <\/p>\n<\/li>\n<p>{\/volist}<\/p>\n<\/p>\n<\/ul>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9e\u73b0thinkphp\u6846\u67b6\u7684\u65e0\u9650\u6781\u5206\u7c7b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u5bf9pid\u7684\u9012\u5f52\u67e5\u8be2\uff0c\u5c06\u5206\u7c7b\u4fe1\u606f\u4ee5\u6811\u5f62\u7ed3\u6784\u5728\u9875\u9762\u4e0a\u8fdb\u884c\u663e\u793a\u3002<\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u5bf9\u672c\u6587\u7684\u9605\u8bfb\uff0c\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u638c\u63e1thinkphp\u6846\u67b6\u7684\u65e0\u9650\u6781\u5206\u7c7b\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u4e86\u89e3\u9012\u5f52\u5728\u5206\u7c7b\u7ba1\u7406\u4e2d\u7684\u5e94\u7528\u3002\u5982\u4eca\uff0c\u5206\u7c7b\u7ba1\u7406\u5df2\u6210\u4e3a\u540e\u53f0\u7ba1\u7406\u7cfb\u7edf\u7684\u5fc5\u5907\u529f\u80fd\u4e4b\u4e00\u3002\u56e0\u6b64\uff0c\u5b66\u4f1athinkphp\u6846\u67b6\u7684\u65e0\u9650\u6781\u5206\u7c7b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6211\u4eec\u7684\u5f00\u53d1\u6548\u7387\u5e76\u4f7f\u6211\u4eec\u7684\u7f51\u7ad9\u66f4\u52a0\u89c4\u8303\u548c\u6613\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u65e0\u9650\u6781\u5206\u7c7b\u7684\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u884c\u4e1a\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u5728\u7f51\u7ad9\u5f00\u53d1\u4e2d\u4f7f\u7528\u540e\u53f0\u7ba1\u7406\u7cfb\u7edf\u5df2\u6210\u4e3a\u5fc5\u9700\u54c1\u3002\u7136\u800c\uff0c\u7f51\u7ad9\u5728\u7ba1\u7406\u8fc7\u7a0b\u4e2d\uff0c\u968f\u7740\u6570\u636e\u7684\u4e0d\u65ad\u79ef\u7d2f\uff0c\u5206\u7c7b\u8d8a\u6765\u8d8a\u591a\uff0c\u4f7f\u5f97\u5728\u9875\u9762\u4e0a\u5c55\u793a\u65f6\uff0c\u9700\u6309\u7167\u5206\u7c7b\u8fdb\u884c\u5f52\u7eb3\u6574\u7406\u3002\u5982\u6b64\u4e00\u6765\uff0c\u4fbf\u9700\u8981\u4f7f\u7528\u5230\u5206\u7c7b\u7ba1\u7406\u7684\u65b9\u6cd5\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528thinkphp\u6846\u67b6\u7684\u65e0\u9650\u6781\u5206\u7c7b\u65b9\u6cd5\uff0c\u6765\u5b9e\u73b0\u7f51\u7ad9\u5206\u7c7b\u7684\u7ba1\u7406\u3002 \u4ec0\u4e48\u662fthinkphp\u6846\u67b6\uff1f thinkphp\u662f\u4e00\u6b3e\u4f18\u79c0\u7684\u6846\u67b6\u6280\u672f\uff0c\u7ee7\u627f\u4e86\u4f20\u7edf\u7f51\u7ad9\u5f00\u53d1\u6846\u67b6\u7684\u4f18\u70b9\uff0c\u540c\u65f6\u878d\u5408\u4e86\u5f53\u524d\u4e3b\u6d41\u7684\u6280\u672f\u65b9\u6cd5\u3002thinkphp\u6846\u67b6\u4e0d\u4ec5\u4ec5\u5177\u5907\u9ad8\u6548\u548c\u65b9\u4fbf\uff0c\u8fd8\u5177\u6709\u4f18\u79c0\u7684\u6269\u5c55\u6027\u548c\u517c\u5bb9\u6027\u3002\u5728\u4f7f\u7528thinkphp\u6846\u67b6\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u5e38\u89c4\u7684\u4ee3\u7801\u91cd\u590d\uff0c\u63d0\u9ad8\u4ee3\u7801\u8fd0\u884c\u6548\u7387\u3002 \u4ec0\u4e48\u662f\u65e0\u9650\u6781\u5206\u7c7b\uff1f \u65e0\u9650\u6781\u5206\u7c7b\u662f\u6307\u5206\u7c7b\u6811\u5f62\u7ed3\u6784\u7684\u65e0\u9650\u6269\u5c55\uff0c\u4e0d\u9650\u5c42\u6570\u3001\u4e0d\u9650\u6570\u91cf\u3002\u4e0e\u4f20\u7edf\u7684\u56fa\u5b9a\u5206\u7c7b\u65b9\u5f0f\u76f8\u6bd4\uff0c\u65e0\u9650\u6781\u5206\u7c7b\u5448\u73b0\u51fa\u66f4\u5f3a\u7684\u7075\u6d3b\u6027\u548c\u6570\u636e\u6574\u7406\u7684\u4fbf\u6377\u6027\u3002\u5728\u7f51\u7ad9\u4e2d\u4f7f\u7528\u65e0\u9650\u6781\u5206\u7c7b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66f4\u52a0\u65b9\u4fbf\u5730\u8fdb\u884c\u6dfb\u52a0\u3001\u4fee\u6539\u548c\u5220\u9664\u3002 thinkphp\u6846\u67b6\u5982\u4f55\u5b9e\u73b0\u65e0\u9650\u6781\u5206\u7c7b\uff1f thinkphp\u6846\u67b6\u901a\u8fc7\u4f7f\u7528\u9012\u5f52\u7684\u65b9\u5f0f\u5b9e\u73b0\u65e0\u9650\u6781\u5206\u7c7b\u3002\u9012\u5f52\u662f\u4e00\u79cd\u6839\u636e\u5f53\u524d\u60c5\u51b5\u53cd\u590d\u8c03\u7528\u81ea\u8eab\u7684\u65b9\u6cd5\u3002\u5728\u65e0\u9650\u6781\u5206\u7c7b\u4e2d\uff0c\u6bcf\u4e2a\u5206\u7c7b\u7ed3\u70b9\u90fd\u6709\u81ea\u5df1\u7684id\u4e0e\u7236\u8282\u70b9pid\u3002\u901a\u8fc7\u5bf9pid\u7684\u4e0d\u65ad\u9012\u5f52\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u6240\u9700\u7684\u5206\u7c7b\u4fe1\u606f\u3002 \u4ee3\u7801\u5b9e\u73b0 (1) \u521b\u5efa\u6570\u636e\u5e93\u5e76\u521b\u5efa\u5206\u7c7b\u8868 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5047\u5b9a\u6709\u4e00\u4e2ablog\u7f51\u7ad9\uff0c\u9700\u8981\u5bf9\u6587\u7ae0\u6309\u7167\u6807\u7b7e\u8fdb\u884c\u5206\u7c7b\u7ba1\u7406\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5206\u7c7b\u8868\uff0c\u8868\u4e2d\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1aid\u3001name\u3001pid\u3002\u5176\u4e2d\uff0cid\u4e3a\u5206\u7c7b\u7684\u552f\u4e00\u6807\u8bc6\uff0cname\u4e3a\u5206\u7c7b\u7684\u540d\u79f0\uff0cpid\u4e3a\u5206\u7c7b\u7684\u7236\u8282\u70b9id\u3002 (2) \u5728Controller\u4e2d\u6dfb\u52a0\u5206\u7c7b\u663e\u793a\u65b9\u6cd5 public function category() { \/\/\u83b7\u53d6\u5206\u7c7b\u6570\u636e $categories = $this-&gt;getCategories(); $this-&gt;assign(&#8216;categories&#8217;, $categories); return $this-&gt;fetch(); } (3) \u5728Model\u4e2d\u6dfb\u52a0\u83b7\u53d6\u5206\u7c7b\u65b9\u6cd5 public function getCategories($pid=0) { $result = $this-&gt;where(&#8216;pid&#8217;, $pid)-&gt;select(); $categories = array(); foreach ($result as $category) { $category[&#8216;sub&#8217;] = $this-&amp;gt;getCategories($category-&amp;gt;id); $categories[] = $category; \u767b\u5f55\u540e\u590d\u5236 } return $categories; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22952","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22952"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22952\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}