confused to make sitemap using php coding

2019-03-02 02:57发布

问题:

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++;
                    }
                }    ?> 

回答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?