arunpatal Posted November 4, 2013 Share Posted November 4, 2013 (edited) Hi, The code is not able to loop topic_title on 2nd foreach loop..... Its looping the subcategory list but not topic_title. It shows only one topic_title under each subcategory Please help <?php if (isset($_GET["datatb"]) AND isset($_GET["cat_id"])){ $datatb = $_GET["datatb"]; $cat_id = $_GET["cat_id"]; $categories = array(); $result = mysql_query("SELECT s.sub_cat_id, s.sub_cat_name, t.sub_id, t.topic_title FROM $sub_category s INNER JOIN $datatb t ON(s.sub_cat_id=t.sub_id)"); while($row = mysql_fetch_assoc($result)) { $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']); $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']); } foreach($categories as $sub_cat_id => $row): ?> <h4><?php echo $row['name']; ?></h4> <ul> <?php foreach($row['topics'] as $sub_cat_id => $sub_row): ?> <li><?php echo $sub_row['name']; ?></li> <?php endforeach; ?> </ul> <?php endforeach; } ?> Edited November 4, 2013 by arunpatal Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/ Share on other sites More sharing options...
dalecosp Posted November 4, 2013 Share Posted November 4, 2013 You're overwriting the variable "$row" here: foreach($categories as $sub_cat_id => $row): ?> Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456936 Share on other sites More sharing options...
arunpatal Posted November 4, 2013 Author Share Posted November 4, 2013 You're overwriting the variable "$row" here: foreach($categories as $sub_cat_id => $row): ?> ok.. i changed it but still showing only 1 topic under subcategory list <?php if (isset($_GET["datatb"]) AND isset($_GET["cat_id"])){ $datatb = $_GET["datatb"]; $cat_id = $_GET["cat_id"]; $categories = array(); $result = mysql_query("SELECT s.sub_cat_id, s.sub_cat_name, t.sub_id, t.topic_title FROM $sub_category s INNER JOIN $datatb t ON(s.sub_cat_id=t.sub_id)"); while($row = mysql_fetch_assoc($result)) { $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']); $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']); } foreach($categories as $sub_cat_id => $row): ?> <h4><?php echo $row['name']; ?></h4> <ul> <?php foreach($row['topics'] as $sub_id => $sub_row): ?> <li><?php echo $sub_row['name']; ?></li> <?php endforeach; ?> </ul> <?php endforeach; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456940 Share on other sites More sharing options...
dalecosp Posted November 4, 2013 Share Posted November 4, 2013 Um, you didn't change it. Not in the post above, anyway. It still says: foreach($categories as $sub_cat_id => $row): Of course, you may have a more insidious issue. Your while() is overwriting $row, too.Something like: $x = 0; while($row = mysql_fetch_assoc($result)) { $categories[$x][$row['sub_cat_id']] = array('name' => $row['sub_cat_name']); $categories[$x][$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']); $x++; } Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456943 Share on other sites More sharing options...
Barand Posted November 4, 2013 Share Posted November 4, 2013 Why have you started a new topic. Now I'll have to search the forum for your earlier one to get your original table structure to give a revised solution. One day, maybe. Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456944 Share on other sites More sharing options...
arunpatal Posted November 4, 2013 Author Share Posted November 4, 2013 Sorry that i started this topic again......... dalecosp can you please edit the code which i posted and make it fine so that it work.... Please............. Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456948 Share on other sites More sharing options...
dalecosp Posted November 4, 2013 Share Posted November 4, 2013 Probably not while I'm at work ;-)We'll see if time allows later. Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456950 Share on other sites More sharing options...
arunpatal Posted November 4, 2013 Author Share Posted November 4, 2013 Probably not while I'm at work ;-) We'll see if time allows later. OK.... Will be waiting Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456951 Share on other sites More sharing options...
cyberRobot Posted November 4, 2013 Share Posted November 4, 2013 You're overwriting the variable "$row" here: foreach($categories as $sub_cat_id => $row): ?> Overwriting $row shouldn't be the issue. The foreach loop isn't being executed in the while loop. Is the following line supposed to maintain all the topic titles? $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']); If so, the issue is probably caused by the following line: $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']); Every time $row['sub_cat_id'] is the same, it overwrites the old value with the new array. Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456954 Share on other sites More sharing options...
arunpatal Posted November 5, 2013 Author Share Posted November 5, 2013 I think overwriting is not the problem...... Here you see this image The problem is that LOOP and Array subcategory have more topics. Its showing only 1st topic and not all Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456958 Share on other sites More sharing options...
arunpatal Posted November 5, 2013 Author Share Posted November 5, 2013 This is how my tables look like subcategory sub_cat_id int auto_increment primary key,sub_cat_name varchar(500),category_id int(11) $datatb id int auto_increment primary key, topic_title mediumtext, topic_detail mediumtext, added_date date, sub_id int(11) => (sub_cat_id from subcategory table) Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456961 Share on other sites More sharing options...
cyberRobot Posted November 5, 2013 Share Posted November 5, 2013 Have you tried outputting the array to make sure it contains what you expect. You could try adding the following after your while loop which creates the $categories array. print '<pre>' . print_r($categories, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456963 Share on other sites More sharing options...
arunpatal Posted November 5, 2013 Author Share Posted November 5, 2013 Hard to understand where to add this print '<pre>' . print_r($categories, true) . '</pre>'; Here is the code i am using.... <?php if (isset($_GET["datatb"]) AND isset($_GET["cat_id"])){ $datatb = $_GET["datatb"]; $cat_id = $_GET["cat_id"]; $categories = array(); $result = mysql_query("SELECT s.sub_cat_id, s.sub_cat_name, t.sub_id, t.topic_title FROM $sub_category s INNER JOIN $datatb t ON(s.sub_cat_id=t.sub_id)"); while($row = mysql_fetch_assoc($result)) { $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']); $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']); } foreach($categories as $sub_cat_id => $row): ?> <?php echo "<green>".$row['name']."</green> <br>"; ?> <?php foreach($row['topics'] as $sub_id => $sub_row): echo $sub_row['name']."<br><br>"; endforeach; ?> <?php endforeach; } ?> Can you kindly edit it.... Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456967 Share on other sites More sharing options...
cyberRobot Posted November 5, 2013 Share Posted November 5, 2013 Just add the line after the closing curly bracket of the while loop and before the foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456968 Share on other sites More sharing options...
arunpatal Posted November 5, 2013 Author Share Posted November 5, 2013 Just add the line after the closing curly bracket of the while loop and before the foreach loop. its displaying same........ only one topic under each subcategory Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456969 Share on other sites More sharing options...
cyberRobot Posted November 5, 2013 Share Posted November 5, 2013 As I mentioned in Reply 9, it looks like you are overwriting the array. To avoid the new array from wiping out what's already in place, you could try something like this: while($row = mysql_fetch_assoc($result)) { if(!isset($categories[$row['sub_cat_id']])) { $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']); } $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']); } Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1456977 Share on other sites More sharing options...
arunpatal Posted November 5, 2013 Author Share Posted November 5, 2013 cyberRobot Hi, I tried as you said.... But still displaying only one topic title....... <?php if (isset($_GET["datatb"]) AND isset($_GET["cat_id"])){ $datatb = $_GET["datatb"]; $cat_id = $_GET["cat_id"]; $categories = array(); $result = mysql_query("SELECT s.sub_cat_id, s.sub_cat_name, t.sub_id, t.topic_title FROM $sub_category s INNER JOIN $datatb t ON(s.sub_cat_id=t.sub_id)"); while($row = mysql_fetch_assoc($result)) { if(!isset($categories[$row['sub_cat_id']])) { $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']); } $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']); } foreach($categories as $sub_cat_id => $row): ?> <?php echo "<green>".$row['name']."</green> <br>"; ?> <?php foreach($row['topics'] as $sub_id => $sub_row): ?> <?php echo $sub_row['name']."<br><br>"; ?> <?php endforeach; ?> <?php endforeach; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1457028 Share on other sites More sharing options...
cyberRobot Posted November 5, 2013 Share Posted November 5, 2013 Have you tried displaying the $row value in the while loop to see if it contains what you expect? You could use the same code as used in Reply 12. Of course, you would replace the $categories variable with $row. Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1457043 Share on other sites More sharing options...
Solution arunpatal Posted November 5, 2013 Author Solution Share Posted November 5, 2013 Thanks cyberRobot and all others who helped me via this... now its working while($row = mysql_fetch_assoc($result)) { $categories[$row['sub_cat_id']]['subcat'] = array('name' => $row['sub_cat_name']); $categories[$row['sub_cat_id']]['topics'][$row['id']] = array('name' => $row['topic_title']); } foreach($categories as $sub_cat_id => $row): ?> <?php echo $row['subcat']['name']."<br>"; ?> <?php foreach($row['topics'] as $sub_id => $sub_row): ?> <?php echo $sub_row['name']."<br>"; ?> <?php endforeach; ?> <?php endforeach; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/283595-can-not-loop/#findComment-1457055 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.