Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help in creating dynamic menu
    primarykey
    data
    text
    <p>I am developing a website using PHP and this is the 1st time I am trying a dynamic menu.</p> <p>What I am doing is I have created a table for pages.</p> <p>The table structure is as follows:</p> <pre><code>____________________________________________________________________________ |page_id|title|url|content|menu_title|show_on_navuigation|is_sub|parent_page| |_______|_____|___|_______|__________|___________________|______|___________| </code></pre> <p>Here <code>is_sub</code> indicates whether it is a dropdown menu of some main menu. and <code>parent_page</code> is the main menu of the sub menu.</p> <p>Now, what I want is, like</p> <p>I have created 2 parent pages:</p> <p><code>About Us</code> and <code>Test</code></p> <p>and 2 sub menu's: Test aboutus and testsub,</p> <p>Test aboutus is sub of About Us and testsub is sub of test.</p> <p>How actually should the query and the loop, so that the menu renders perfectly.</p> <p>Thanks in advance.</p> <p>This is my menu structure:</p> <pre><code>&lt;ul&gt; &lt;li class='has-sub'&gt;&lt;a href='#'&gt;&lt;span&gt;About Us&lt;/span&gt;&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href='corporateprofile'&gt;&lt;span&gt;Corporate Profile&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li class='last'&gt;&lt;a href='visionandmission'&gt;&lt;span&gt;Vision &amp;amp; Mission&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class='has-sub'&gt;&lt;a href='#'&gt;&lt;span&gt;Business Services&lt;/span&gt;&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href='recruitment'&gt;&lt;span&gt;Recruitment&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='training'&gt;&lt;span&gt;Training&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='executivesearch'&gt;&lt;span&gt;Executive Search&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='payroll'&gt;&lt;span&gt;Payroll&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li class='last'&gt;&lt;a href='backgroundverification'&gt;&lt;span&gt;Background Verification&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class='has-sub'&gt;&lt;a href='#'&gt;&lt;span&gt;Employers&lt;/span&gt;&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href='enquiry'&gt;&lt;span&gt;Enquiry&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='jobdescription'&gt;&lt;span&gt;Job Description&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='employercontract'&gt;&lt;span&gt;Employer Contract&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li class='last'&gt;&lt;a href='feedback'&gt;&lt;span&gt;Feedback&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class='has-sub'&gt;&lt;a href='javascript:;'&gt;&lt;span&gt;Job Seeker&lt;/span&gt;&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href='applyforjob'&gt;&lt;span&gt;Apply For Job/Register&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='careertips'&gt;&lt;span&gt;Career Tips&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='interview Questions'&gt;&lt;span&gt;Interview Questions&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='interviewprocess'&gt;&lt;span&gt;Interview Process&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li class='last'&gt;&lt;a href='corporatedress'&gt;&lt;span&gt;Corporate Dress&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class='has-sub'&gt;&lt;a href='#'&gt;&lt;span&gt;Franchise&lt;/span&gt;&lt;/a&gt; &lt;ul&gt; &lt;li class='last'&gt;&lt;a href='#'&gt;&lt;span&gt;Franchise Enquiry Form&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class='last'&gt;&lt;a href='#'&gt;&lt;span&gt;Contact us&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;?php function displayMenu($parent_page_id) { $sql = "SELECT * FROM `pages` WHERE `parent_page` = '$parent_page_id'"; // sql $result = mysql_query($sql); if( mysql_num_rows($result) === 0 ) { // mysql_num_rows() is deprecated, but you are using mysql extension so that's why I show it here return true; // exit function } // else, continue to rest of function echo '&lt;ul&gt;'; while($row = mysql_fetch_assoc($result)) { // deprecated mysql php function here for simplicity echo '&lt;li&gt;&lt;a href="' . $result['url'] . '"&gt;' . $result['menu_title'] . '&lt;/a&gt;'; // no closing &lt;/li&gt; yet displayMenu($row['page_id']); // this is the recursive part echo '&lt;/li&gt;'; // close the &lt;li&gt; from before the recursion } echo '&lt;/ul&gt;'; } $get_base_menu = mysql_query("SELECT * FROM pages WHERE parent_page = 0"); while($fetch_base_menu = mysql_fetch_array($get_base_menu)) { $parent_page_id = $fetch_base_menu['page_id']; displayMenu($parent_page_id); } </code></pre> <p>?></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload