CodeIgniter Forums
subcategory solution - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: subcategory solution (/thread-29670.html)

Pages: 1 2


subcategory solution - El Forum - 04-17-2010

[eluser]fRkSsK[/eluser]
i searched, but i have no solution about subcategories. Simply;

-Product
- -Shoes
- - -Red Shoes
- - -Blue Shoes
- - - -Summer Shoes
- - - - -Product1
- - - - -Product2
- - - - -...
- - - -Winter Shoes
- - -Black Shoes
- -Shirts
- -Clocks

my uri must be like this:
../product/shoes/blue-shoes/summer-shoes/product1

and it must use only one controller named "product". i dont use shoes,shirts and clocks controllers because of these will come in database.

so, there is not a problem with databases. my problem is controller and uri.

how can i do that? how must i write my product controller?


subcategory solution - El Forum - 04-17-2010

[eluser]Twisted1919[/eluser]
I am not sure if this is what you want but :
Code:
class Products extends Controller(){


function __construct(){
    parent::Controller();
}

//Send every request to index
function _remap($method){
    $this->index();
}

function index(){
   $main_category = $this->uri->segment(2);
   $uriA  = $this->uri->segment(3);
   $uriB  = $this->uri->segment(4) ;
   $uriC = $this->uri->segment(5);
   //Next you would check to see if it requires products from a subcategory like
   //So let's assume this uri : /products/shoes/blue-shoes/summer-shoes/sandals.html
   if( ! empty($main_category) && ! empty($uriA) && ! empty($uriB) && ! empty($uriC) ){
   //If this is true then that means it requires a product from the category shoes , that is in subcat blue shoes that is in subcat summer shoes .
   $this->model->get_product($uriC);
   }elseif( ! empty($main_category) && ! empty($uriA) && ! empty($uriB) && empty($uriC) ){
   //This mean that a product from shoes that is in blue-shoes subcategory .
   $this->model->get_product($uriB);
   }elseif( ! empty($main_category) && !empty($uriA) && empty($uriB) && empty($uriC) ){
   //This means that requires a product that is directly in the shoes category .
   $this->model->get_product($uriA);  
   }
   else{
   show_error('The product was not found blah blah ...')
   }
}

}



Is this close to what you want ?


subcategory solution - El Forum - 04-17-2010

[eluser]cahva[/eluser]
I've done ecommerce site myself with SEO friendly urls. I suggest you create 2 controllers: product and category. Use _remap() method in both of them(so everything after product or category points to that method). After that you can inspect what has passed to the controller and fetch product/category according to that.

Couple of pointers:
Save the full seo path(call it seo, slug whatever) to databse. For example for the product1, save "shoes/blue-shoes/summer-shoes/product1" as the seo url. With this saved, you can do search from the database easily. I did the mistake NOT to save the path in the first place and had to "travel" the segments one by one, which ofcourse caused overhead.

In my shop I have only one controller named shop. But being wiser now, it would have been better to have category and product controller as you can do seo search more easily according to which controller you are at(search for product seo if you are on product controller and search for category seo if you are on category controller).


subcategory solution - El Forum - 04-17-2010

[eluser]Twisted1919[/eluser]
[quote author="cahva" date="1271544529"]
Save the full seo path(call it seo, slug whatever) to databse. For example for the product1, save "shoes/blue-shoes/summer-shoes/product1" as the seo url. With this saved, you can do search from the database easily. I did the mistake NOT to save the path in the first place and had to "travel" the segments one by one, which ofcourse caused overhead.
[/quote]

Nice one , makes sense to save it to database .

L.E: But if you have 1000 products and you change a category name , doesn't this mean that you need to update all the url's in database ?


subcategory solution - El Forum - 04-17-2010

[eluser]Tominator[/eluser]
Yes, it means Smile I think it will be "fail" Big Grin

I am using this model (DB):

| category |
-----------------------------
| ID | name | parent | etc (like date, who added ...)
| 1 | First | 0 | ...
| 2 | Second | 0 | ...
| 3 | Third | 2 | ...
| 4 | Fourth | 3 | ...
| 5 | NewOne | 4 | ...
| 6 | LastOne | 4 | ...
-------------------------------

##############################################

| product |
----------------------------
| ID | category | name | etc. (value, count, seo-name ...)
| 1 | 1 | First | ...
| 2 | 2|3|4|5 | Second | ...
| 2 | 2|3|4|6 | Third | ...
| 2 | 2|3|4|6 | LastOne| ...

##########################################

Your menu will be then:

- First (+ First product)
- Second
- Third
- Fourth
- NewOne (+ Second product)
- LastOne (+ Third and LastOne product)


Why this model?

When you want to show all products in some category you use:

Second category:
SELECT * FROM product WHERE category LIKE '2%'

Third category:
SELECT * FROM product WHERE category LIKE '2|3%'

etc.


subcategory solution - El Forum - 04-17-2010

[eluser]Twisted1919[/eluser]
The correct database schema for categories would be a single table referencing itself . So you will have category_id and parent_id , and you can go how deep you want with this . But that's not the point of the thread , i think the O.P was just in doubt how the controller should look like , and a good starting point is my first post .
L.E: @Tominator , maybe you should take a look at database normalization, your structure is not as good as it must be in order to maintain a database in easy steps .


subcategory solution - El Forum - 04-17-2010

[eluser]Tominator[/eluser]
Yes it is, but having a great DB allow you to write your code smarter! (and my post use the same category system as you write)


subcategory solution - El Forum - 04-17-2010

[eluser]fRkSsK[/eluser]
@Twisted1919; if i use your model, i can not do unlimited category.

@cahva; your model is unchangeble category name, or changing is so hard. Smile

@Tominator; you did not explain your controller model.

so, there is no solution for this problem. please think about that.


subcategory solution - El Forum - 04-17-2010

[eluser]Tominator[/eluser]
I was just writting how to create good DB.

Isn't work this in routers.php?
Code:
$route['product/:any'] = 'product/?cat=$1';

Then you have to check, if data are secure, and you get your categories and subcategories in $_GET["cat"] (you will use explode() )


subcategory solution - El Forum - 04-17-2010

[eluser]skunkbad[/eluser]
Creating a good product controller and menu that updates dynamically when products are added is a bit of a challenge. The best I could do for Community Cart was to have the category name as the 3rd URI segment.

/product/category/examples

This means that no two categories or sub-categories could be named the same. I'd be interested to see what somebody else can come up with.