andz Posted February 26, 2009 Share Posted February 26, 2009 Good day guys Could you help me out sort this problem. The table below will illustrate what i need to achieve. Parent 1 Sub Parent 1.1 Child 1000 Child 2000 Parent 2 Sub Parent 2.2 Child 3000 Child 4000 Parent 3 Sub Parent 3.3 I want to create a loop that will look like that on the result but i'm not good enough on it. Ciould you help me guys? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/146961-solved-need-help-on-looping/ Share on other sites More sharing options...
allworknoplay Posted February 26, 2009 Share Posted February 26, 2009 Parent 1 Sub Parent 1.1 Child 1000 Child 2000 Parent 2 Sub Parent 2.2 Child 3000 Child 4000 Parent 3 Sub Parent 3.3 if($parent1) { if($sub_parent1) { echo "child1000"; echo "child2000"; } }elseif($parent2) { if($sub_parent2) { echo "child3000"; echo "child4000"; }elseif($parent3) { if($sub_parent3) { echo "child5000"; } } Quote Link to comment https://forums.phpfreaks.com/topic/146961-solved-need-help-on-looping/#findComment-771500 Share on other sites More sharing options...
andz Posted February 26, 2009 Author Share Posted February 26, 2009 i want that data to be pulled out from database. is it possible to do it using foreach() loop? Quote Link to comment https://forums.phpfreaks.com/topic/146961-solved-need-help-on-looping/#findComment-771558 Share on other sites More sharing options...
drisate Posted February 26, 2009 Share Posted February 26, 2009 How abbout something like this: $command1 = mysql_query("Select * FROM $table ") or die(mysql_error()); while ($data1 = mysql_fetch_array($command1)){ echo $data1[parent]; $command2 = mysql_query("Select * FROM $table where parent='$data1[parent]'") or die(mysql_error()); while ($data2 = mysql_fetch_array($command2)){ echo " ".$data2[sub]; $command3 = mysql_query("Select * FROM $table where sub='$data2[sub]'") or die(mysql_error()); while ($data3 = mysql_fetch_array($command3)){ echo " ".$data2[child]; } } } Whats up allworknoplay :-) Nice to see your stickying arround Quote Link to comment https://forums.phpfreaks.com/topic/146961-solved-need-help-on-looping/#findComment-771567 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.