I have a problem with my code. What is the best way to display all sub pages for a parent page regardless of whether or not you are viewing the parent or child pages? If a different parent is chosen, then the child pages of the previously selected parent should be hidden. All pages have a id vairable in the URL that is available using $_GET['id']. My MYSQL DB looks like this. CREATE TABLE IF NOT EXISTS `pages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `parent` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8; PHP: With the code I have currently, the child pages display if you are on the parent page, but as soon as you jump to any of the child pages, the child pages disappear. $gParents = mysql_query('SELECT * FROM db.pages WHERE parent = 0'); while($parents = mysql_fetch_assoc($gParents)) { echo '<a href="?id='.$parents['id'].'">'.$parents['title'].'</a><br />'; $qChilds = mysql_query('SELECT * FROM db.pages WHERE parent = '.$parents['id'].' AND parent = '.$_GET['id']); while($childs = mysql_fetch_assoc($qChilds)) { echo '<a href="?id='.$childs['id'].'">'.$childs['title'].'</a><br />'; } } PHP:
Firstly, the subdirectories disappear because you are asking for two parameters to match, both $parents['id'] and $_GET['id']. Do not ask for $_GET['id'] as that is the one you have in your URL (consequently, the query only returns the subdirectory results of the $_GET['id'] directory). Secondly, your queries are vulnerable to SQL injections. Use prepared statements or escape $_GET results. Thirdly, mysql library is deprecated. Use mysqli or PDO instead. Fourthly, learn JOINs which would help you combine both of these queries into one.
Thanks for your response, it's much appreciated. My aim is to create a vertical page menu using <ul>'s. Upon a user clicking a parent menu item, they will taken to a different page where the content of the selected page will be displayed. Any child pages will be displayed as <li>'s of a child <ul> in the menu, underneath the relevent paten in the menu. Child pages need to be visible only when the parent page, or child pages belonging to the parent have been selected. I'm guessing I will need to check if the user is in a parent or child page of the parent, in order to show any relevent child pages?
Hi there, Could you please draw a mock up of the 1st page and the the next one? I did not fully understood what you want to achieve. Anyhow few hints: For sure you will not want ...WHERE parent = '.$parents['id'].' AND parent = '.$_GET['id'] in your case, if $parents['id'] != $_GET['id'] then your query will return nothing. If you want to get the children of the current page, next pice of code could be usefull <?php $page_id = 0; if (array_key_exists('id', $_GET)) { $page_id = (int) $_GET['id']; }; // to get children of current page $qChilds = mysql_query('SELECT * FROM db.pages WHERE parent = '.$page_id); ?> PHP: BR, Marcel
Hi Marcel, Thanks for getting back to me. I have attached a mockup of what I'm trying to achieve. Please find attached.zip file. Extract contents and open index.html Many Thanks, FishSword
See what Marcel Preda posted earlier. If it doesn't do what you thought it should, please explain the differences.
Hi ActiveFrost, I thought I already had explained the differences in the Example.zip file I uploaded, and in my previous post(s). See attachment. I need the pages to function like this. Currently all navigation and page contents are managed by one file. When a user clicks on a menu item, I need to display any child pages as child ul list items. The items need to be display whenever the user navigates to any child page of the parent, or the parent page directly. Once again, see Example.zip If a user navigates to a different parent menu item, then all child for the previously selected parent should be hidden, and the child pages for the new selected page should display. I hope this makes sense!
Can anyone help on this? If's it easier, I can talk on Instant Messaging! Trying to work out the logic for this, is really starting to do my head in.