Welcome Guest, Not a member yet? Register   Sign In
Categories and Sub Categories
#1

[eluser]CI pedro[/eluser]
Hello all,

I have looked at CodeIgniter before and I finally got back to it and decided to try to use it for a site I am building.

I am new to using CodeIgniter and not all that experienced with PHP (read as haven't done any production code in PHP).

So, the site I am building is a local classifieds site and the problem I am having is figuring out how to go about listing out Categories with Sub Categories listed under the Categories.

I did see something earlier that said it would be useful in such a situation but cannot for the life of me find it again. Besides even if I did find it again I wouldn't know how to implement it in CI.

My Table is as follows:

Category
------------------
id
parentid
category
active

obviously self referential

I have a Category Model that has methods to get the categories and a method to get the subcategories by categoryid

could someone please point me in the right direction?

Any help will be greatly appreciated.

If you need more info please let me know

Thank you
#2

[eluser]Developer13[/eluser]
Will your subcategories also contain subcategories?

If so, you'll need to look into recursive functions. If you're newish to PHP or any other language, it might be a bit frustrating to wrap your head around.

But it sounds like you're on the right track with what you're doing. I am having trouble understanding exactly what you need help with - it looks like you have the logic of what needs to happen down.

Your model should contain two functions:

mdl_categories:
Code:
function getCategories() {
    $this->db->where("parentid = 0 AND active = 1");
    $this->db->orderby("category");
    $query = $this->db->get("categories");
    return ($query->result());
}

function getSubCategories($parentid) {
    $this->db->where("parentid = {$parentid} AND active = 1");
    $this->db->orderby("category");
    $query = $this->db->get("categories");
    return ($query->result_array());
}

Then in your controller:

Code:
function whatever() {
    $dbCategories = $this->mdl_categories->getCategories();
    foreach ($dbCategories as $dbCategory) {
        $categories[$dbCategory->category]['name'] = $dbCategory->category;
        $categories[$dbCategory->category]['subcategories'] = $this->mdl_categories->getSubCategories($dbCategory->id);

    }
}

This particular example doesn't include recursion, but it will put all your categories and subcategories into an array that you can then process in your view.
#3

[eluser]esra[/eluser]
I think there are a couple of threads that refer to the Adjacency List Model on the forums. I'm not sure if any code was actually posted. Adjacency List Model and the Nested Sets Model are two popular methods (there are others) for making a relational database support hierarchies (sections, categories, subcategories, etc.). There is a Nested Sets Model solution with sample code for categories/subcategories posted on the wiki by Thunder UK.

For some ideas on how to code it, you might take a look at Joomla 1.5 which is MVC and it does use both hierarchial models. I believe that their ACL and menu systems use nested sets and their content hierarchies use the adjacency list model.
#4

[eluser]CI pedro[/eluser]
The Nested sets is what I was looking at earlier that I couldn't find. I am going to take a look at again in the morning. Thanks for the responses guys. I will let you know how it all turns out.
#5

[eluser]CI pedro[/eluser]
I have figured out the nested sets for categories and sub categories and now I am wondering what the best way to go about setting up ads in the database would be.

What I am thinking is to set up the ads table so that each ad stores a left and right values for the sub category it is listed under.

The reason I thought that is that if it stores left and right values then clicking on a category will allow a faster processing time. Rather than having to preform multiple queries ie( select sub category ids where leftval > category.leftval and rightval < category.rightval then build another query to select ads in that range) I would be able to directly query the ads table one time based on the url parameters.

I hope it is clear to you what I am asking.

If not please let me know so I can try to clarify.

What are your thoughts on this.
#6

[eluser]CI pedro[/eluser]
I think I have figured this out now. It works as I have done it and it seems to perform very well.

What I have done is join the tables ad and category on the category id and selected values between the left and right values.

If there is a better way to do this please let me know.

Thank you everyone for your input.
#7

[eluser]mistress_shiira[/eluser]
[qoute]

$categories[$dbCategory->category]['name'] = $dbCategory->category;
$categories[$dbCategory->category]['subcategories'] = $this->mdl_categories->getSubCategories($dbCategory->id); //can anyone explain this line of code?

[/quote]

thanks!
#8

[eluser]xwero[/eluser]
As they loop through the categories they make a call to get the subcategories using the category id.
#9

[eluser]morcegon[/eluser]
[quote author="Developer13" date="1189923191"]Will your subcategories also contain subcategories?

If so, you'll need to look into recursive functions. If you're newish to PHP or any other language, it might be a bit frustrating to wrap your head around.

But it sounds like you're on the right track with what you're doing. I am having trouble understanding exactly what you need help with - it looks like you have the logic of what needs to happen down.

Your model should contain two functions:

mdl_categories:
Code:
function getCategories() {
    $this->db->where("parentid = 0 AND active = 1");
    $this->db->orderby("category");
    $query = $this->db->get("categories");
    return ($query->result());
}

function getSubCategories($parentid) {
    $this->db->where("parentid = {$parentid} AND active = 1");
    $this->db->orderby("category");
    $query = $this->db->get("categories");
    return ($query->result_array());
}

Then in your controller:

Code:
function whatever() {
    $dbCategories = $this->mdl_categories->getCategories();
    foreach ($dbCategories as $dbCategory) {
        $categories[$dbCategory->category]['name'] = $dbCategory->category;
        $categories[$dbCategory->category]['subcategories'] = $this->mdl_categories->getSubCategories($dbCategory->id);

    }
}

This particular example doesn't include recursion, but it will put all your categories and subcategories into an array that you can then process in your view.[/quote]

Its a very best solution for my throuble, thanks dude.!!!
#10

[eluser]obiron2[/eluser]
Can an advert appear in more than one category.

if so then you would need to keep a X-ref table with the advert id and its left and right categories


Does an advert appear in the parent category of the sub category or only in the sub category.


Can it appear directly in a category, even if there are sub-categories.

If so then you will need some way of moving ads up and down the tree if you add or delete categories



just though I'd ask....

Obiron




Theme © iAndrew 2016 - Forum software by © MyBB