Bravat Posted March 25, 2011 Share Posted March 25, 2011 I am trying to make menu with submenu. I have two tables: meni(id_meni, naslov, pozicija, lang_id) and subjects (subjecte_id, naslov, text, meni_id, lang_id, pozicija). Into subjects(meni_id) is written meni(id_meni) value in order to determinate what menu items are having summenu options. How can i do this? I tried following code, but it only display items from meni table: <?php $query_meni =mysql_query("SELECT * FROM meni WHERE lang_id = '$jezik'"); $query_subject =mysql_query("SELECT * FROM subjects WHERE lang_id = '$jezik'"); while($row_meni = mysql_fetch_array($query_meni)){ echo "<li>"; echo "<a href=\"index.php?page="; echo $row_meni['id_meni']; echo "\">"; echo $row_meni['naslov']; echo "</a>"; while($row_subjects = mysql_fetch_array($query_subject)){ if($row_meni['id_meni'] == $row_subjects['meni_id']){ echo "<ul class=\"subnav\">"; echo "<li>"; echo "<a href=\"index.php?subject="; echo $row_subjects['subject_id']; echo "\">"; echo $row_subjects['naslov']; echo "</li>"; echo "</a>"; echo "</ul>"; } } echo "</li>"; } ?> Link to comment https://forums.phpfreaks.com/topic/231701-making-menu-with-submenu/ Share on other sites More sharing options...
yonny Posted March 26, 2011 Share Posted March 26, 2011 Hi, I have done these menus before and I think you need to start with a <ul> before any while loop. While inside the first loop for the fist menu print <li> link </li> then if the second sql query has a result the second <ul> that starts the submenu needs to print before the second while loop. The second while loop needs to print only <li> link </li>. once that loop is done close the second </ul> ending the submenu. After you closed the first loop then close </li> and </ul>. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/231701-making-menu-with-submenu/#findComment-1192368 Share on other sites More sharing options...
yonny Posted March 26, 2011 Share Posted March 26, 2011 this is the code you posted on another post <li><a href="#">Home</a></li> <li><a href="#">About Us</a></li> <li><a href="#">Web Design</a> <ul class="subnav"> <li><a href="#">Corporate</a></li> <li><a href="#">Small Business</a></li> <li><a href="#">HTML Templates</a></li> <li><a href="#">Joomla Templates</a></li> </ul> </li> so to make it work with what you have // make your sql_query // count the affected rows // if not 0 start the menu by printing <ul> // start your first loop print "<li><a href=\"$some_link\">$some_column</a>"; \\ do not close <li> yet because you dont know if it has a submenu // do the second sql query // count the affected rows // if 0 rows then print "</li>"; \\ to close that one and move on with the next // else (not 0 rows) then print "<ul class=\"subnav\">"; // now run your second while loop print"<li><a href=\"$sub_page\">$submenu_item</a></li>"; //etc <li><a href="#">Small Business</a></li> <li><a href="#">HTML Templates</a></li> <li><a href="#">Joomla Templates</a></li> // endwhile; print"</ul>"; endif; print"</li>"; //and repeat if for some reason you need to link to anchors in the same page you need to set the mainmenu link to "#" as opposed to "website_file.php#" and the submenues to "#anchor" instead of "website_file.php#anchor" or they will not work. i used PHP_SELF to check if I was inside the page or not. so if the page I was in was about.php and i had anchors in it i would print"<li><a href=\"#123\">$submenu_item</a></li>"; if it was not about.php i would print"<li><a href=\"about.php#123\">$submenu_item</a></li>"; Link to comment https://forums.phpfreaks.com/topic/231701-making-menu-with-submenu/#findComment-1192384 Share on other sites More sharing options...
Bravat Posted March 26, 2011 Author Share Posted March 26, 2011 I think that I am doing something wrong here, but I cannot see error. I managed to create submenu for specific menu, but it only shows one item in subemnu. Here is the code: <?php $query_meni =mysql_query("SELECT * FROM meni WHERE lang_id = '$jezik'"); if(mysql_affected_rows() >0 ){ while($row_meni = mysql_fetch_array($query_meni)){ echo "<li>"; echo "<a href=\"index.php?page="; echo $row_meni['id_meni']; echo "\">"; echo $row_meni['naslov']; echo "</a>"; $query_subject =mysql_query("SELECT * FROM subjects WHERE meni_id = '$row_meni[id_meni]' AND lang_id = '$jezik' ORDER BY pozicija ASC"); if(mysql_affected_rows() == 0){ echo "<li>";} else { while($row_subjects = mysql_fetch_array($query_subject)){ echo "<ul class=\"subnav\">"; echo "<li>"; echo "<a href=\"index.php?subject="; echo $row_subjects['subject_id']; echo "\">"; echo $row_subjects['naslov']; echo "</li>"; echo "</a>"; echo "</ul>"; } } } echo "</li>"; } ?> Link to comment https://forums.phpfreaks.com/topic/231701-making-menu-with-submenu/#findComment-1192465 Share on other sites More sharing options...
yonny Posted March 26, 2011 Share Posted March 26, 2011 try this. i have not tested it but... <?php $query_meni = "SELECT * FROM meni WHERE lang_id = '$jezik'"; $result = mysql_query($query_meni); $count = mysq_affected_rows(); if($count >0 ): print"<ul class=\"main_menu\">"; /// open main menu while($row_meni = mysql_fetch_array($query_meni)): $link = $row_meni['id_meni']; $show = $row_meni['naslov']; print"<li><a href=\"index.php?page=$link\">$show</a>"; $query_subject = "SELECT * FROM subjects WHERE meni_id = '$row_meni[id_meni]' AND lang_id = '$jezik' ORDER BY pozicija ASC"; $result1 = mysql_query($query_subject); $count1 = mysql_affected_rows(); if($count1 == 0): print "</li>"; /// close list else: print"<ul class=\"subnav\">"; /// start sublist before 2nd while loop while($row_subjects = mysql_fetch_array($query_subject)): $link1 = $row_subjects['subject_id']; $show1 = $row_subjects['naslov']; print"<li> <a href=\"index.php?subject=$link1\">$show1</a></li>"; endwhile; print"</ul>"; /// end sublist after while loop endif; print"</li>"; /// close list at endif endwhile; print"</li>"; ///close parent endif; print"</ul>"; /// close main ?> Link to comment https://forums.phpfreaks.com/topic/231701-making-menu-with-submenu/#findComment-1192527 Share on other sites More sharing options...
Bravat Posted March 26, 2011 Author Share Posted March 26, 2011 It is working perfectly . Thank you a lot for helping me . Link to comment https://forums.phpfreaks.com/topic/231701-making-menu-with-submenu/#findComment-1192540 Share on other sites More sharing options...
yonny Posted March 26, 2011 Share Posted March 26, 2011 You are welcome. I'm glad it worked out. Link to comment https://forums.phpfreaks.com/topic/231701-making-menu-with-submenu/#findComment-1192541 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.