Welcome Guest, Not a member yet? Register   Sign In
Logical Problem With category and sub-category
#11

[eluser]brandingdavid[/eluser]
I put that code in my view file.

And my controller, as a consequence, doesn't have much to it.

Code:
<?php

class Frontpage extends Controller {
    
    function Frontpage()
    {
        parent::Controller();    
        $this->load->helper('url');
        $this->load->helper('form');
    
    }
    
    function index()
    {
        $this->load->database();
        $this->load->view('frontpage_view');
    }
    
}
?>
#12

[eluser]brandingdavid[/eluser]
My database structure:

id title parent menu_index
1 Information Technology 0 4
2 Human Resources 0 1
3 Planning 0 3
4 Web Specialist 1 2
5 Server Room 1 1
6 Travel 4 1
#13

[eluser]jedd[/eluser]
Okay. Things will break (or at least, work in non-predictable, non-sane ways) if you do things like this.

Database calls should be done in your model(s).

Your view(s) should present the data, as in, create HTML output using whatever raw data is given to them.

Your controller should make calls to the model, and then make a call to the view.

(In its simplest terms - this is what you're aiming at - it can get more complex later.)

I strongly encourage you to go and read up on the first several chapters of the user guide, especially the bits about models, views, controllers.
#14

[eluser]brandingdavid[/eluser]
I've tried to separate this before, but I don't understand passing things between the different types. Especially when doing something like what I am trying to accomplish here (taking the current id and checking/listing possible child categories).
#15

[eluser]brandingdavid[/eluser]
In the end though, it would be nice to know why I can't use $this->(anything) inside of a function...
#16

[eluser]jedd[/eluser]
To answer your last question - it's because $this isn't what you think it is when you're in a view.

Provide a bit more information about your database schema, and I (or someone else) might rattle some hints for you on how to do this properly.
#17

[eluser]brandingdavid[/eluser]
The database schema is pretty much posted above.

I have one table with all the categories, and have the id of the parent category if needed. The menu index is just for ordering items when displayed.

I need to, without knowing how deep the sub-categories will go, list parent and child categories in an unordered list.
#18

[eluser]jedd[/eluser]
[quote author="brandingdavid" date="1252632417"]
The database schema is pretty much posted above.
[/quote]

Succinct design.

Quote:The menu index is just for ordering items when displayed.

I'm confused by menu_index. For a given parent #, is menu_index guaranteed unique?

What's wrong with alphabetical sorting? Users like that - it makes things predictable, and hence easy to find.

Quote:I need to, without knowing how deep the sub-categories will go, list parent and child categories in an unordered list.

Okay, so if parent = 0, you're at the top. You want to be be able to provide all this data in one fell swoop, or are you given a specific ID? If the latter, do you want to find parent(s), or just children in that case?

Your controller would look almost as simple as it does now. Note that you should drop the closing ?> tag from your files - they're pointless, and occasionally confusing.
Code:
<?php
class Frontpage extends Controller {
    
    function Frontpage()
    {
        parent::Controller();    
        $this->load->helper('url');
        $this->load->helper('form');
    }
    
    function index()
    {
        $this->load->database();    // You should probably autoload this.
        $this->load->model('Category');

        $data['categories'] = $this->Category->get_all();

        $this->load->view('frontpage_view', $data);
    }  
}



Your model - here called Category for want of a better appellation - has a method (get_all()) that will return the data you're after - basically the database gumpf you are currently trying to do in your view.

Code:
<?php
class  Category  extends  Model {
    // Constructor
    function  __construct ()  {
        parent::Model();
        }

    function get_all ($parent=0)  {
        $this->db->where('parent', $parent);
        $query = $this->db->get('bin_location');
        $parents = $query->result_array();
        $results = array ();
        $x = 0;
        foreach ($parents as $parent)
            $results[$x]['parent'] = $parent;
            $this->db->where('parent', $parent->id);
            $query = $this->db->get('bin_location');
            $results[$x]['children'] = $query->result_array();
            $x++;
        }
    }

Note that this is quite ugly, and not the way I'd do it. Firstly, I'm not overly comfy with objects, so I use result_array() instead, and I've certainly no idea how you'd do multi-dimension equivalence in objects. I'd also not use the AR database method - I'm a bit old school - so I'd do a sub-select and have a single joined query there to pull out the parents at a given level, and all their children. Showing this data in the view then actually becomes much easier (much like [url="http://ellislab.com/forums/viewthread/125879/#622302"]this view partial[/url]).

In any case, tidying this up and converting back to object-friendly code is left as an exercise for the reader. From memory you can do sub-selects with AR, but, as I say, something I've always avoided.

Anyhoo, your view then becomes somewhat more straightforward - just dumping information out:
Code:
<?php
    foreach($categories as $category) {
            echo $category['parent']['title'] .'<br />';
            
            foreach($category['child'] as $child) {
                echo '- '. $child['title']. '<br />';
            }
        }

Final notes - anything involving a self-referential table needs some love and care, lest you get yourself into a circular reference. If you're only ever pulling out one layer and its spawn, this is unlikely to bite you, but it can still lead to 'interesting' results. If you try to recursively dig a bit deeper you can get into real trouble if your data is not clean.
#19

[eluser]brandingdavid[/eluser]
Seems like the category model has issues. In function get_all, you have an array created called $parents, from the query that gets all top level categories.

The result is:
Array ( [0] => Array ( [id] => 1 [title] => Information Technology [parent] => 0 [menu_index] => 4 [depth] => 0 ) [1] => Array ( [id] => 2 [title] => Human Resources [parent] => 0 [menu_index] => 1 [depth] => 0 ) [2] => Array ( [id] => 3 [title] => Planning [parent] => 0 [menu_index] => 3 [depth] => 0 ) )

You then proceed to go through and try to assign it children, but for some reason, that's not working.

$results[$x]['parent'] = $parent; seems like it could be the issue. I am unable to print_r or echo out anything from there to check what the problem could be.
#20

[eluser]brandingdavid[/eluser]
Okay, so it was your $this->db->where statement that wasn't working.
Changed to = $this->db->where('parent', $results[$x]['id']);

Tried this for my model:

Code:
&lt;?php
class  Category  extends  Model {
    // Constructor
    function  __construct ()  {
        parent::Model();
        }

    function get_all ($parent=0)  {
        $this->db->where('parent', $parent);
        $query = $this->db->get('bin_location');
        $parents = $query->result_array();
        $results = array ();
        //print_r($parents);
        $x = 0;
        foreach ($parents as $parent) {
            $results[$x] = $parent;
            //echo $results[$x]['id'];
            $this->db->where('parent', $results[$x]['id']);
            $query = $this->db->get('bin_location');
            $results[$x]['children'] = $query->result_array();
            print_r ($results[$x]['children']);
            $x++;
       }
            //print_r($results[$x]);
        }
       // print_r($results);
    }

Got me part of the way there... already though I can see that it doesn't come back and do the third level. The children of children. Also, I can't get it to display. I get:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/frontpage_view.php

Line Number: 12

CODE USED: foreach ($categories as $category) {




Theme © iAndrew 2016 - Forum software by © MyBB