[英]How to create dropdown menu with php?
This title may confuse someone, but the my goal is to make a dropdown menu with echoing it within the PHP function. 这个标题可能会使人感到困惑,但是我的目标是创建一个下拉菜单并在PHP函数中回显该菜单。 To understand my problem a little bit...
为了稍微了解我的问题...
My mainpages, the names are displayed at the top menu: 我的主页名称显示在顶部菜单中:
TABLE INDEX_PAGES 表格INDEX_PAGES
id, name, href
----------------------------
1 login ?page=login
2 admin ?page=admin
3 projects ?page=projects
4 home ?page=home
The table to know what mainpages can be displayed in the top menu 知道哪些主页可以显示在顶部菜单中的表格
TABLE TOP_MENU 表格TOP_MENU
id, index_page
------------------
1 1
2 2
3 3
4 4
Im using function to echo them into my topmenu. 我正在使用函数将它们回显到我的顶层菜单中。
Structure: 结构体:
<header>
<h1 class="header">Godhaze</h1>
<nav id="nav-vasak">
<ul>
<?php echo get_topnav(); ?>
</ul>
</nav>
</header>
Function: 功能:
function get_topnav(){
global $db;
$sql="SELECT i.name,i.href from index_pages i join top_menu t on t.index_page=i.id ";
foreach($db->query($sql) as $row) {
echo "<li><a href='".get_domain().$row['href']."'>".$row['name']."</a>";
//dropdownmenu
echo "</li>";
}
}
Now I have topmenu names and its all working fine, but I want to display my second pages in dropmenu view. 现在,我有了topmenu名称,并且一切正常,但是我想在dropmenu视图中显示第二页。
Like: 喜欢:
<header>
<h1 class="header">Godhaze</h1>
<nav id="nav-vasak">
<ul>
<li><a>Login</a> //mainpage
<li><a>Action_log</a></li> //secondpage
</li>
</ul>
</nav>
</header>
TABLE SECOND_PAGES 表格SECOND_PAGES
id, name, index_page_id, href
------------------------------------------------------------------
1 error_log 2 ?page=admin&secondpage=error_log
2 user_log 2 ?page=admin&secondpage=user_log
3 action_log 1 ?page=login&secondpage=action_log
4 test_log 3 ?page=projects&secondpage=test_log
The table to know what secondpages can be displayed in the dropdown top menu, index_page is the foreign key to index_pages and second_page is foreign key to second_pages 该表知道可以在下拉菜单中显示哪些第二页,index_page是index_pages的外键,second_page是second_pages的外键
TABLE TOP_MENU_PAGES 表格TOP_MENU_PAGES
id, index_page, second_page
-------------------------------
1 1 3
2 2 1
So now to my question is.. how can I make this function so I can display the second pages at dropdown menu? 所以现在我的问题是..我怎样才能使此功能,以便我可以在下拉菜单中显示第二页?
Try this modified function 试试这个修改的功能
function get_second_pages(){
global $db;
$sql="SELECT i.name,i.href,s.name as name2,s.href as href2,
from index_pages i
join top_menu t on t.index_page=i.id
join SECOND_PAGES s on t.second_page=s.id ";
foreach($db->query($sql) as $row) {
echo "<li><a href='".get_domain().$row['href']."'>".$row['name']."</a>";
echo "<li><a href='".get_domain().$row['href2']."'>".$row['name2']."</a>";
echo "</li>";
}
}
I thing it will help you. 我认为这会对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.