Welcome Guest, Not a member yet? Register   Sign In
Logical Problem With category and sub-category
#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.


Messages In This Thread
Logical Problem With category and sub-category - by El Forum - 04-24-2009, 06:00 AM
Logical Problem With category and sub-category - by El Forum - 04-24-2009, 10:36 AM
Logical Problem With category and sub-category - by El Forum - 04-24-2009, 11:07 AM
Logical Problem With category and sub-category - by El Forum - 04-24-2009, 12:14 PM
Logical Problem With category and sub-category - by El Forum - 04-24-2009, 12:39 PM
Logical Problem With category and sub-category - by El Forum - 04-24-2009, 04:44 PM
Logical Problem With category and sub-category - by El Forum - 04-25-2009, 02:32 AM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 01:34 PM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 01:49 PM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 01:56 PM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 02:02 PM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 02:08 PM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 02:16 PM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 02:18 PM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 02:21 PM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 02:24 PM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 02:26 PM
Logical Problem With category and sub-category - by El Forum - 09-10-2009, 06:06 PM
Logical Problem With category and sub-category - by El Forum - 09-11-2009, 07:31 AM
Logical Problem With category and sub-category - by El Forum - 09-11-2009, 07:48 AM
Logical Problem With category and sub-category - by El Forum - 09-11-2009, 07:51 AM
Logical Problem With category and sub-category - by El Forum - 09-11-2009, 07:56 AM
Logical Problem With category and sub-category - by El Forum - 09-11-2009, 08:12 AM
Logical Problem With category and sub-category - by El Forum - 09-11-2009, 08:18 AM
Logical Problem With category and sub-category - by El Forum - 09-11-2009, 08:36 AM
Logical Problem With category and sub-category - by El Forum - 09-11-2009, 09:07 AM
Logical Problem With category and sub-category - by El Forum - 09-11-2009, 09:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB