In my site I have a database that lists categories of movies books etc etc. Here is something of what the database looks like:
id name derivedfrom
1 movies 0
2 action 1
3 comedy 1
4 black hawk down 2
5 Books 0
6 Bourne Identity 2
7 Harry Potter 5
The derivedfrom column means which ccategory does that category belong to. The while loop I used to print them out onto the page is:
$cats = mysql_query("SELECT * FROM categories ORDER BY derivedfrom,name") or die(mysql_error());
while ($cat = mysql_fetch_object($cats)) {
$loop = $cat->derivedfrom * 2;
$x = 0;
echo "<option value=\"" . $cat->id . "\">";
while ($x < $loop) {
echo " ";
$x++;
}
echo $cat->name;
}
The database is then printed out like this:
Books
Movies
Action
Comedy
Black Hawk Down
Bourne Identity
Harry Potter
But I do not wanted it printed out like that. I know why it is doing it that way but I do not know how to fix it. This is how I want it:
Books
Harry Potter
Movies
Action
Black Hawk Down
Bourne Identity
Comedy
How can I get the loop to order the rows like that so it is properly ordered and properly aligned. Thank in advance.