💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 无限级分类 数据表 字段 id pid title ``` <pre class="calibre16">``` <span class="token6">/** * 获得分类树 * @param int $pid 父id * @param array $result返回树结构 * @param int $spac * @return array */</span> require_once <span class="token4">"pdo.php"</span><span class="token2">;</span> <span class="token5">function</span> <span class="token1">gettree</span><span class="token2">(</span>$pid<span class="token">=</span><span class="token3">0</span><span class="token2">,</span><span class="token">&</span>$result<span class="token">=</span><span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">,</span>$spac<span class="token">=</span><span class="token3">0</span><span class="token2">)</span><span class="token2">{</span> $spac <span class="token">=</span> $spac<span class="token">+</span><span class="token3">2</span><span class="token2">;</span> $row <span class="token">=</span> <span class="token1">pdo_getall</span><span class="token2">(</span><span class="token4">'item'</span><span class="token2">,</span><span class="token1">array</span><span class="token2">(</span><span class="token4">'pid'</span><span class="token">=</span><span class="token">></span>$pid<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$row as $v<span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span>$v<span class="token2">[</span><span class="token4">'pid'</span><span class="token2">]</span><span class="token">==</span><span class="token3">0</span><span class="token2">)</span><span class="token2">{</span> $v<span class="token2">[</span><span class="token4">'title'</span><span class="token2">]</span> <span class="token">=</span> $v<span class="token2">[</span><span class="token4">'title'</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span><span class="token2">{</span> $v<span class="token2">[</span><span class="token4">'title'</span><span class="token2">]</span> <span class="token">=</span> <span class="token1">str_repeat</span><span class="token2">(</span><span class="token4">'&nbsp;&nbsp;'</span><span class="token2">,</span>$spac<span class="token2">)</span><span class="token2">.</span><span class="token4">"|--"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'title'</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> $result<span class="token2">[</span><span class="token2">]</span> <span class="token">=</span> $v<span class="token2">;</span> <span class="token1">gettree</span><span class="token2">(</span>$v<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">,</span>$result<span class="token2">,</span>$spac<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> $result<span class="token2">;</span> <span class="token2">}</span> <span class="token6">/* * 父类id 选中的id * */</span> <span class="token5">function</span> <span class="token1">puttree</span><span class="token2">(</span>$pid<span class="token">=</span><span class="token3">0</span><span class="token2">,</span>$selected<span class="token">=</span><span class="token3">0</span><span class="token2">)</span><span class="token2">{</span> $rs <span class="token">=</span> <span class="token1">gettree</span><span class="token2">(</span>$pid<span class="token2">)</span><span class="token2">;</span> $str<span class="token">=</span><span class="token4">''</span><span class="token2">;</span> $str <span class="token2">.</span><span class="token">=</span> <span class="token4">"<select name='pid'>"</span><span class="token2">;</span> $str <span class="token2">.</span><span class="token">=</span> <span class="token4">'<option value="0">顶级分类</option>'</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$rs as $key<span class="token">=</span><span class="token">></span>$val<span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span>$val<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span> <span class="token">==</span> $selected<span class="token2">)</span><span class="token2">{</span> $selectedstr <span class="token">=</span> <span class="token4">"selected"</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> $selectedstr <span class="token">=</span> <span class="token4">""</span><span class="token2">;</span> <span class="token2">}</span> $str <span class="token2">.</span><span class="token">=</span> <span class="token4">"<option $selectedstr value='"</span><span class="token2">.</span>$val<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"'>"</span><span class="token2">.</span>$val<span class="token2">[</span><span class="token4">'title'</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</option>"</span><span class="token2">;</span> <span class="token2">}</span> $str <span class="token2">.</span><span class="token">=</span> <span class="token4">"</select>"</span><span class="token2">;</span> <span class="token5">return</span> $str<span class="token2">;</span> <span class="token2">}</span> <span class="token6">//面包屑路径 ,$cid为分类id ,直到返回顶级</span> <span class="token5">function</span> <span class="token1">getmenu</span><span class="token2">(</span>$cid<span class="token2">,</span><span class="token">&</span>$result<span class="token">=</span><span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> <span class="token6">//引用数据库连接资源</span> $row <span class="token">=</span> <span class="token1">pdo_get</span><span class="token2">(</span><span class="token4">'item'</span><span class="token2">,</span><span class="token1">array</span><span class="token2">(</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span>$cid<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span>$row<span class="token2">)</span><span class="token2">{</span> $result<span class="token2">[</span><span class="token2">]</span> <span class="token">=</span> $row<span class="token2">;</span> <span class="token1">getmenu</span><span class="token2">(</span>$row<span class="token2">[</span><span class="token4">'pid'</span><span class="token2">]</span><span class="token2">,</span>$result<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token6">//数组顺序倒序</span> <span class="token1">krsort</span><span class="token2">(</span>$result<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> $result<span class="token2">;</span> <span class="token2">}</span> <span class="token6">//输出从当前分类到顶级分类路径</span> <span class="token5">function</span> <span class="token1">putmenu</span><span class="token2">(</span>$cid<span class="token2">,</span>$url<span class="token">=</span><span class="token4">"demo.php?cid="</span><span class="token2">)</span><span class="token2">{</span> $res <span class="token">=</span> <span class="token1">getmenu</span><span class="token2">(</span>$cid<span class="token2">)</span><span class="token2">;</span> $str <span class="token">=</span> <span class="token4">""</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$res as $key<span class="token">=</span><span class="token">></span>$val<span class="token2">)</span><span class="token2">{</span> $str <span class="token2">.</span><span class="token">=</span> <span class="token4">"<a href='{$url}{$val['id']}'>{$val['title']}</a>>"</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> $str<span class="token2">;</span> <span class="token2">}</span> <span class="token6">//查询下级 下下级</span> <span class="token5">function</span> <span class="token1">get_cid</span><span class="token2">(</span>$cid<span class="token2">,</span><span class="token">&</span>$result<span class="token">=</span><span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> $row <span class="token">=</span> <span class="token1">pdo_getall</span><span class="token2">(</span><span class="token4">'item'</span><span class="token2">,</span><span class="token1">array</span><span class="token2">(</span><span class="token4">'pid'</span><span class="token">=</span><span class="token">></span>$cid<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span>$row<span class="token2">)</span><span class="token2">{</span> <span class="token1">foreach</span><span class="token2">(</span>$row as $v<span class="token2">)</span><span class="token2">{</span> $result<span class="token2">[</span><span class="token2">]</span> <span class="token">=</span> $v<span class="token2">;</span> <span class="token1">get_cid</span><span class="token2">(</span>$v<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">,</span>$result<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token5">return</span> $result<span class="token2">;</span> <span class="token2">}</span> <span class="token">?</span><span class="token">></span> <span class="token"><</span><span class="token">!</span>DOCTYPE html<span class="token">></span> <span class="token"><</span>html<span class="token">></span> <span class="token"><</span>head<span class="token">></span> <span class="token"><</span>meta charset<span class="token">=</span><span class="token4">"UTF-8"</span><span class="token">></span> <span class="token"><</span>title<span class="token">></span>演示无限级分类<span class="token"><</span><span class="token">/</span>title<span class="token">></span> <span class="token"><</span>style<span class="token">></span> <span class="token2">.</span>table <span class="token2">{</span> width<span class="token2">:</span> <span class="token3">100</span><span class="token">%</span><span class="token2">;</span> background<span class="token2">:</span> #f8f9fa<span class="token2">;</span> margin<span class="token2">:</span> <span class="token3">10</span>px auto<span class="token2">;</span> border<span class="token">-</span>collapse<span class="token2">:</span> collapse<span class="token2">;</span> <span class="token2">}</span> <span class="token2">.</span>table th<span class="token2">,</span> <span class="token2">.</span>table td <span class="token2">{</span> border<span class="token2">:</span> <span class="token3">1</span>px solid #f0f1f4<span class="token2">;</span> padding<span class="token2">:</span> <span class="token3">8</span>px<span class="token2">;</span> <span class="token2">}</span> <span class="token2">.</span>table th <span class="token2">{</span> background<span class="token2">:</span> #f8f9fa<span class="token2">;</span> font<span class="token">-</span>weight<span class="token2">:</span> bold<span class="token2">;</span> <span class="token2">}</span> <span class="token2">.</span>table tr <span class="token2">{</span> background<span class="token2">:</span> #FFF<span class="token2">;</span> <span class="token2">}</span> <span class="token2">.</span>table tr<span class="token2">:</span>hover <span class="token2">{</span> background<span class="token2">:</span> #f8f9fa<span class="token2">;</span> <span class="token2">}</span> <span class="token2">.</span>table td a <span class="token2">{</span> color<span class="token2">:</span> #<span class="token3">18</span>B4ED<span class="token2">;</span> text<span class="token">-</span>decoration<span class="token2">:</span> none<span class="token2">;</span> <span class="token2">}</span> <span class="token2">.</span>table td a<span class="token2">:</span>hover <span class="token2">{</span> color<span class="token2">:</span> #<span class="token3">18</span>B4ED<span class="token2">;</span> text<span class="token">-</span>decoration<span class="token2">:</span> underline<span class="token2">;</span> <span class="token2">}</span> <span class="token"><</span><span class="token">/</span>style<span class="token">></span> <span class="token"><</span><span class="token">/</span>head<span class="token">></span> <span class="token"><</span>body<span class="token">></span> <span class="token"><</span><span class="token">?</span>php <span class="token6">//默认设置顶级分类开始,1选中</span> echo <span class="token1">puttree</span><span class="token2">(</span><span class="token3">0</span><span class="token2">,</span><span class="token3">1</span><span class="token2">)</span> <span class="token2">;</span> echo <span class="token1">putmenu</span><span class="token2">(</span><span class="token3">8</span><span class="token2">)</span> <span class="token2">;</span><span class="token">?</span><span class="token">></span> <span class="token"><</span>table class<span class="token">=</span><span class="token4">"table"</span><span class="token">></span> <span class="token"><</span>tr<span class="token">></span><span class="token"><</span>th<span class="token">></span>ID<span class="token"><</span><span class="token">/</span>th<span class="token">></span><span class="token"><</span>th<span class="token">></span>上级ID<span class="token"><</span><span class="token">/</span>th<span class="token">></span><span class="token"><</span>th<span class="token">></span>分类名称<span class="token"><</span><span class="token">/</span>th<span class="token">></span><span class="token"><</span><span class="token">/</span>tr<span class="token">></span> <span class="token"><</span>tbody<span class="token">></span> <span class="token"><</span><span class="token">?</span>php <span class="token1">foreach</span><span class="token2">(</span><span class="token1">gettree</span><span class="token2">(</span><span class="token2">)</span> as $k<span class="token">=</span><span class="token">></span>$v<span class="token2">)</span><span class="token2">{</span> echo <span class="token4">"<tr ><td>"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</td><td>"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'pid'</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</td><td align='left'>"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'title'</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</td></tr>"</span><span class="token2">;</span> <span class="token2">}</span> <span class="token">?</span><span class="token">></span> <span class="token"><</span><span class="token">/</span>tbody<span class="token">></span> <span class="token"><</span><span class="token">/</span>table<span class="token">></span> <span class="token"><</span><span class="token">/</span>body<span class="token">></span> <span class="token"><</span><span class="token">/</span>html<span class="token">></span> ``` ```