我有以下从我的数据库中获取值的变量。这些变量存储分层列表(格式)的父值和同一分层列表的子值:
$getSolos = $wpdb->get_results($wpdb->prepare("
SELECT * FROM wp_terms p
LEFT OUTER JOIN wp_term_taxonomy t ON p.term_id = t.term_id
WHERE t.taxonomy = 'format'
AND t.parent = 0
AND t.term_id NOT IN (SELECT parent FROM wp_term_taxonomy WHERE taxonomy = 'format' AND parent > 0)
ORDER BY t.parent
"));
$getParents = $wpdb->get_results($wpdb->prepare("
SELECT * FROM wp_terms p
LEFT OUTER JOIN wp_term_taxonomy t ON p.term_id = t.term_id
WHERE t.taxonomy = 'format'
AND t.parent = 0
AND t.term_id IN (SELECT parent FROM wp_term_taxonomy WHERE taxonomy = 'format' AND parent > 0)
"));
虽然以上不一定重要,但以下是我的数据库如下所示:
term_id name slug term_id1 parent
1 Entry Form entry-form 1 0
2 Facebook facebook 2 0
3 Entry Form entry-form-facebook 3 2
4 Twitter twitter 4 0
5 Page page-facebook 5 2
所以从逻辑上讲,我的 OBJECT ARRAYS 包含:
$getParents->parent = 2 // contains only IDs of parent formats
$getSolos->term_id = 1,5 // contains only IDs of formats that have no children
这个想法是用这些格式填充下拉表单菜单,父格式是包含其子格式的 optgroup。到目前为止,我已经实现了以下目标:
<form>
Format: <select name="format">
<option value="empty"></option>
<?php
foreach ($getSolos as $solo) {
echo "<option value='".$solo->name."' ".$selected.">".$indent.$solo->name."</option>";
}
foreach ($getParents as $parent) {
echo "<optgroup label='".$parent->name."'>";
// How do I get the children in here?
echo "</optgroup>";
}
?>
</select>
</form>
上述输出:
Entry Form
Twitter
Facebook // Bold as it is an optgroup header
输出应该是:
Entry Form
Twitter
Facebook // Bold as it is an optgroup header
Entry Form
Page
所以这是我的问题:1)如何让孩子出现在父 optgroup 中?