CodeIgniter Forums
Parent-Child-Grandchild Hierarchy - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Parent-Child-Grandchild Hierarchy (/showthread.php?tid=52171)



Parent-Child-Grandchild Hierarchy - El Forum - 05-31-2012

[eluser]NikosV[/eluser]
Here's my problem.
I have 3 tables in my database. Collections, Boxsets and CDs.
These tables are connected as following:
A Collection has one or more Boxsets
A Boxset has one or more CDs

I created the following columns to each table:
Collections
collection_id - collection_title
Boxsets
boxset_id - boxset_title - collection_id
CDs
cd_id - cd_title - boxset_id

I am trying to get all available data and structure these data in an array in this manner:
-Collection 1
--Boxset1
---CD 1
---CD 2
--Boxset 2
---CD 1
...
-Collections 2
....

I am going fine for the first two tables using the code below:

Code:
$collections = $this->collection_model->get_all_collections();
  foreach($collections->result_array() as $row){
   $arrayCollections[$row['collection_id']] = $row;
  }  
  $boxsets = $this->boxset_model->get_all_boxsets();
  foreach($boxsets->result_array() as $row){
   $arrayCollections[$row['collection_id']]['boxsets'][] = $row;
  }
  $cds = $this->cd_model->get_all_cds();
  foreach($cds->result_array() as $row){
          ........
                        ........
  }

How should i add the third table (CDs) since the collection_id will no longer be available when i get the CDs? Should i add a collection_id field in CDs table? Is there another way to achieve the same result?

Thanks in advance for your help ;-)


Parent-Child-Grandchild Hierarchy - El Forum - 05-31-2012

[eluser]NikosV[/eluser]
no answers? i thought that would be easy..


Parent-Child-Grandchild Hierarchy - El Forum - 06-01-2012

[eluser]NikosV[/eluser]
My solutions so far is creating two different arrays. However it doesn't look as the optimal solution. Any suggestions fore improvements?

Code:
$collections = $this->collection_model->get_all_collections();
  foreach($collections->result_array() as $row){
   $arrayCollections[$row['collection_id']] = $row;
  }  
  $boxsets = $this->boxset_model->get_all_boxsets();
  foreach($boxsets->result_array() as $row){
   $arrayCollections[$row['collection_id']]['boxsets'][] = $row;
  }
  foreach($boxsets->result_array() as $row){
   $arrayBoxset[$row['boxset_id']] = $row;
  }  
  $cds = $this->cd_model->get_all_cds();
  foreach($cds->result_array() as $row){
   $arrayBoxset[$row['boxset_id']]['cds'][] = $row;
  }
//run through tables and echo data
  foreach($arrayCollections as $collections){
//1st level
   echo '-'.$collections['collection_title'].'<br />';
   foreach($collections['boxsets'] as $boxset){
//2nd level
    echo '--'.$boxset['boxset_title'].'<br />';
    foreach($arrayBoxset[$boxset['boxset_id']]['cds'] as $cd){
//3rd level
     echo '---'.$cd['title'].'<br />';
    }
   }  
  }