我已经创建了一个包含来自mysql的optgroups和选项的选择列表。当我尝试以下代码时,虽然optgroup正确,但是出现此错误,但选项显示错误。有人可以帮我解决吗?感谢。
<select id="lr-cat" name="category">
<option value="0"></option>
<?php
$mysqli = new mysqli("localhost", "root", "password", "database");
$results = $mysqli->query('SELECT subcat_id, subcat_name, subcategories.cat_id, cat_name FROM subcategories INNER JOIN categories ON subcategories.cat_id=categories.cat_id');
$categories = array();
while ($row = $results->fetch_assoc()) {
$cat_name = $row['cat_name'];
if (!array_key_exists($cat_name, $categories)){
$categories[$cat_name] = array();}
$categories[$cat_name][] = array($row['subcat_id'], $row['subcat_name']);
}
mysqli_close($con);
?>
<?php foreach ($categories as $cat_name => $subcategories): ?>
<optgroup label="<?php echo htmlentities($cat_name); ?>">
<?php foreach ($subcategories as $subcat_name): ?>
<option value=$subcat_id><?php echo htmlentities($subcat_name); ?></option> <!--line 128-->
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
错误
警告:htmlentities()期望参数1为字符串,给定数组 在第128行