1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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

Discussion in 'PHP' started by FishSword, Jun 22, 2013.

  1. #1
    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:
     
    FishSword, Jun 22, 2013 IP
  2. Deltazon

    Deltazon Member

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    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.
     
    Deltazon, Jun 22, 2013 IP
  3. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    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?
     
    FishSword, Jun 22, 2013 IP
  4. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    Does that make sense? Can anyone help with this?
     
    FishSword, Jun 25, 2013 IP
  5. Marcel Preda

    Marcel Preda Member

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    43
    #5
    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
     
    Marcel Preda, Jun 26, 2013 IP
  6. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    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
     

    Attached Files:

    FishSword, Jun 26, 2013 IP
  7. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    Can anyone help with this?
    I don't know what further information I can provide.
     
    FishSword, Jun 28, 2013 IP
  8. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #8
    See what Marcel Preda posted earlier. If it doesn't do what you thought it should, please explain the differences.
     
    ActiveFrost, Jun 28, 2013 IP
  9. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #9
    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!
     
    FishSword, Jun 28, 2013 IP
  10. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #10
    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.
     
    FishSword, Jun 29, 2013 IP