Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

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

$
0
0

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 />';
    }
}

Viewing all articles
Browse latest Browse all 13200

Trending Articles