[eluser]PoWah[/eluser]
What is the best way (having in mind performance) to do such thing:
I have in DB a field with information representing pages hierarchy:
Code:
1
1.1
1.2
1.2.1
2
2.1
2.2
...
...
...
11
11.1
11.1.1
11.2
11.2.1
11.2.2
11.3
...
and so on...
.. and I need them to be sorted in exactly the same way as I wrote above. Maybe is there any way to do this with only SQL?
At this moment I know the only way (with PHP sorting):
I get all results from DB and then do my sorting:
Code:
$result_array = array();
foreach ($query->result() as $row)
{
$how_many_dots = substr_count($row->hierarchy, "."); // find how many dots is in 'hierarchy' field data
$result_array[$how_many_dots][] = $row;
}
ksort($result_array); // sort by array key
$keys = array_keys($result_array);
foreach ($keys as $key)
{
$run_query = true;
foreach ($result_array[$key] as $sorted_results)
{
$sorted_result_array[] = sorted_results;
}
}