woodplease Posted September 3, 2010 Author Share Posted September 3, 2010 ok, looking at that example, i'm not sure how joining the tables will allow me to display, the query results the way i want them i.e. menu (first loop) submenu (second loop) submenu (second loop) menu (first loop) submenu (second loop) Quote Link to comment https://forums.phpfreaks.com/topic/212441-looping-through-tables/page/2/#findComment-1106952 Share on other sites More sharing options...
wildteen88 Posted September 3, 2010 Share Posted September 3, 2010 You don't want to be looking at the example from that other thread. That was just to demonstrate a simple join and how you categorise your data. Which is basically what you're needing to do. So forgetting that other example. You should look at the code I posted earlier which is this <?php $query = 'SELECT m.section_id, m.section_title, s.section_sub_title, s.section_sub_desc FROM section_main AS m LEFT JOIN section_sub as s ON s.section_title = m.section_title ORDER BY m.section_id, s.section_sub_title ASC'; $result = mysql_query($query); $data = array(); while($row = mysql_fetch_assoc($result)) $data[$row['section_title']][] = $row; ?> <dl> <?php foreach($data as $section_title => $section): ?> <dt><h1><?php echo $section_title; ?></h1></dt> <?php foreach($section as $sub_section): ?> <dd><?php echo $sub_section['section_sub_title']; ?></dd> <?php endforeach; ?> <?php endforeach; ?> </dl> If you ran that code you'll get something similar to this Film Avatar Speed Starteck TV Eastenders Xfactor The output above is from my test data. Your result will differ. How it is displaying like that is to do with the PHP code. First I'm adding all the results into the data array $data = array(); while($row = mysql_fetch_assoc($result)) $data[$row['section_title']][] = $row; I assigned the section_title as the key to the $data array. With print_r you'll see how this array is formatted. This is my result. Array ( [film] => Array ( [0] => Array ( [section_id] => 1 [section_title] => film [section_sub_title] => Avatar [section_sub_desc] => tall blue people ) [1] => Array ( [section_id] => 1 [section_title] => film [section_sub_title] => Speed [section_sub_desc] => Run away bus ) [2] => Array ( [section_id] => 1 [section_title] => film [section_sub_title] => starteck [section_sub_desc] => space odyssey ) ) [tv] => Array ( [0] => Array ( [section_id] => 2 [section_title] => tv [section_sub_title] => eastenders [section_sub_desc] => crap soap ) [1] => Array ( [section_id] => 2 [section_title] => tv [section_sub_title] => Xfacter [section_sub_desc] => for people that cant sing ) ) ) A nice easy to use multi-dimensionally. Now we can easily loop through this array using a couple of loops. All the hard work was done by MySQL. <dl> <?php foreach($data as $section_title => $section): ?> <dt><h1><?php echo $section_title; ?></h1></dt> <?php foreach($section as $sub_section): ?> <dd><?php echo $sub_section['section_sub_title']; ?></dd> <?php endforeach; ?> <?php endforeach; ?> </dl> You can change this to what ever you wish. Just change the html to your liking. Quote Link to comment https://forums.phpfreaks.com/topic/212441-looping-through-tables/page/2/#findComment-1106963 Share on other sites More sharing options...
woodplease Posted September 3, 2010 Author Share Posted September 3, 2010 thanks very much, i understand it now. you've been a great help Quote Link to comment https://forums.phpfreaks.com/topic/212441-looping-through-tables/page/2/#findComment-1106969 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.