简体   繁体   English

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

[英]MySQL/PHP/HTML How to insert children in hierarchical dropdown form menu?

I have the following variables that fetch values from my database. 我有以下变量可从数据库中获取值。 These variables are storing Parent values of a hierarchical list (formats) and children values of the same hierarchical list: 这些变量存储层次结构列表(格式)的父级值和同一层次结构列表的子级值:

$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)
        "));

Although the above isn't necessarily important, the following is as my database looks like this: 尽管上面的内容不一定重要,但以下是我的数据库如下所示:

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

So logically, my OBJECT ARRAYS contain: 从逻辑上讲,我的对象数组包含:

$getParents->parent = 2 // contains only IDs of parent formats
$getSolos->term_id = 1,5 // contains only IDs of formats that have no children

The idea is to populate a dropdown form menu with these formats, with parent formats being optgroups containing their children. 想法是使用这些格式填充下拉菜单,其中父格式是包含其子级的optgroup。 SO far I've achieved the following: 到目前为止,我已经实现了以下目标:

<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>

The above outputs: 以上输出:

Entry Form
Twitter
Facebook // Bold as it is an optgroup header

The output should be: 输出应为:

Entry Form
Twitter
Facebook // Bold as it is an optgroup header
    Entry Form
    Page

So here is my question: 1) How do I get the children to appear inside the parent optgroup? 所以这是我的问题:1)如何让孩子出现在父母的optgroup中?

You could indeed construct a suitable NOT IN condition in PHP and pass it to MySQL, for example like this: 您确实可以在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)
    "));

(Note: The intval is there to make sure this code cannot lead to SQL injection. In your case, that shouldn't be an issue, but one can never be too sure, and in any case it's a good habit to get into. If you were doing this with string keys, you could replace it with the appropriate string quoting function for your database.) (注意: intval可以确保此代码不会导致SQL注入。在您的情况下,这应该不成问题,但是永远不能太确定,无论如何都应养成良好的习惯。如果使用字符串键执行此操作,则可以使用适合数据库的字符串引用功能将其替换。)

However, if there are a lot of parent records, it may be more efficient to use a subquery ( demo on SQLFiddle ): 但是,如果有很多父记录,则使用子查询( 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)

or a LEFT JOIN ( demo on SQLFiddle ): LEFT JOINSQLFiddle上的演示 ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM