php - 如何从数据库中打开多个下拉列表。如何为每个获取的下拉按钮分配唯一 ID
问题描述
我需要在 while 循环中打开多个从数据库中获取的下拉列表。如何为每个提取的下拉按钮分配唯一 ID。
这是Javascript代码。
<script>
function myFunction() {
document.getElementById("myDropdown1").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
And this is HTML
<table><tr><td>
<a href="userprofile.php?id=<?php echo $r['id']; ?>"></td>
<div class="dropdown">
<td><button onclick="myFunction()" class="dropbtn"><img
src="images/options.png" width="12" height="12" align="right">
</button>
</td>
<i class="fa fa-caret-down"></i>
<td><div id="myDropdown1" class="dropdown-content">
<a
href="#onclick="document.getElementById('debate').style.display='inline-
block'"><table><tr><td><img src="images/debates.png" width="20"
height="20"></td><td>Debate This!</td></tr></table></a>
<a href="#"><table><tr><td><img src="images/flag.png" width="20"
height="20"></td><td>Report.</td></tr></table></a>
</div> </div></td></tr></table>
解决方案
怎么样(纯html没有js):
$data = $db->query("SELECT * FROM $table WHERE id='$someid');//or whatever query the point is that $data is array fetch from database
print "<select name='dropdown'>";
for($i=0; $i <=count($data)-1; $i++) {
print "<option value='$i'>$data[$i]['some_field']</option>"; // you can change $i with $data[$i]['primary_key_field']
}
print "</select>";
推荐阅读
- rust - 将非 Future Rust 函数变成 Future 函数?
- python - 用于静态方法参数的 Python 集类
- c# - WPF 在按钮模板中更改图像
- http - 如何从 webhook 重定向到 URL
- postgresql - 如何优化 Postgresql 中的逆向模式匹配?
- python - 无法使用 yFinance JSON 值创建 DataFrame
- c# - 无法在 MVC 中动态更改部分视图
- sql - 请找到这个问题的解决方案。(在 PL SQL 中)--------
- visual-studio-code - VSCode 扩展 - 获取编辑器类型
- c - 如何将结构存储在链表中