【发布时间】:2018-06-26 17:36:10
【问题描述】:
当我查看包含父类别和子类别的下拉选择菜单时,它在 optgroup 标签内的所有父类别显示不正确,如下所示:
**Cat1 Cat2 Cat3**
- Sub
- Sub
- Sub
我的意图是这样的:
**Cat**
- Sub
**Cat**
- Sub
等等……
任何想法我需要做什么才能正确显示。
这是当前代码:
<select name="DeleteSubCategory" class="form-control" data-style="btn-new">
<optgroup label="
<?php
require('functions/connect.php');
$stmt = $connect->prepare("SELECT CategoryID, CategoryName FROM category ORDER BY CategoryName ASC");
$stmt->execute();
$stmt->bind_result($CategoryID, $CategoryName);
while ($stmt->fetch()){
echo "$CategoryName";
}
$stmt->close();
?>">
<?php
require('functions/connect.php');
$stmt = $connect->prepare("SELECT SubCategoryID, SubCategoryParentID, SubCategoryName FROM subcategory");
$stmt->execute();
$stmt->bind_result($SubCategoryID, $SubCategoryParentID, $SubCategoryName);
while ($stmt->fetch()){
echo "<option value='$SubCategoryID'>$SubCategoryName</option>";
}
$stmt->close();
?>
</optgroup>
</select>
在下面的反馈之后,我加入了我的查询,该查询按要求显示:)
<?php
require('functions/connect.php');
$stmt = $connect->prepare("SELECT
category.CategoryID,
category.CategoryName,
subcategory.SubCategoryID,
subcategory.SubCategoryParentID,
subcategory.SubCategoryName
FROM category
INNER JOIN subcategory ON category.CategoryID = subcategory.SubCategoryParentID");
$stmt->execute();
$stmt->bind_result($CategoryID, $CategoryName, $SubCategoryID, $SubCategoryParentID, $SubCategoryName);
?>
<select name="DeleteSubCategory" class="form-control" data-style="btn-new">
<?php
while ($stmt->fetch()){
?>
<optgroup label="<?php echo $CategoryName; ?>">
<option value= "<?php echo $SubCategoryID; ?>"><?php echo $SubCategoryName; ?></option>
</optgroup>
<?php
}
$stmt->close();
?>
</select>
【问题讨论】:
-
我真的建议尽可能将你的逻辑和 html 解耦:phptherightway.com/#templating
-
非常感谢乔,我已经把你的 cmets 带上了 :)