Home  >  Article  >  Daily Programming  >  Implementation method of drop-down list in PHP infinite classification (2) (picture, text + video)

Implementation method of drop-down list in PHP infinite classification (2) (picture, text + video)

藏色散人
藏色散人Original
2018-10-08 09:35:325761browse

This article’s implementation method of PHP infinite classification drop-down list menu will be combined with the previous article [PHP infinite classification (1) data table design method] The example will be explained in detail for everyone.

PHP Unlimited Classification is actually a technique of classification. Any technique will be very simple as long as we master its implementation principle, and the principle of PHP infinite classification is the principle of recursion.

Relevant knowledge points about recursion are in [How to implement recursive sorting in PHP? 】This article has already been introduced, friends who need to know more can refer to it.

As mentioned in the previous article, PHP unlimited classification must be implemented in combination with a database, and this database must have two fields, id and pid. (pid represents the upper level id)

Below we will introduce the implementation method of PHP unlimited classification through a simple data table mode.

A simple classification data table information is as follows:

Implementation method of drop-down list in PHP infinite classification (2) (picture, text + video)

PHP infinite classification implementation drop-down list The code example is as follows:

<?php
$host = "127.0.0.1";
$user = "root";
$password = "root";
$dbName = "php";
$link = mysqli_connect($host, $user, $password, $dbName);

function getList($pid = 0, &$result = array(), $spac = 0)
{
    global $link;
    $spac = $spac + 2;//标题前空格重复的次数
    //根据父id查找数据
    $sql = "select * from sort WHERE pid=$pid";
    //发送sql语句
    $res = mysqli_query($link, $sql);

    //判断$row里的值是否为空然后再循环
    while ($row = mysqli_fetch_assoc($res)) {
        //str_repeat 重复括号里的字符串,后面跟的是次数
        $row[&#39;title&#39;] = str_repeat(&#39; &#39;, $spac) . &#39;|--&#39; . $row[&#39;title&#39;];
        //把数组赋给 $result
        $result[] = $row;
        //递归调用,自己调用自己 这个括号里的参数和上面getList($pid=0...)是一样的$row[&#39;id&#39;]==$pid=0;
        getList($row[&#39;id&#39;], $result, $spac);
    }
    return $result;//把结果返回出去
}

$rs = getList();//使用方法
echo "<pre class="brush:php;toolbar:false">";
//print_r($rs);//打印方法结果

echo "<select>";
foreach ($rs as $k => $v) {
    echo "<option value =>{$v[&#39;title&#39;]}</option>";
}
echo "</select>";

The final effect of this code is as follows:

Implementation method of drop-down list in PHP infinite classification (2) (picture, text + video)

In the above code, we define a getList method and set the pass Enter three optional parameters, which are a default $pid, a referenced array parameter &$result, and a space character count $spac. Then the variable $link that connects to the database is set as a global variable, mainly so that it can be used normally in this method.

The main idea to achieve the effect of the above picture is to traverse the array in the example. When its parent pid is the same as the top level 0, extract the array name, re-edit it, add the prefix, put it back, and then pass The recursive algorithm then calls itself to complete one recursion. Next time, if there is a pid of the next level, it will continue to be called. In this way, the effect of the above drop-down list can be easily achieved!

This article is about the specific implementation method of PHP infinite classification drop-down list.

If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of Implementation method of drop-down list in PHP infinite classification (2) (picture, text + video). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn