dongyi1777 2015-04-28 16:08
浏览 39

尝试使用pdo获取多级类别树并获取PHP致命错误:调用未定义的函数build_category_tree()in

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 . "&nbsp;&nbsp;");
}
}

}
// 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 ?>

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 有没有帮写代码做实验仿真的
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥30 vmware exsi重置后登不上
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
    • ¥20 yolov5自定义Prune报错,如何解决?
    • ¥15 电磁场的matlab仿真