I am trying to make selectable options with "infinity depth". I created the tables however I am unable to fetch them into selectpicker. How should I do it?
SECTORS in my case are Categories
This is what I've done:
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
);
This makes the selections but subcategories are lost like this:
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>';
}
}
}
What should I do? Any response is appreciated.