1internet Posted April 4, 2013 Share Posted April 4, 2013 I am working on an e-commerce site. to There are two types of pages, products and categories. I can't work the best way to identify them in the htaccess. example urls: site.com/category/product site.com/category/sub-category/product1 site.com/category/sub-category/sub-sub-category There are essentially an unlimited level of sub-categories. So I am guessing I need to explode the url $url = (explode("/",$url); So from there I will have an array of urls. I thought the best thing to do would take the last element in the array, and check to see if it is in the database for the products. But then what if the products and categories have the same same? Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/276547-trying-to-identify-categories-from-products-in-a-url/ Share on other sites More sharing options...
davidannis Posted April 5, 2013 Share Posted April 5, 2013 Either make all the products conform to a pattern: site.com/category/p-productname site.com/category/sub-category/p-name1 site.com/category/sub-category/sub-sub-category or if you have a name that is ambiguous keep going up levels until you disambiguate: So if you start with: site.com/category/sub-category/product1 = site.com/cars/sports/ferrari and ferrari is in both the product and category table, go up a level and see if the product ferrari is in the sports category - if it is great - if not you are in the subcategory ferrari. Quote Link to comment https://forums.phpfreaks.com/topic/276547-trying-to-identify-categories-from-products-in-a-url/#findComment-1423000 Share on other sites More sharing options...
1internet Posted April 5, 2013 Author Share Posted April 5, 2013 Well I can't change the urls. So any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/276547-trying-to-identify-categories-from-products-in-a-url/#findComment-1423002 Share on other sites More sharing options...
1internet Posted April 5, 2013 Author Share Posted April 5, 2013 Ok, I get it, this is not possible to determine, so I need to create validation so that the product and categories do not have the same urls. So if I do that, should I do the look up for the last array element, like I said at the start. Would that be the best way? Or is it going to slow down things. Quote Link to comment https://forums.phpfreaks.com/topic/276547-trying-to-identify-categories-from-products-in-a-url/#findComment-1423009 Share on other sites More sharing options...
davidannis Posted April 5, 2013 Share Posted April 5, 2013 (edited) I think it is possible to determine. Let me try to explain my example again: Category Table: id Parent id Cat name 1 cars 2 1 sports 3 1 luxury 4 2 ferari Products prodid cat prod name 1 4 ferari 2 4 deluxe ferari now, you ca get 4 kinds of urls: 1. site.com/cars/sports/ferari // cat with name same as product 2. site.com/cars/sports/ferari/ferari //prod with name same as cat 3. site.com/cars/sports // cat name unique 4. site.com/cars/sports/ferari/deluxe ferari //prod name unique You look up the last part of the url by prod name in case 3 you get no results for prod "sports" so you know the url ends in a category Once you find the product record you look at the category column. In case 1 you see cat id is 4 - read the category table and you see that the next level up is not ferari - so you are in the category ferari In case 2 you see cat id is 4 - read the category table and the Cat name column == the name of cat 4 - so it is the ferari product in case 4 you see cat id is 4 - read the category table and the Cat name column == the name of cat 4 - so it is the deluxe ferari product Edited April 5, 2013 by davidannis Quote Link to comment https://forums.phpfreaks.com/topic/276547-trying-to-identify-categories-from-products-in-a-url/#findComment-1423059 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.