php - MySQL/PHP/HTML 如何在分层下拉菜单中插入子项?

标签 php html mysql drop-down-menu optgroup

我有以下从我的数据库中获取值的变量。这些变量存储分层列表(格式)的父值和同一分层列表的子值:

$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

想法是用这些格式填充下拉菜单,父格式是包含其子格式的 optgroups。到目前为止,我已经取得了以下成就:

<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) 如何让 child 出现在父 optgroup 中?

最佳答案

您确实可以在 PHP 中构造一个合适的 NOT IN 条件并将其传递给 MySQL,例如:

$parents_sql = implode(',', array_map(intval, $getParents->parent));

$getSolos = $wpdb->get_results($wpdb->prepare("
    SELECT term_id FROM wp_term_taxonomy            
    WHERE taxonomy = 'format'           
    AND parent = 0 AND term_id NOT IN ($parents_sql)
    "));

(注意:intval 用于确保此代码不会导致 SQL 注入(inject)。在您的情况下,这应该不是问题,但可以永远不要太确定,无论如何养成一个好习惯。如果您使用字符串键执行此操作,则可以将其替换为适合您的数据库的字符串引用函数。)

但是,如果有很多父记录,使用子查询(demo on SQLFiddle)可能更有效:

SELECT term_id FROM wp_term_taxonomy AS t1            
WHERE taxonomy = 'format'           
AND parent = 0
AND term_id NOT IN (
    SELECT parent FROM wp_term_taxonomy         
    WHERE taxonomy = 'format'           
    AND parent > 0)

LEFT JOIN ( demo on SQLFiddle ):

SELECT t1.term_id
FROM wp_term_taxonomy AS t1            
  LEFT JOIN wp_term_taxonomy AS t2
    ON t1.term_id = t2.parent
WHERE t1.taxonomy = 'format'           
  AND t1.parent = 0
  AND t2.term_id IS NULL

关于php - MySQL/PHP/HTML 如何在分层下拉菜单中插入子项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14130947/

相关文章:

PHP 试图计算段落之间的空格

html - 如何在 HTML 的 <dl> 中添加换行符 <br/>

javascript - 将选择元素放入具有 z-index 位置的 div 到 Highcharts 容器

sql - 在 MySQL 中用一个公共(public)键连接两个表

php - 即使代码正确,也无法选择数据库表

php - 如何将列的值设置为使用同一表的列之一进行 SQL 查询的结果

php - 如何在wordpress的侧边栏下方添加图片

php - Doctrine2,Symfony2。甲骨文错误 :Class has no field or association named user_id

php - 如何在MYSQL数据库中存储PHP Session

html - 为什么单击链接时内框会移动?