I am working on a catalog for a site and I working on build a category tree. This is my first major php project. Using the codeigniter framework, I am trying to use this tutorial http://www.phpbuilder.com/articles/databases/mysql/handling-hierarchical-data-in-mysql-and-php.html because it is simple.
I broke it up into MVC and create this model:
<?php
ini_set('display_errors', 1); error_reporting(~0); class Upholstery_get extends 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;
}
}
this controller:
<?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');
}
}
and this view:
<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>
my database:
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);
This is the error i've knocked it down to. Since I'm a beginner if anyone can help me sort out the errors it would be greatly appreciated.
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
I believe my problem is in the view. I am certain that is not broken up correctly, however where would this code go? Into the controller?
thanks.