Results 1 to 6 of 6

Thread: PhP auto generate table

  1. #1
    Join Date
    Jul 2006
    Posts
    142
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default PhP auto generate table

    Hey guys,

    I have weird problem,

    Somewhere in my DB I have a table that had items like this:

    id | Name | Sub_of
    1 | A | 0
    2 | B | 1
    3 | C | 1
    4 | D | 2
    5 | E | 2

    All I am trying to do is automatically generate a table so that I can see all the sub_of like this (A,B,C... are table headers I will be writing the table row by row. e.g. <tr>...</tr> ):
    Code:
    |------------------------------------|
    |----------------A-------------------|
    --------------------------------------
    |-----------B---------- |------C-----|
    --------------------------------------
    |----D--| ----- E ------|------------|
    --------------------------------------
    |--------| --------------|------------|
    |--------| --------------|------------|
    |--------| --------------|------------|
    ...
    I have tried to get the depth of the final table header and the number of final set of columns like this :

    Code:
    function tree_span($category_id,$find) {
      $q = "SELECT category_id, name, id from ".$table_prefix."categories where sub_of = ".$category_id." order by name";
      $query = mysql_query($q);
    
      if (!$query) echo "Database Error : ".$q;
      else {
        $depth = mysql_num_rows($query);
        if($depth !=0) {
          $category_depth = $category_depth+1;
        }
        while ($row=mysql_fetch_row($query)) {
          if ($row[0] != $id) {
            $category_span = $category_span +1;
            tree_span($row[0],$find);
          }
        }
      }
    I hope this makes sense about what I am looking for. Please help.
    Last edited by neo_philiac; 10-30-2010 at 12:07 AM.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    You mean generate an html table from a db table?

    The ., -, and _ aren't clear to me.
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Jul 2006
    Posts
    142
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Well the . - _ are just for formatting purposes. To show that from the mysql table that has the data (sub_of) and then there should be 2 columns under A, the under B there should be two columns and since C has no child, it should be its own column. Does that make sense?

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    databases can't do that. they're fairly flat.
    this is a fairly common method of dealing with multi-dimensional relationships in databases:

    Code:
    |--table---------------|
    |--col A---------------|
    ------------------------
    
    |--table A-------------|
    |--col B----|--col C---|
    ------------------------
    
    |--table B-------------|
    |--col D----|--col E---|
    ------------------------

  5. #5
    Join Date
    Jul 2006
    Posts
    142
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    OK. I guess I didn't make myself clear enough. I am making an html table from the relationship you see from the table. It should be like traversing thru the binary tree but I just dont know how to. I can get the depth and the nodes like this by recursion but not the rest of it:

    Code:
    function tree_span($category_id,$find) {
      $q = "SELECT category_id, name, id from ".$table_prefix."categories where sub_of = ".$category_id." order by name";
      $query = mysql_query($q);
    
      if (!$query) echo "Database Error : ".$q;
      else {
        $depth = mysql_num_rows($query);
        if($depth !=0) {
          $category_depth = $category_depth+1;
        }
        while ($row=mysql_fetch_row($query)) {
          if ($row[0] != $id) {
            $category_span = $category_span +1;
            tree_span($row[0],$find);
          }
        }
      }
    Hope this makes things clear. And 'traq' is right but how do I align the cells? Because I will be writing the cells row by row? To do that we need the end column numbers. Any thoughts?
    Last edited by neo_philiac; 10-27-2010 at 08:53 PM.

  6. #6
    Join Date
    Jul 2006
    Posts
    142
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Any ideas?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •