Jump to content

Always Display Child Pages on Parent Pages and Child Pages PHP MySQL


FishSword

Recommended Posts

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;

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 />';
    }
}
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.