I am very new to OOP and haven't been programing in php in years, but essentially what I am trying to do is to display a multilevel category tree in a CRUD application. I have a Category class and a method that will be the one to display the appropriate category based on the parent_id. As of now my other attempts display the whole thing, that as is all categories and subcategories in a select statement. this is my Category class you'll notice I tried a few things, that didn't work.
class Category{
// database connection and table name
private $conn;
private $table_name = "menu";
// object properties
public $id;
public $name;
public $parent_id;
public function __construct($db){
$this->conn = $db;
}
//Builds the category tree recursively fetching data
function build_category_tree(&$output, $preselected, $parent=0, $indent=""){
$query = "SELECT id, name FROM menu WHERE parent_id = " . $parent . "
";
while($r = $query->fetch(PDO::FETCH_ASSOC)){
$selected = ($r["id"] == $preselected) ? "selected=\"selected\"" : "";
$output .= "<option value=\"" . $r["id"] . "\" " . $selected . ">" . $indent . $r["name"] . "</option>";
if($r["id"] != $parent){
build_category_tree($output, $preselected, $r["id"], $indent . " ");
}
}
}
// used by select drop-down list
function read(){
//select all data
$query = "SELECT
id, name
FROM
" . $this->table_name . "";
/*$query = "SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4, t5.name as lev5
"
. "FROM menu AS t1
"
. "LEFT JOIN menu AS t2 ON t2.parent_id = t1.id
"
. "LEFT JOIN menu AS t3 ON t3.parent_id = t2.id
"
. "LEFT JOIN menu AS t4 ON t4.parent_id= t3.id
"
. "LEFT JOIN menu AS t5 ON t5.parent_id= t4.id
"
. "WHERE t1.parent_id = 0";*/
$stmt = $this->conn->prepare( $query );
$stmt->execute();
return $stmt;
}
function readName(){
/*$query = "SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4, t5.name as lev5
"
. "FROM menu AS t1
"
. "LEFT JOIN menu AS t2 ON t2.parent_id = t1.id
"
. "LEFT JOIN menu AS t3 ON t3.parent_id = t2.id
"
. "LEFT JOIN menu AS t4 ON t4.parent_id= t3.id
"
. "LEFT JOIN menu AS t5 ON t5.parent_id= t4.id
"
. "WHERE t1.parent_id = ? limit 0,1";*/
$query = "SELECT name FROM " . $this->table_name . " WHERE id = ? ";
$stmt = $this->conn->prepare( $query );
$stmt->bindParam(1, $this->id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$this->name = $row['name'];
}
}
The next script builds the select tag with the appropriate categories. As I have 5 levels of categories I am trying to simplify the SELECT and make it more manageable.
// read the product categories from the database
include_once 'objects/category.php';
$category = new Category($db);
$stmt = $category->read();
/* //This works fine in a single level that is no subcategories
echo "<select class='form-control' name='category_id'>";
echo "<option>Seleccioná la categoría...</option>";
while ($row_category = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row_category);
echo "<option value='{$id}'>{$name}</option>";
}*/
// put them in a select drop-down
$category->build_category_tree($locations, 0);
// echo "</select>";
?><select><?php echo $locations ?></select>
<!-- Test other options -->
<select>
<option value="-1">Seleccioná la categoría</option>
<?php echo $locations ?>