0

I am working on a php project in which I am stuck on the category section. Categories are stored in the database like :

id  parent_id   category
1   0           MainCatA                    
2   0           MainCatB                    
3   0           MainCatC                    
4   1           ChildCatD                   
5   4           ChildCatE
6   2           ChildCatF

and so on.

Everything is working fine if someone click on the link like <a href="www.example.com/?cat=3">MainCatC</a> and obviously URL become www.example.com/?cat=3 and also same for the other. But I need to change the URL pattern like if someone click on the link like <a href="www.example.com/?cat=5">ChildCatE</a> then the URL will become www.example.com/MainCatA/ChildCatD/ChildCatE like wordpress and magento URL pattern. I need any advice or sample code to achive this URL pattern. I have surf google to get an idea for the same but everywhere suggested htaccess and other stuff which is not understandable. Might be the answer of the question is long for you but I need to know how this works?

any help would be much appreciated.

Nick
  • 3
  • 9
  • sorry, but `htaccess` _is_ the way to go! [This](http://stackoverflow.com/questions/23927446/htaccess-rewriterule-and-condition?lq=1) question & answers should do the trick! – Jeff Jun 19 '16 at 09:57
  • Thank You @Jeff. But I am not able to understand those. I need to apply category name as deeper they are in database to the URL. :( – Nick Jun 19 '16 at 10:03
  • I cannot explain more or better than it was already explained... google 'htaccess rewrite' – Jeff Jun 19 '16 at 10:12
  • Click on `?cat=3` and then become `MainCatA/ChildCatD/ChildCatE`? Are you sure you don't want it the other way round? The whole point of user-friendly URLs is that the user does see them. – Álvaro González Jun 19 '16 at 10:57
  • @ÁlvaroGonzález its `?cat=5` – Nick Jun 19 '16 at 10:59
  • So... You really want the user to see the `?cat=5` part?? – Álvaro González Jun 19 '16 at 11:07
  • @ÁlvaroGonzález Yes – Nick Jun 19 '16 at 16:02
  • @ÁlvaroGonzález I want to user se the this link on address bar www.example.com/MainCatA/ChildCatD/ChildCatE. When he hit www.example.com/?cat=5. – Nick Jun 19 '16 at 16:11
  • @ÁlvaroGonzález, I have follow some htaccess tutorial and all the answer on SO i.e possible duplicates show that how we append a specific text to the url. But can some some suggest how to append them dynamically with categories. – Nick Jun 20 '16 at 09:12
  • I have the impression that your question has nothing to do with .htaccess, URLs or SEO and what you are really asking is how to get a path from a hierarchical structure stored in a database. That's an entirely different subject and you should start by sharing your current PHP code to retrieve categories (if any). – Álvaro González Jun 20 '16 at 09:16

0 Answers0