1

I have created a CMS website, now I am trying to make site map with php coding, but the code which i have made is not complete. i.e It is not showing all the sub menu data.

Please look at this image . According to this image, I am having many other sub menu under Weight training(sub menu of Fitness Exercises) ,but they are not visible. Why they are not visibile?

Please guide/help me to solve this issue

My php code

   foreach ($query_sitemap as $artrow) {
        $datefromsql = $artrow->created_date;
          $time = strtotime($datefromsql);  ?> 

                <p>    
                        <ul>
                            <?php echo '<b>'.$artrow->menu_name.'</b>'; ?>


                                <?php $submenucon=$this->menumodel->fetch_menu_byPid($artrow->id);//i.e select*from menu where parent_id=$mid
                                 if (empty($submenucon)) { ?>
                                  ---
                                 <?php
                                    } else {
                                         foreach ($submenucon as $subtrow) {
                                             ?>
                                              <?php echo '<li style="color:gray;margin:">'.$subtrow->menu_name.'</li><br/>'; ?>
                                      <?php
                                         }
                                    }
                                 ?>    
                        </ul> 
                        </p> 
                        <?php
                        $i++;
                    }
                }    ?> 
user2173803
  • 53
  • 2
  • 8
  • its very difficult for me. please any one tell me some logic. please – user2173803 Jul 16 '13 at 10:23
  • Not sure what the problem is, but all you need to do is put one line in the sitemap for each unique page/post, not a line for each menu item unless each item is itself a unique page, The page order in the sitemap is not generally important so you don't have to order it to match your menu. If the logic to get your own site structure is difficult, then perhaps you should consider changing how you structure your database – Anigel Jul 16 '13 at 10:26
  • So u have submenus in a submenu ? Then u should use some recursion to fetch all menus – DarkBee Jul 16 '13 at 10:31
  • @Anigel eg. http://www.w3.org/WAI/sitemap.html .what code u think i have to add in my line to make? – user2173803 Jul 16 '13 at 10:32
  • Just loop through all your pages and add them to the sitemap, categories are not relevant – Anigel Jul 16 '13 at 10:33
  • @DarkBee Yeah yeah yeah, recursion. I was thinking, Can plz post sample code for my above problem. It will be helpful. Because i am unable to write that code, i think that is out of my mind capacity – user2173803 Jul 16 '13 at 10:33
  • @Anigel "Looping thorugh" how to write that code , plz post a sample answer code below – user2173803 Jul 16 '13 at 10:34
  • Try a search for php loop through array there are hundreds of examples already on this site. – Anigel Jul 16 '13 at 10:36
  • @DarkBee What you said is correct, now i solved my this problem using recursion – user2173803 Jul 16 '13 at 21:24

1 Answers1

1

You're dealing with a tree structure here represented (in a most siplistic/naive way) in a database. So the first thing you should do is to build a tree-like structure based on returned rows and only then traverse it recrusively in order to display.

You can read more on parsing tree-like tables here:

Is it possible to query a tree structure table in MySQL in a single query, to any depth?

Implementing a hierarchical data structure in a database

What is the most efficient/elegant way to parse a flat table into a tree?

Community
  • 1
  • 1
Jarek.D
  • 1,274
  • 1
  • 8
  • 18
  • yeah i saw your example they are similar with my problem. But still i am very confused how to write its code for my above problem. Please modify my question and post answer for it. It will be very useful for me – user2173803 Jul 16 '13 at 15:45
  • i found this link http://paratechnical.blogspot.in/2011/02/php-sitemap-using-tree-data-structure.html Can u plz write code for my above problem. I am not cable to this type of task but i have to do it, to save my job – user2173803 Jul 16 '13 at 18:54
  • Once u got the hang of the nested set approach its realy nice to build dynamic menus – DarkBee Jul 17 '13 at 13:30