Note that there are some explanatory texts on larger screens.

plurals
  1. POHierarchical dropdown menu from array?
    primarykey
    data
    text
    <p>I have the following code which fetches fields from my database and places them into an html form's dropdown menu for Formats. In it's current form, I am fetching from my database 3 times, how the code works is explained in the comments of the code:</p> <pre><code>$getSolos = $wpdb-&gt;get_results($wpdb-&gt;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 &gt; 0) ORDER BY t.parent ")); // This fetches all rows that do not have children or parents. $getParents = $wpdb-&gt;get_results($wpdb-&gt;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 &gt; 0) ")); // This fetches all rows that have children $getChildren = $wpdb-&gt;get_results($wpdb-&gt;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 &gt; 0 ORDER BY t.parent AND p.name ")); //This fetches all rows that ARE children &lt;select name="format"&gt; //start the dropdown &lt;option value="empty"&gt;&lt;/option&gt; //default field is empty &lt;?php foreach ($getSolos as $solo) { //start loop through solos for output echo "&lt;option value='".$solo-&gt;name."'&gt;".$solo-&gt;name."&lt;/option&gt;"; // output solos as options in the dropdown } foreach ($getParents as $parent) { //start loop through parents for output echo "&lt;optgroup label='".$parent-&gt;name."'&gt;"; // Spit out parent as an optgroup foreach ($getChildren as $child) { //Start loop through children for output if ($child-&gt;parent == $parent-&gt;term_id) { // if child's parent value matches the ID of the parent echo "&lt;option value='".$child-&gt;name."'&gt; ".$child-&gt;name."&lt;/option&gt;"; //Spit out the child as an option } } echo "&lt;/optgroup&gt;"; //close the optgroup } ?&gt; &lt;/select&gt; // end the dropdown </code></pre> <p>The output is as follows:</p> <pre><code>Entry Form Twitter Facebook - Entry Form - Page </code></pre> <p>The combined table from the database looks like this:</p> <pre><code>term_id name slug taxonomy parent 1 Entry Form entry-form format 0 2 Page page format 3 3 Facebook facebook format 0 4 Entry Form facebook-entry-form format 3 5 Twitter twitter format 0 </code></pre> <p>There is a problem with this method however.</p> <p>1) It is inefficient to access the database 3 times.</p> <p>2) It is ineffective if a child also has a child. While the children of children do all go into $getChildren, the code will only spit out 1st level children and ignore the rest.</p> <p>For demonstration purposes, if I have 6th row:</p> <pre><code>term_id name slug taxonomy parent 6 Single single format 2 </code></pre> <p>Then the code would do this:</p> <pre><code>Entry Form Twitter Facebook - Entry Form - Page </code></pre> <p>Notice that Single is completely ignored, ALTHOUGH it is contained inside the $getChildren array.</p> <p>So how can this code be made better?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload