php - 从 MS SQL 数据库填充 Bootstrap 下拉列表
问题描述
我整个早上都在绞尽脑汁,通过谷歌搜索了很多页面。
我找不到用来自 MS SQL 服务器的数据填充引导按钮下拉框的方法。
有很多方法可以从 MySQL 中进行纯 HTML 下拉菜单。
这是按钮的代码:
<div class="btn-group">
<button type="button" class="btn btn-info btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Family</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Option 1</a>
<a class="dropdown-item" href="#">Option 2</a>
<a class="dropdown-item" href="#">Option 3</a>
</div>
</div>
非常感谢您的帮助。
解决方案
您不能使用 HTML 直接从 MySQL 填充下拉列表,因为您需要使用 PHP,因此如果您想从 MySQL 数据库中填充下拉项目,您可以访问您的数据库服务器端。
因此,您需要将 HTML 文件转换为 PHP 文件(index.html -> index.php),并且在其中您需要有类似的内容:
索引.php
<div class="btn-group">
<button type="button" class="btn btn-info btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Family</button>
<div class="dropdown-menu">
<?php
// server credentials
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Replace with the desired SQL
$sql = "SELECT options FROM MyOptions";
// get the results
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($option = $result->fetch_assoc())
{
// change "name" to the column fo your DB row.
echo "<a class='dropdown-item' href='#'>".$option["name"]."</a> ";
// if you also store a link to your DB, for ex in a column link then use
// echo "<a class='dropdown-item' href='".option["link"]."'>".$option["name"]."</a> ";
}
}
$conn->close();
?>
</div>
</div>
推荐阅读
- r - R:函数的形式在哪里存储在内存中?
- java - java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver(包括 JAR)
- pattern-matching - 匹配 ocaml 中的复杂数据类型
- raku - 在 Perl 6 中,如何使用 NativeCall 接口将原始字节转换为浮点?
- python - Relu函数,返回0和大数
- ios - IOS/Xcode:抑制 NSLog 语句以在 2018 年发布
- php - 两行之间的PHP内容正则表达式
- kentico - Paypal 与 Paypal IPN 的集成
- java - 在java中构造嵌套的Tuple2
- php - JQuery 和 PHP - 插入多行