selectpicker中的PHP MySQL无限类别树

问题描述 投票:0回答:3

我试图用“无限深度”制作可选择的选项。我创建了表但是我无法将它们提取到selectpicker中。我该怎么办?

我的案例中的行业是类别

这就是我所做的:

CREATE TABLE sectors(
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(20) NOT NULL,
        parent_id INT DEFAULT NULL
);

CREATE TABLE users(
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(20) NOT NULL,
        is_agreed TINYINT NOT NULL
);

CREATE TABLE selected_sectors(
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        selected_sector_id INT NOT NULL

);

这使得选择但子类别丢失如下:

public function getAllSectors(){
    $statement = $this->connect()->query("SELECT * FROM sectors");
    while ($row = $statement->fetch()){
        $section_id = $row['id'];
        $name = $row['name'];
        $parent_id = $row['parent_id'];


        if ($parent_id == NULL){
            echo '<option value="'.$section_id.'">   '.$name.'</option>';
        }elseif($parent_id == $section_id){
            echo '<option value="'.$section_id.'">'.$name.'</option>';
        }
    }
}

我该怎么办?任何回应表示赞赏。

php html mysql
3个回答
0
投票

出于多种原因,您应该在数据库中具有物化路径

CREATE TABLE sectors(
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(20) NOT NULL,
        tree VARCHAR(255) DEFAULT '',
        parent_id INT DEFAULT NULL
);

其中tree应该有像'{sector_id_depth_0}/{sector_id_depth_1}/{sector_id_depth_2}/{sector_id_depth_3}/{sector_id}'这样的值

你不能做递归查询来生成你的select(使用ORDER BY tree)以及你应该用这棵树做的许多其他事情。您可以在不查询数据库的情况下了解其深度($depth = count(explode('/', $treeColumnValue));)和祖先。

并在ui中生成您的深度选项:

<option value="<?= $id ?>"><?php echo str_repeat('-', $depth).' '.$name; ?>

0
投票

您有n级别的类别,因此您需要运行recursive function

首先,您应该从数据库运行所有数据并存储在数组中。我认为每次从数据库调用数据都不好。

<?php

// Categories Tree

function get_category_list($data, $pk = 'parent', $key = 'id', $sub_c = 'sub_categories')
    {
    $cats_tree = array();
    foreach($data as $d)
        {
        isset($cats_tree[$d[$pk]]) ? : $cats_tree[$d[$pk]] = array();
        isset($cats_tree[$d[$key]]) ? : $cats_tree[$d[$key]] = array();
        $cats_tree[$d[$pk]][$d[$key]] = array_merge($d, array(
            $sub_c => & $cats_tree[$d[$key]]
        ));
        }

    return $cats_tree[0];
    }

// make categories in option

function makeoptions($Array)
    {
    foreach($Array as $k => $v)
        {
        $Output.= "<option>";
        $Output.= $v["title"];
        if (is_array($v["children"]))
            {
            $Output.= makeNestedList($v["children"]);
            }

        $Output.= '</option>';
        }

    return $Output;
    }

// call functions

$query_categories = $mysqli->pepare("SELECT `id`,... FROM table");

// get data in array format  from database

$reStruct = array();

foreach($query_categories as $k => $v)
    {
    $reStruct[$v["id"]] = array(
        "id" => $v["id"],
        "name" => $v["name"],
        "parent" => $v["parent"],
    );
    }

$a = get_category_list($reStruct);
$x = makeoptions($a);
$html = "<select>";
$html.= $x;
$html.= "</select>";
echo $html;

0
投票

这就是我做的:

function getAllSectors(&$output = null, $parent_id = 0, $indent = null){

    $statement = $this->connect()->prepare("SELECT id, name FROM sectors WHERE parent_id =:parent_id ORDER BY parent_id, sort_order");

    $statement->execute(array(
        'parent_id'     => $parent_id
    ));

    // show the sectors one by one
    while($row = $statement->fetch()){
        $output .= '<option value=' . $row['id'] . '>' . $indent . $row['name'] . "</option>";
        if($row['id'] != $parent_id){
            // In case the current sectors's id is different than $parent_id
            // I call the function again with new parameters(so it is recursive)
            $this->getAllSectors($output, $row['id'], $indent . "&nbsp;&nbsp;");
        }
    }
    // return the list of sectors(call out is in index.php)
    return $output;
}
© www.soinside.com 2019 - 2024. All rights reserved.