类别在php中的树构建

时间:2013-01-01 19:47:42

标签: php codeigniter hierarchical-data

我正在为一个网站编目,我正在构建一个类别树。这是我的第一个主要的php项目。使用codeigniter框架,我正在尝试使用本教程http://www.phpbuilder.com/articles/databases/mysql/handling-hierarchical-data-in-mysql-and-php.html,因为它很简单。

我将其分解为MVC并创建此模型:

<?php 

ini_set('display_errors',1);使用error_reporting(〜0); class Upholstery_get扩展了CI_Model {

function get_path($category_id)
{
    // look up the parent of this node
    $result = mysql_query("SELECT c1.parent_id,c2.category_name AS parent_name FROM category AS c1 LEFT JOIN category AS c2 ON c1.parent_id=c2.category_id WHERE c1.category_id='$category_id' ");
    $row = mysql_fetch_array($result);

   // save the path in this array
    $path = array();

    //continue if this node is not the root node
    if ($row['parent_id']!=NULL)
    {
        // the last part of the path to node

        end($path);
        $last_key = key($path);
        $key = $last_key==0 ? 0 : $last_key+1;

        $path[$key]['category_id'] = $row['parent_id'];
        $path[$key]['category_name'] = $row['parent_name'];

        $path = array_merge(get_path($row['parent_id']), $path);
    }

   return $path;
}

function get_children($category_id, $level)
{
    $html = "";
    // retrieve all children
        //$result = mysql_query("SELECT * FROM category WHERE parent_id='$category_id'");
        //return $query->result();
        //$query = $this->db->get_where("products", array("productname" => $productname));

    $query = $this->db->get_where("category", array("parent_id" => $category_id));

            while ($row = $query)
            {
                // indent and display the title of this child
               // if you want to save the hierarchy, replace the following line with your code
                $html = $html . str_repeat('  ',$level) . $row['category_name'] . "<br/>";

               // call this function again to display this child's children
               get_children($row['category_id'], $level+1);
            }
        return $html;
}
}

这个控制器:

<?php
ini_set('display_errors', 1); error_reporting(~0);
class Pages extends CI_Controller {

public function index()
{
$this->home();
}

public function home()
{
    $this->load->view('header');
    $this->load->view('nav');
    $this->load->view('home/left_menu');
    $this->load->view('home/home_content');
//      $this->load->view('footer');
}
public function upholstery()
    {
    //load path for bread crumb
    $this->load->model("upholstery_get");
    $data["pathData"] = $this->upholstery_get->get_path("182 Davenport");
    $HTMLdata = $this->upholstery_get->get_children("182 Davenport", 3);

    //load product model
    $this->load->model("model_get");
    $data["productData"] = $this->model_get->getData("182 Davenport");

    $this->load->view('header');
    $this->load->view('nav');
    $this->load->view('upholstery/upholstery_menu');
    $this->load->view('upholstery/upholstery_content', $data);
//      $this->load->view('footer');
    }   
public function fabrics()
{
    $this->load->view('header');
    $this->load->view('nav');
    $this->load->view('fabric/fabric_menu');
    $this->load->view('fabric/fabric_content');
//      $this->load->view('footer');
}   

public function contact()
{
    $this->load->view('header');
    $this->load->view('nav');
    $this->load->view('contact/left_menu');
    $this->load->view('contact/contact_content');
//      $this->load->view('footer');
}

public function about()
{
    $this->load->view('header');
    $this->load->view('nav');
    $this->load->view('about/left_menu');
    $this->load->view('about/about_content');
//      $this->load->view('footer');
}
}

和这个观点:

<div id="menu">

<strong><font size="3"><font color="#9a141b"><font style="font-size: 20px;" color="#ac7643" face="Palatino Linotype, Book Antiqua, Palatino, serif">Upholstery<br></font><b><font face="Palatino Linotype, Book Antiqua, Palatino, serif" size="5"><br>

    // display generated HTML
    <?php echo $HTMLdata?>

</div>

我的数据库:

CREATE TABLE `category` (
`category_id` int(10) NOT NULL AUTO_INCREMENT,
`category_name` varchar(50) NOT NULL,
`parent_id` int(10) DEFAULT NULL,
PRIMARY KEY (`category_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;


INSERT INTO `category` (`category_id`, `category_name`, `parent_id`) VALUES
(1, 'Upholstery', NULL),
(2, 'Sofes', 1),
(3, '182 Davenport', 2);

这是我把它击倒的错误。由于我是初学者,如果有人能帮助我解决错误,那将非常感激。

 Fatal error: Cannot use object of type CI_DB_mysql_result as array in /Users/Home/Sites/application/models/upholstery_get.php on line 46

我相信我的问题在视野中。我确信没有正确分解,但是这段代码会在哪里?进入控制器?

感谢。

2 个答案:

答案 0 :(得分:1)

  

这是一个冗长的评论而非答案:

     
     

当我访问我的网站时,页面完全变白,没有源代码。因此,我更难以拍摄。如果有人可以帮我理清错误,那将非常感激。

这就是所谓的死亡白屏(WSOD),您可以在网站上找到如何处理PHP错误参考中概述的内容:

答案 1 :(得分:0)

试试这个:

public function get_catagory($parent_id=0)
    { 
        $this->db->where('parent_id', $parent_id);

        $result = $this->db->get('catagory')->result();

        foreach ($result as $row){


            $i = 0;
            if ($i == 0) echo '<ul>';
            echo '<li>' .$row->catagory;

            $this->get_catagory($row->id);
            echo '</li>';
            $i++;
            if ($i > 0) echo '</ul>';

        }

    }