this is my database structure for my terms table (categories):
id | name | parent_id | level | position
--------------------------------------------------------------
1 | term 1 | NULL | 0 | 1
2 | term 2 | 1 | 1 | 1
3 | term 3 | 1 | 1 | 2
4 | term 4 | NULL | 0 | 2
5 | term 5 | 4 | 1 | 1
so terms 2 and 3 are 1st level children of 1 and 5 is a first lst level child of 4
this is my query: (and it’s not correct, this should be fixed)
SELECT
`id`,
`name`
FROM
`terms`
ORDER BY
`position` ASC,
`level` ASC
this is my php:
$terms = array();
// query part
if(!$this->_db->resultRows > 0)
return $terms;
while($d = $this->_db->fetch($r))
{
$terms[$d->id] = new Term($d->id);
}
return $terms;
current result:
term 1
term 2
term 5
term 4
term 3
but the result should be:
term 1
term 2
term 3
term 4
term 5
I don’t know how to alter the query to get the correct result
the goal is to output this in a (multiple) select box
I know how to do it with nested lists, but you can’t nest a select
Okay, so there are many ways to deal with this type of problem. All the ways that I will present do not require there to be a level column in the table. In my opinion that is redundant data since it can be deduced from the information found in the other columns.
If you know the maximum “level” will only be 1 (maximum depth is 2) you can use a query like this:
where
$row_countis calculated:There are ways to modify this SQL to make it work with more levels (depth), but the query needs to get bigger with each maximum level/depth it will support.
If you are uncertain about the number of levels you will have, then you should probably just do something like this:
(By the way I recommend using PDO.)