如何使用MySQL数据库创建下拉菜单,当数据库结构如下时?

huangapple go评论106阅读模式
英文:

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>

dbhconfig.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

&lt;?php
$sqlo = &quot;SELECT * from outlet&quot;;
$queryo = $dbh -&gt; prepare($sqlo);
$queryo-&gt;execute();
$results=$queryo-&gt;fetchAll(PDO::FETCH_OBJ);{
?&gt;
&lt;div class=&quot;col-md-1&quot;&gt;
    &lt;div class=&quot;position-relative form-group&quot;&gt;&lt;label for=&quot;outlet&quot;&gt; Kategori &lt;span style=&quot;color:red&quot;&gt; *&lt;/span&gt;&lt;/label&gt;
        &lt;select name=&quot;outlet&quot; id=&quot;outlet&quot; class=&quot;form-control&quot;&gt;

            &lt;option value=&quot;&lt;?php htmlentities($result-&gt;nameoutlet); ?&gt;&quot;&gt;&lt;?php htmlentities($result-&gt;nameoutlet); }?&gt;&lt;/option&gt;
        &lt;/select&gt;
    &lt;/div&gt;
&lt;/div&gt;

dbh has declare in config.php, in config just connection into loacalhost.

&lt;?php
// DB credentials.
define(&#39;DB_HOST&#39;,&#39;localhost&#39;);
define(&#39;DB_USER&#39;,&#39;root&#39;);
define(&#39;DB_PASS&#39;,&#39;&#39;);
define(&#39;DB_NAME&#39;,&#39;ereport&#39;);
// Establish database connection.
try
{
    $dbh = new PDO(&quot;mysql:host=&quot;.DB_HOST.&quot;;dbname=&quot;.DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND =&gt; &quot;SET NAMES &#39;utf8&#39;&quot;));
}
catch (PDOException $e)
{
    exit(&quot;Error: &quot; . $e-&gt;getMessage());
}
?&gt;

答案1

得分: 2

# 代码部分不要翻译,只提供原始代码
我假设你正在尝试使用数据库填充下拉菜单了解前端语言和后端语言之间的区别很重要[这可能对你有所帮助](https://careerfoundry.com/en/blog/web-development/whats-the-difference-between-frontend-and-backend/)

至于你的问题你需要使用 foreach 循环

        &lt;?php
        
        $options = &#39;&#39;;
        foreach ($results as $result) {
            $safe_value = htmlentities($result-&gt;nameoutlet);
            $options .= &quot;&lt;option value=&#39;$safe_value&#39;&gt;$safe_value&lt;/option&gt;&quot;;
        }

        ?&gt;

        &lt;div class=&quot;col-md-1&quot;&gt;
            &lt;div class=&quot;position-relative form-group&quot;&gt;
                &lt;label for=&quot;outlet&quot;&gt; Kategori &lt;span style=&quot;color:red&quot;&gt; *&lt;/span&gt;&lt;/label&gt;
                &lt;select name=&quot;outlet&quot; id=&quot;outlet&quot; class=&quot;form-control&quot;&gt;
                    &lt;?= $options ?&gt;
                &lt;/select&gt;
            &lt;/div&gt;
        &lt;/div&gt;
英文:

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.

    &lt;?php
    
    $options = &#39;&#39;;
    foreach ($results as $result) {
        $safe_value = htmlentities($result-&gt;nameoutlet);
        $options .= &quot;&lt;option value=&#39;$safe_value&#39;&gt;$safe_value&lt;/option&gt;&quot;;
    }

    ?&gt;

    &lt;div class=&quot;col-md-1&quot;&gt;
        &lt;div class=&quot;position-relative form-group&quot;&gt;
            &lt;label for=&quot;outlet&quot;&gt; Kategori &lt;span style=&quot;color:red&quot;&gt; *&lt;/span&gt;&lt;/label&gt;
            &lt;select name=&quot;outlet&quot; id=&quot;outlet&quot; class=&quot;form-control&quot;&gt;
                &lt;?= $options ?&gt;
            &lt;/select&gt;
        &lt;/div&gt;
    &lt;/div&gt;

答案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:

&lt;pre&gt;
    &lt;?php foreach ($results as $row) { ?&gt;
      &lt;option value=&quot;&lt;?php echo htmlentities($row-&gt;nameoutlet); ?&gt;&quot;&gt;&lt;?php echo htmlentities($row-&gt;nameoutlet); ?&gt;&lt;/option&gt;
    &lt;?php } ?&gt;
&lt;/pre&gt;

huangapple
  • 本文由 发表于 2020年1月3日 17:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定