1

I google and google and read 100000 tutorials but i think this is inposible to do in codeigniter on model, controller and views.

Am tring to show my database records like this :

Default Category
   |----- Sub category
          | ----One more category
               |----- Somthing else 

I try with LFT and RGT cols but i realy dont much understand that concept.

I read this and try that function to optimize in Codeigniter model but that only work in model. http://www.sitepoint.com/hierarchical-data-database-2/

http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

I now have simple db sheme : Categories with cols id, parent_id, title

Any1 can give me one simple example... Please

Thanks

1
  • you need to use nested foreach()
    – Kyslik
    Commented Aug 7, 2013 at 17:38

1 Answer 1

0

Assuming the follow basic table structure:

  • id
  • parent_id
  • name

You need to do a nested loop.

Something like:

SELECT * FROM table WHERE parent_id = 0; # First level
SELECT * FROM table WHERE parent_id = ?; # Subsequent levels

Normally you would then build this into an object in your model which would return something like an array with children in it. We would normally do this with a function that calls itself and returns an object that we append to the previous object. Watch out with recursive functions though, they have a tendency to go wrong on you!

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.