PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Im trying to make submenus for a cms im playing with, but i cant find the correct SQL SELECT im suppose to use to get the submenu items. basicly I want it to show up like this
Home
About us
Contact
Test Page
- test page sub
and for each sub to be categorized under its parent. As you see i have a parent column with the id of the items parent. What sql statement would I use to select all subitems of the parent?
I have the code working for my parents but not for the child, here is the current child code:
You can't just straight up query parents and their children, because the children is a separate recordset in and of itself, based on the way you have your DB schema. You'll need to use a nested loop to get each child for each parent. IE:
Hey the moose thanks for the fast reply! Im actually getting submenus now but they are the wrong ones. Check it out, this is my full code from menu.php
Ahh, gotcha. I just mistook your child code for being your parent code. My fault. You had it basically right at the beginning, the only part you missed was you put
$query3 = $database->query("SELECT * FROM pages WHERE parent = $id ORDER by pos");
The first query does not return anything unless the parent id is the current record's ID. If you are on record ID #2, and ID #2 had a parent ID of 2, it would return that row (which doesn't make sense, something can't be it's own parent). The second one puts the current parent ID in the query and will then return the children for that item.