首页 > 解决方案 > 如何从数据库中打开多个下拉列表。如何为每个获取的下拉按钮分配唯一 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>       

标签: php

解决方案


怎么样(纯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>";

推荐阅读