Creating Pages w/ Children

Discussion in 'PHP' started by fireflyproject, Sep 21, 2008.

  1. #1
    I am working on a project that allows users to create pages as children of another page.

    Currently I have this being noted in the new pages row in a mySQL database. The row has a parent column which has the id of the parent page in it.

    However I'm struggling with finding a way to list pages in order by ID, however to have a pages children listed after it.

    What the end result will be is a list of pages, and any page that has children has a list inside of it.

    So the end HTML would look something like this:

    <ul>
    <li>Page 1</li>
    <li>Page 2
       <ul>
       <li>Child 1</li>
       <li>Child 2</li>
       </ul>
    <li>Page 3</li>
    </ul>
    HTML:
    I think I may have to restructure the database, but if I don't have to, that would be awesome.

    Thanks for your advice in advance.
     
    fireflyproject, Sep 21, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Okay, so we'll say that your table has an ID column for the page's ID and then an optional parent ID column that references the ID column of that row's parent if it's a child page. We'll assume the columns' names are 'id' and 'parent' and that the default value for 'parent' is 0. We're also gonna assume you have a column named 'title' for the page's title/name.

    <?
    $query = mysql_query("SELECT * FROM pages");
    $pages = array();
    while($row = mysql_fetch_array($query)) {
      if($row['parent'] != 0) {
        $pages[$row['parent']][1][$row['id']] = $row;
      } else {
        $pages[$row['id']][0] = $row;
      }
    }
    print "<ul>";
    foreach($pages as $id => $page) {
      print "<li>";
      print $page[0]['title'];
      if(is_array($page[1])) {
        print "<ul>";
        foreach($page[1] as $cid => $child) {
          print "<li>" . $child['title'] . "</li>";
        }
        print "</ul>";
      }
      print "</li>"
    }
    print "</ul>";
    
    /*
     After the while loop, your array would look something like this:
     array(1 => array(0 => array("id" => 1, "parent" => 0, "title" => "Page 1"), 1 => array("2" => array("id" => 2, "parent" => 1, "title" => "Child 1"))),
              3 => array(0 => array("id" => 3, "parent" => 0, "title" => "Page 2")));
     And that's assuming you have 3 pages; first one being Page 1, second one being Child 1 which is the child of Page 1, and third one being Page 2 which is the second parent page with no children.
    */
    ?>
    PHP:
    Maybe not the best method but it's what I came up with right away and should work at least.
     
    zerxer, Sep 21, 2008 IP
  3. fireflyproject

    fireflyproject Active Member

    Messages:
    969
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    70
    #3
    It looks like it'll do the job. I'll play around with this code today to see how well it functions. BTW, you had the database down well, I have all those fields in there named just as you said.

    Thanks!
     
    fireflyproject, Sep 22, 2008 IP