我正在为网站制作目录,并且正在构建类别树。这是我的第一个主要 php 项目。使用 codeigniter 框架,我正在尝试使用本教程http://www.phpbuilder.com/articles/databases/mysql/handling-hierarchical-data-in-mysql-and-php.html因为它很简单。
我把它分解成 MVC 并创建了这个模型:
<?php
ini_set('display_errors', 1); 错误报告(~0);室内装潢类扩展 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
我相信我的问题在视图中。我确定没有正确分解,但是这段代码会去哪里?进入控制器?
谢谢。