如何从数据库中输入<select>类别和子类别?

时间:2016-06-28 11:41:46

标签: php mysql sql list while-loop

我真的想做这样的事情http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_optgroup 但我有DB中的主要类别和子类别,我真的不知道如何在一段时间内管理它。我怎样才能做到这一点 ?实际上循环是这样的,但由于我的必需品,我不能再使用它了 &lt; select class =&#34; form-control&#34; ID =&#34;子类别&#34;命名=&#34;子类别&#34;所需&GT; &LT; PHP    $ categories =&#34; SELECT Name,Category FROM SubCategory ORDER BY Category ASC,Name ASC;&#34 ;;    $ query_categories = mysqli_query($ connh,$类别);       而($ rows_categories = mysqli_fetch_array($ query_categories)){ 回声&#39;&LT;选项&GT;&#39; $ rows_categories [&#39;分类&#39;]&#39; /&#39; $ rows_categories [&#39;名称&#39;]。 #39;&LT; /选项&GT;&#39 ;;                                                                     } ?&GT; &LT; /选择&GT; p.s:Category是主要的Category,Name是子类别 我希望将主要类别纳入&lt; optgroup&gt;但由于一个类别包含多个子类别,我不知道如何放置&lt; optgroup&gt;以某种方式显示主要类别只有一次而不是每条记录。

5 个答案:

答案 0 :(得分:1)

在你的情况下,如果你的html上添加了当前主要类别,需要标记,如果我理解你的数据库中的字段,名称和类别有字符串值,你不需要两个查询或两个查询,但只需检查变量,如这样:

<select  class="form-control" id="subcategory" name="subcategory" required>
<?php
$categories="SELECT Name,Category FROM SubCategory ORDER BY Category ASC, Name ASC;";
$query_categories=mysqli_query($connh,$categories);

$currentMainCategory = null;
while($rows_categories=mysqli_fetch_array($query_categories)){
    // check if your current category was diferent of row category
    if($currentMainCategory != $rows_categories['Category']) {
         // check if is not null (for close </optgroup>)
         if(!is_null($currentMainCategory)) {
              echo '</optgroup>';
         }
         // set your new current category for this loop
         $currentMainCategory = $rows_categories['Category'];
         echo '<optgroup label="'. $rows_categories['Category'] .'">';
    }
    // do not forget put value of this category if you needed
    echo'<option>' . $rows_categories['Name'] . '</option>';  
}

// then check again for close last optgroup if is opened
if(!is_null($currentMainCategory)) {
   echo '</optgroup>';
}
?>
</select>

答案 1 :(得分:0)

您完成基本目标并不太远,即在optgroup中显示类别和子类别。

你可以:

  1. 使用两个单独的查询查询MySQL,并使用PHP将它们与SubCategoryCategory表结合起来。

  2. 执行两个foreach循环,每个循环包含optgroupCategorySubCategory个结果。

    1. 使用OUTER JOIN使用一个查询查询MySQL,以针对SubCategoryCategory表(甚至可能是UNION)检索一个结果集。
    2. 执行两个foreach循环,每个循环包含optgroupCategorySubCategory个结果,但仅显示Category个结果,如果它来自Category表,反之亦然。
    3. 以下是一些参考资料:

      MySQL加入:https://www.sitepoint.com/understanding-sql-joins-mysql-database/

      MySQL Union:Combining the results of two separate MySQL queries

      PHP Foreach循环:http://php.net/manual/en/control-structures.foreach.php

      PHP数组合并:http://www.w3schools.com/php/func_array_merge.asp

答案 2 :(得分:0)

{{1}}

你可以这样做

答案 3 :(得分:0)

创建一个包含主要类别的数组,子类别作为子数组。

<select  class="form-control" id="subcategory" name="subcategory" required>
<?php
$option_list = array();

$categories="SELECT Name,Category FROM SubCategory ORDER BY Category ASC, Name ASC;";
$query_categories=mysqli_query($connh,$categories);

while($rows_categories=mysqli_fetch_array($query_categories)){  
    $option_list[$rows_categories['Category']][] = $rows_categories['Name'];
}

// this is what we're aiming for
// $option_list = array("Maincat1"=>array("subcat1","subcat2","subcat3"),"Maincat2"=>array("subcat4","subcat5"));


foreach($option_list as $maincat=>$subcats){
    echo "<optgroup label='".$maincat."'>";
    foreach($subcats as $subcat){
        echo "<option value='$subcat'>$subcat</option>";
    }
    echo "</optgroup>";
}
?>
</select>

答案 4 :(得分:0)

<?php
$conn = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password,$mysql_database) or die("Could not connect database");
$result = mysqli_query($conn,"select distinct Category from SubCategory");
echo '<select  class="form-control" id="subcategory" name="subcategory" required>';
while($row = mysqli_fetch_assoc($result)){  
    echo '<optgroup label="'. $row['Category'] .'">';
     $data =  mysqli_query($conn,"SELECT Name,Category FROM SubCategory where Category = '".$row['Category']."'");
while($value = mysqli_fetch_assoc($data)){  
    echo '<option>'. $value['Name'] .'</option>';
}
 echo '</optgroup>';
}
echo '</select>';
?>