-1

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.

user1093111
  • 1,071
  • 3
  • 18
  • 43
  • 1
    All the ```mysql_*``` methods are part of the **deprecated** mysql PHP extension. 1) Don't use ```ext/mysql``` - it's deprecated. 2) Don't use ```ext/mysql``` when you're using a framework which provides a far superior database functionality. – Fergus In London Jan 01 '13 at 19:51
  • @user1093111 , what are the controllers responsible for in MVC-inspired patterns? – tereško Jan 01 '13 at 19:53
  • What do you mean? since I'm new to php and mvc, i guess right now, i just have my pages controller with no routing yet. The site has only 5 pages, and once i got the tree to work i was to continue. I'm going to put up and edit shortly – user1093111 Jan 01 '13 at 19:58
  • k i just edited it with code that i think is on the right track – user1093111 Jan 01 '13 at 20:04
  • If you follow hakre's advice, you can debug the problem even though you're getting a white screen. Just so you know, people are unlikely to download and run your code - not only is it time-consuming, but it may also contain unsafe code. Remember not to trust code you find on the internet! – halfer Jan 01 '13 at 20:14

2 Answers2

1

This is more a lengthy comment than an answer:


When i go to my site the page turns completely white, and no source code. Therefore it is much harder for me to trouble shoot. If anyone can help me sort out the errors it would be greatly appreciated.

That is the so called White Screen of Death (WSOD), you find how to deal with this outlined in the PHP error reference here on the website:

Community
  • 1
  • 1
hakre
  • 178,314
  • 47
  • 389
  • 754
0

Try this:

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>';

        }

    }
Harshad Hirapara
  • 462
  • 3
  • 11