I have data in mysql table in a parent child hierarchy like;
|---------+-----------+-------------|
| msg_id | parent_id | msg |
|---------+-----------+-------------|
| 1 | NULL | msg1 |
| 2 | NULL | msg2 |
| 3 | NULL | msg3 |
| 4 | 1 | msg1_child1 |
| 5 | 1 | msg1_child2 |
| 6 | 3 | msg3_child1 |
|---------+-----------+-------------|
I need to display it in a parent-child unordered list format like
-msg1
-msg1-child1
-msg2-child2
-msg2
-msg3
-msg3-child1
How do I do it? I need help especially how could I display it in a hierarchy on a form.
Or, close to that.
OK working from the backend towards the front-end...
You could call a single non recursive stored procedure (sproc) from your php script which generates the message hierarchy for you. The advantage of this approach is you only need to make a SINGLE call from php to your database whereas if you use inline SQL then you'll be making as many calls as there are levels (at a minimum). Another advatange is that as it's a non recursive sproc it's extremely performant and it also keeps your php code nice and clean. Finally, and I have to say this for the record, that calling stored procedures is more secure and more efficient than any other method because you only need to GRANT execute permissions to your app user and stored procedures require less round trips to the database than any other methods including parameterised queries which require at least 2 calls for a single query (1 to setup the query template in the db, the other to populate the params)
So here's how you'd call the stored procedure from the MySQL command line.
and here's the resultset it creates.
Ok, so now we have a the ability to fetch a full or partial message tree by simply calling our sproc with whatever starting node we require but what are we going to do with the resultset ??
Well in this example I've decided we're going to generate an XML DOM with it, then all I need to do is transform (XSLT) the XML and we'll have a nested messages web page.
PHP script
The php script is fairly simple, it just connects to the database, calls the sproc and loops the resultset to build the XML DOM. Remember we're only calling into the db once.
XML output
This is the XML that the php script generates. If you save this XML in a file and open it in your browser you'll be able to expand and collapse the levels.
Now you could forego building the XML DOM and using XSL to render a web page if you wish and perhaps just loop the resultset and render the messages directly. I've simply chosen this method to make my example as comprehensive and informative as possible.
MySQL script
This is a complete script including tables, sprocs and test data.
The full source for this answer can be found here : http://pastie.org/1336407. As you'll have noted already I've omitted the XSLT but you probably wont go the XML route and if you do there are heaps of examples on the web.
Hope you find this helpful :)
EDIT:
Added a little more data so you have more than one root message (msg_ids 1,9,14).
Now if you want to just get the messages that are specific to a root node (starting message) you can call the original stored procedure passing in the starting msg_id of the root you require. Using the new data above that would be msg_ids 1,9,14.
you can pass in any msg_id you like so if I want all of the messages below msg 1-2-2-1 then you would pass in msg_id = 6:
However, if you want all of the messages for all of the roots then you can call this new sproc I've created as follows:
The main problem with this is as your message table grows it's going to be returning lots of data which is why I was focusing on a more specific sproc that only fetched messages for a given root node or starting msg_id.
I wont post the new sproc code as it is virtually the same as the original but you can find all the amendments here : http://pastie.org/1339618
The final change you'll need to make is in the php script which will now call the new sproc as follows:
Hope this helps :)