Hold fire if you are confused by the question a second on the question because I’m not sure that it exactly makes sense.
SELECT * FROM `rules` ORDER BY `Rank_1` , `Rank_2` , `Rank_3` , `Rank_4` ASC
Yields:
Copy pasta:
0 0 0 0 0
1 0 0 0 1
1 1 0 0 1.1
1 1 1 0 1.1.1
2 0 0 0 2
4 0 0 0 4
4 1 0 0 4.1
4 1 1 0 4.1.1
4 1 1 9 4.1.1.9
4 1 1 10 4.1.1.10
4 1 1 11 4.1.1.11
However this data is not quite in a form I need it to be in order to do something useful with it.
I want to loop through all the rules and depending on how ‘deep’ I am, do different things. For example I’d like to take RuleID = 0 and do <h1>, 1.1 have <h2> but when it comes to 4.1.x, open up a <ul> and give each rule an <li>.
For this I figure the best way is to select the data like an array where I’d end up with:
array( 4 =>
array( 4.1 =>
array( 4.1.1 => 'rule content')
)
);
Then I could realise my depth and open up a <ul> tag, loop through printing out the rules at that depth etc.
What is really the best way to tackle this? I’ve been at it for ages and don’t have a clue where to go from here to be honest. I really want the data to be in the same table. I figure I could probably solve this if they were all in different tables but I don’t know that for sure it would be any easier.
I started down this route:
foreach($m as $rule) {
$depth = count(explode('.', $rule['ruleid']));
switch($depth) {
case 1:
echo '<h1>'.$rule['content'].'</h1>';
break;
case 2:
echo '<h2>'.$rule['content'].'</h2>';
break;
case 3:
echo '<strong>'.$rule['content'].'</strong>';
break;
}
echo '<br />\n';
}
Then I realised this is just going to deal with each rule entry individually, whereas my solution needs some sort of ‘awareness’ of where it is in the rules, so it can know when to open a tag (such as a <ul>) and then close it again when it’s done echoing list items that might be present in a rule (such as "Don't do: <ul><li>this</li><li>or this</li></ul>")
Here’s an example of desired output from the table of data above:
0. Introduction
…
4. Chat
4.1. Do’s and do not’s
4.1.1. Do:
- 4.1.1.9 Be polite
- 4.1.1.10 Be patient
- 4.1.1.11 Be smart
Hope some bright spark can help!

Suppose, from your input data, you can produce an array like this:
which you could then transform to a tree-like structure with this:
demo: http://codepad.org/vtMPjGoa