英文:
How make dropdown using mysql database, when structure like this?
问题
如何使用数据库SQL创建下拉菜单?当像这样的结构时,我不知道如何使用这个结构来获取下拉菜单。
<?php
$sqlo = "SELECT * from outlet";
$queryo = $dbh->prepare($sqlo);
$queryo->execute();
$results = $queryo->fetchAll(PDO::FETCH_OBJ);
?>
<div class="col-md-1">
<div class="position-relative form-group"><label for="outlet"> Kategori <span style="color:red"> *</span></label>
<select name="outlet" id="outlet" class="form-control">
<?php
foreach ($results as $result) {
echo '<option value="' . htmlentities($result->nameoutlet) . '">' . htmlentities($result->nameoutlet) . '</option>';
}
?>
</select>
</div>
</div>
dbh
在 config.php
中已经声明,在配置中只是连接到本地主机。
<?php
// DB credentials.
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'ereport');
// Establish database connection.
try {
$dbh = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
} catch (PDOException $e) {
exit("Error: " . $e->getMessage());
}
?>
英文:
How to make dropdown using database sql? when like this structure. im confuse how to get dropdown using this structure
<?php
$sqlo = "SELECT * from outlet";
$queryo = $dbh -> prepare($sqlo);
$queryo->execute();
$results=$queryo->fetchAll(PDO::FETCH_OBJ);{
?>
<div class="col-md-1">
<div class="position-relative form-group"><label for="outlet"> Kategori <span style="color:red"> *</span></label>
<select name="outlet" id="outlet" class="form-control">
<option value="<?php htmlentities($result->nameoutlet); ?>"><?php htmlentities($result->nameoutlet); }?></option>
</select>
</div>
</div>
dbh has declare in config.php, in config just connection into loacalhost.
<?php
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','ereport');
// Establish database connection.
try
{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}
?>
答案1
得分: 2
# 代码部分不要翻译,只提供原始代码
我假设你正在尝试使用数据库填充下拉菜单?了解前端语言和后端语言之间的区别很重要。[这可能对你有所帮助。](https://careerfoundry.com/en/blog/web-development/whats-the-difference-between-frontend-and-backend/)
至于你的问题,你需要使用 foreach 循环。
<?php
$options = '';
foreach ($results as $result) {
$safe_value = htmlentities($result->nameoutlet);
$options .= "<option value='$safe_value'>$safe_value</option>";
}
?>
<div class="col-md-1">
<div class="position-relative form-group">
<label for="outlet"> Kategori <span style="color:red"> *</span></label>
<select name="outlet" id="outlet" class="form-control">
<?= $options ?>
</select>
</div>
</div>
英文:
I assume you're trying to use the DB to fill a dropdown menu? It's important to know the difference between a frontend language and a backend language. This might help you.
As for your problem, you'll want to use a foreach loop.
<?php
$options = '';
foreach ($results as $result) {
$safe_value = htmlentities($result->nameoutlet);
$options .= "<option value='$safe_value'>$safe_value</option>";
}
?>
<div class="col-md-1">
<div class="position-relative form-group">
<label for="outlet"> Kategori <span style="color:red"> *</span></label>
<select name="outlet" id="outlet" class="form-control">
<?= $options ?>
</select>
</div>
</div>
答案2
得分: 0
你需要运行 foreach
循环。插入以下代码:
<pre>
<?php foreach ($results as $row) { ?>
<option value="<?php echo htmlentities($row->nameoutlet); ?>"><?php echo htmlentities($row->nameoutlet); ?></option>
<?php } ?>
</pre>
英文:
You have to run foreach
loop. Insert this code:
<pre>
<?php foreach ($results as $row) { ?>
<option value="<?php echo htmlentities($row->nameoutlet); ?>"><?php echo htmlentities($row->nameoutlet); ?></option>
<?php } ?>
</pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论