答案 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中显示类别和子类别。
你可以:
使用两个单独的查询查询MySQL,并使用PHP将它们与SubCategory
和Category
表结合起来。
执行两个foreach
循环,每个循环包含optgroup
个Category
和SubCategory
个结果。
或
OUTER JOIN
使用一个查询查询MySQL,以针对SubCategory
和Category
表(甚至可能是UNION
)检索一个结果集。foreach
循环,每个循环包含optgroup
个Category
和SubCategory
个结果,但仅显示Category
个结果,如果它来自Category
表,反之亦然。以下是一些参考资料:
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
答案 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>';
?>