ricky spires Posted January 18, 2012 Share Posted January 18, 2012 hello. say i have list and i want the correct children under each parent so i have $id $parent_id $type $name in the db i have 4 parents each with 2 children id=1 - type=parent - parent_id=0 - name=p1 id=2 - type=parent - parent_id=0 - name=p2 id=3 - type=parent - parent_id=0 - name=p3 id=4 - type=parent - parent_id=0 - name=p4 id=5 - type=child - parent_id=1 - name=c1 id=6 - type=child - parent_id=1 - name=c2 id=7 - type=child - parent_id=2 - name=c3 id=8 - type=child - parent_id=2 - name=c4 id=9 - type=child - parent_id=3 - name=c5 id=10 - type=child - parent_id=3 - name=c6 id=11 - type=child - parent_id=4 - name=c7 id=12 - type=child - parent_id=4 - name=c8 so how would i do the code? i thought i could do it like this but its not working. <?php $family = Family::find_all(); foreach($family as $familys){ $id = $familys->id; $type = $familys->type; $parent_id = $familys->parent_id; $name = $familys->name; echo' <ul>'; if($type == "parent"){ echo $name; } echo' </ul> <li>'; if($type == "child" && $parent_id == $id){ echo $name; } echo' </li>'; } ?> all i get back is p1 p2 p3 p4 no children ?? if i remove $parent_id == $id from if($type == "child" && $parent_id == $id){ i get this p1 p2 p3 p4 c1 c2 c3 c4 c5 c6 c7 c8 but they are not listed under the correct parent i also tried moving the </ul> to the bottom but i get the same <?php $family = Family::find_all(); foreach($family as $familys){ $id = $familys->id; $type = $familys->type; $parent_id = $familys->parent_id; $name = $familys->name; echo' <ul>'; if($type == "parent"){ echo $name; } echo' <li>'; if($type == "child"){ echo $name; } echo' </li> </ul>'; } ?> any thoughts? thanks rick Quote Link to comment https://forums.phpfreaks.com/topic/255274-how-do-i-reference-a-parent-id/ Share on other sites More sharing options...
dzelenika Posted January 18, 2012 Share Posted January 18, 2012 This is not so simple as you tried. You need two nested loops, outer one for parents the one and inner one for children of selected parent Quote Link to comment https://forums.phpfreaks.com/topic/255274-how-do-i-reference-a-parent-id/#findComment-1308840 Share on other sites More sharing options...
ricky spires Posted January 18, 2012 Author Share Posted January 18, 2012 thanks. ill have another go and post my code Quote Link to comment https://forums.phpfreaks.com/topic/255274-how-do-i-reference-a-parent-id/#findComment-1308854 Share on other sites More sharing options...
AyKay47 Posted January 18, 2012 Share Posted January 18, 2012 really, this could/should be done with your query, can you post the relevant class/method code. Quote Link to comment https://forums.phpfreaks.com/topic/255274-how-do-i-reference-a-parent-id/#findComment-1308857 Share on other sites More sharing options...
ricky spires Posted January 18, 2012 Author Share Posted January 18, 2012 sorry but could you give an example please. this is my query public static function find_all(){ global $database; $sql = "SELECT * FROM ".self::$table_name.""; return self::find_by_sql($sql); } thanks Quote Link to comment https://forums.phpfreaks.com/topic/255274-how-do-i-reference-a-parent-id/#findComment-1308876 Share on other sites More sharing options...
mikosiko Posted January 18, 2012 Share Posted January 18, 2012 you are implementing an "Adjacency List"; this query let you get the children of a node. SELECT t1.id, t1.name, t2.id, t2.name FROM your-table AS t1 LEFT JOIN your-table AS t2 ON t2.parent_id = t1.id Quote Link to comment https://forums.phpfreaks.com/topic/255274-how-do-i-reference-a-parent-id/#findComment-1308882 Share on other sites More sharing options...
ricky spires Posted January 18, 2012 Author Share Posted January 18, 2012 i've done it public static function find_all(){ $sql = "SELECT * FROM ".self::$table_name.""; return self::find_by_sql($sql); } public static function find_by_parent($parent_id){ $sql = "SELECT * FROM ".self::$table_name." WHERE parent_id=".$parent_id.""; $result_array = self::find_by_sql($sql); return $result_array; } <?php $family = Family::find_all(); foreach($family as $familys){ $parent_id = $familys->id; $type = $familys->type; $name = $familys->name; echo' <ul>'; if($type == "parent"){ echo $name; } echo' <li>'; $child = Family::find_by_parent($parent_id); foreach($child as $childs){ $child = $childs->name; echo $child; } echo' </li> </ul>'; } ?> that echos p1 c1c2 p2 c3c4 p3 c5c6 p4 c7c8 thanks all p.s mikosiko = your way looks interesting. im not sure if i fully understand what your doing but if it can be used to make my code better i would love to see it. thanks Quote Link to comment https://forums.phpfreaks.com/topic/255274-how-do-i-reference-a-parent-id/#findComment-1308884 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.