Home > Article > Backend Development > PHP uses recursive functions to achieve infinite classification, PHP recursion_PHP tutorial
I believe that many friends who learn php will try to build an online mall as a way to improve their skills. You should be comfortable with various operations on product classification, product names, etc., then you can try making unlimited classification lists.
What is infinite classification?
Infinite-level classification is a classification technique. For example, department organization, article classification, subject classification, etc. often use infinite-level classification. It can be simply understood as classification. In fact, if we think about it carefully, there are simply too many classifications in life. Clothes can be divided into men's clothing and women's clothing, tops and pants, and they can also be classified according to age groups. Classification is everywhere, and classification appears "infinite". I won’t talk about the necessity of infinite classification here.
Introduction to the infinite classification principle
Infinite classification may seem "high-end", but in fact the principle is very simple. Infinite classification not only requires the ingenuity of the code, but also relies on the rationality of the database design. To satisfy infinite classification, the database needs to have two necessary fields, id and pid. The id is used to identify itself, while the pid is used to indicate the parent id. In other words, each classification record not only describes itself, but also describes another ID that it is most closely concerned about. What seemed like a complicated matter was solved with such a little trick.
Without further ado, it’s time to show the examples of this article.
As an avid pirate fan, I will use the character organization of "One Piece" as an example in this article.
Database preparation:
Build table onepiece:
<span>create</span> <span>table</span><span> onepiece( id </span><span>int</span><span> auto_increment, pid </span><span>int</span> <span>not</span> <span>null</span><span>, name </span><span>varchar</span>(<span>225</span>) <span>not</span> <span>null</span><span>, </span><span>primary</span> <span>key</span><span>(id) );</span>
Insert test data:
<span>insert</span> onepiece <span>values</span><span> (</span><span>1</span>,<span>0</span>,<span>'</span><span>海军</span><span>'</span><span>), (</span><span>2</span>,<span>0</span>,<span>'</span><span>海贼</span><span>'</span><span>), (</span><span>3</span>,<span>0</span>,<span>'</span><span>革命军</span><span>'</span><span>), (</span><span>4</span>,<span>1</span>,<span>'</span><span>青雉</span><span>'</span><span>), (</span><span>5</span>,<span>1</span>,<span>'</span><span>赤犬</span><span>'</span><span>), (</span><span>6</span>,<span>1</span>,<span>'</span><span>黄猿</span><span>'</span><span>), (</span><span>7</span>,<span>2</span>,<span>'</span><span>四皇</span><span>'</span><span>), (</span><span>8</span>,<span>2</span>,<span>'</span><span>七武海</span><span>'</span><span>), (</span><span>9</span>,<span>2</span>,<span>'</span><span>草帽海贼团</span><span>'</span><span>), (</span><span>10</span>,<span>9</span>,<span>'</span><span>索隆</span><span>'</span><span>), (</span><span>11</span>,<span>7</span>,<span>'</span><span>香克斯</span><span>'</span><span>), (</span><span>12</span>,<span>8</span>,<span>'</span><span>多弗朗明哥</span><span>'</span><span>), (</span><span>13</span>,<span>8</span>,<span>'</span><span>克洛克达尔</span><span>'</span>);
Here is the setting in One Piece based on popular science: the world is divided into three major camps: Navy, Pirates, and Revolutionary Army. The navy has generals: Aokiji, Akainu, and Kizaru. The pirates include: the Four Emperors, the Shichibukai, and the Straw Hat Pirates. The Four Emperors have Shanks, the Shichibukai have Doflamingo and Crocodile, and the Straw Hat Pirates have Zoro. (Advertisement: One Piece is really good).
Ultimate goal:
What we are making today are two forms of infinite classification forms, one is a drop-down list type, and the other is a navigation link type. The renderings are uploaded directly:
Drop-down list styleNavigation Link style
Example code:
I encapsulated an Unlimited class to call diaplayList() to display the drop-down list form, and call diaplayLink to display the navigation Link category. You can also add (addNodes()) and delete (deleteNodes) categories.
<?<span>php </span><span>class</span><span> Unlimited{ </span><span>protected</span> <span>$mysqli</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$config</span><span>){ </span><span>$this</span>->mysqli=<span>new</span> mysqli(<span>$config</span>['host'],<span>$config</span>['user'],<span>$config</span>['pwd'<span>]); </span><span>$this</span>->mysqli->select_db(<span>$config</span>['db'<span>]); </span><span>$this</span>->mysqli->set_charset('utf8'<span>); </span><span>if</span> (<span>$this</span>->mysqli-><span>connect_errno) { </span><span>echo</span> <span>$this</span>->mysqli-><span>connect_error; } } </span><span>private</span> <span>function</span> getList(<span>$pid</span>=0,&<span>$result</span>=<span>array</span>(),<span>$spac</span>=0<span>){ </span><span>$spac</span>=<span>$spac</span>+2<span>; </span><span>$sql</span>="select * from onepiece where pid={<span>$pid</span>}"<span>; </span><span>$rs</span>=<span>$this</span>->mysqli->query(<span>$sql</span><span>); </span><span>while</span>(<span>$row</span>=<span>$rs</span>-><span>fetch_assoc()) { </span><span>$row</span>['name']=<span>str_repeat</span>('  ',<span>$spac</span>).<span>$row</span>['name'<span>]; </span><span>$result</span>[]=<span>$row</span><span>; </span><span>$this</span>->getList(<span>$row</span>['id'],<span>$result</span>,<span>$spac</span><span>); } </span><span>return</span> <span>$result</span><span>; } </span><span>/*</span><span>* * 展现下拉列表式分类 * @return [type] </span><span>*/</span> <span>public</span> <span>function</span><span> displayList(){ </span><span>$rs</span>=<span>$this</span>-><span>getList(); </span><span>$str</span>="<select name='cate'>"<span>; </span><span>foreach</span> (<span>$rs</span> <span>as</span> <span>$key</span> => <span>$val</span><span>) { </span><span>$str</span>.="<option >{<span>$val</span>['name']}</option>"<span>; } </span><span>$str</span>.="</select>"<span>; </span><span>return</span> <span>$str</span><span>; } </span><span>private</span> <span>function</span> getLink(<span>$cid</span>,&<span>$result</span>=<span>array</span><span>()){ </span><span>$sql</span>="select * from onepiece where id={<span>$cid</span>}"<span>; </span><span>$rs</span>=<span>$this</span>->mysqli->query(<span>$sql</span><span>); </span><span>if</span>(<span>$row</span>=<span>$rs</span>-><span>fetch_assoc()){ </span><span>$result</span>[]=<span>$row</span><span>; </span><span>$this</span>->getLink(<span>$row</span>['pid'],<span>$result</span><span>); } </span><span>return</span> <span>array_reverse</span>(<span>$result</span><span>); } </span><span>/*</span><span>* * 展现导航Link * @param [type] $cid [description] * @return [type] [description] </span><span>*/</span> <span>public</span> <span>function</span> displayLink(<span>$cid</span><span>){ </span><span>$rs</span>=<span>$this</span>->getLink(<span>$cid</span><span>); </span><span>$str</span>=''<span>; </span><span>foreach</span> (<span>$rs</span> <span>as</span> <span>$val</span><span>) { </span><span>$str</span>.="<a href=''>{<span>$val</span>['name']}</a>>"<span>; } </span><span>return</span> <span>$str</span><span>; } </span><span>/*</span><span>* * 增加分类 * @param [type] $pid 父类id * @param [type] $name 本类名 </span><span>*/</span> <span>public</span> <span>function</span> addNodes(<span>$pid</span>,<span>$name</span><span>){ </span><span>$sql</span>="insert into onepiece values('',{<span>$pid</span>},'".<span>$name</span>."')"<span>; </span><span>if</span>(<span>$this</span>->mysqli->query(<span>$sql</span><span>)){ </span><span>return</span> <span>true</span><span>; } } </span><span>/*</span><span>* * 删除分类 * @param [type] $id 本类id * @return [type] </span><span>*/</span> <span>public</span> <span>function</span> deleteNodes(<span>$id</span><span>){ </span><span>$sql</span>="select * from onepiece where pid ={<span>$id</span>}"<span>; </span><span>$rs</span>=<span>$this</span>->mysqli->query(<span>$sql</span><span>); </span><span>if</span>(<span>$row</span>=<span>$rs</span>-><span>fetch_assoc()){ </span><span>$mes</span>="还有子元素,请勿删除"<span>; }</span><span>else</span><span>{ </span><span>$sql</span>="delete from onepiece where id={<span>$id</span>}"<span>; </span><span>if</span>(<span>$this</span>->mysqli->query(<span>$sql</span><span>)){ </span><span>$mes</span>="删除成功"<span>; } } </span><span>return</span> <span>$mes</span><span>; } }</span>
The functions in the class mainly adopt the method of recursive functions. If you understand the recursive functions deeply, the rest will fall into place. The following three methods of PHP recursive functions introduce the principles of PHP recursive functions in detail.