title: How to display categories, subcategories, sub-subcategories in select list - php/mysql?
body: <p>I want to display categories, subcategories and sub-subcategories in a select list (drop-down list) like the way the WordPress shows in its admin panel. First look at my database table (tb_categories) -</p>
  
  <p><img src="https://i.stack.imgur.com/I4FhQ.png" alt="Database Table"></p>
  
  <p>I want the following output in HTML form -</p>
  
  <p><img src="https://i.stack.imgur.com/VtvmL.png" alt="Output"></p>
  
  <p>The two items "None" and "Uncategorized" are hardcoded in the code. I am wondering how to display categories and their subcategories in hierarchical order using select list options.</p>
  
  <p>I am trying with the following sql query in which I am using self join. Here it is -</p>
  
  <pre><code>SELECT
      `cat`.`category_name` AS 'category name',
      `cat2`.`category_name` AS 'parent category'
  FROM
      `tb_categories` AS `cat`
  LEFT JOIN `tb_categories` AS `cat2` ON `cat`.`category_parent` = `cat2`.`category_id`
  ORDER BY
      'parent category'
  </code></pre>
  
  <p>And the output it is giving is -</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [0] =&gt; My Parent Category
              [category name] =&gt; My Parent Category
              [1] =&gt; 
              [parent category] =&gt; 
          )
  
      [1] =&gt; Array
          (
              [0] =&gt; Parent Category 2
              [category name] =&gt; Parent Category 2
              [1] =&gt; 
              [parent category] =&gt; 
          )
  
      [2] =&gt; Array
          (
              [0] =&gt; Parent Category 3
              [category name] =&gt; Parent Category 3
              [1] =&gt; 
              [parent category] =&gt; 
          )
  
      [3] =&gt; Array
          (
              [0] =&gt; My Child Category
              [category name] =&gt; My Child Category
              [1] =&gt; My Parent Category
              [parent category] =&gt; My Parent Category
          )
  
      [4] =&gt; Array
          (
              [0] =&gt; Sports
              [category name] =&gt; Sports
              [1] =&gt; 
              [parent category] =&gt; 
          )
  
      [5] =&gt; Array
          (
              [0] =&gt; Cricket is best
              [category name] =&gt; Cricket is best
              [1] =&gt; Sports
              [parent category] =&gt; Sports
          )
  
      [6] =&gt; Array
          (
              [0] =&gt; AJAX
              [category name] =&gt; AJAX
              [1] =&gt; 
              [parent category] =&gt; 
          )
  
      [7] =&gt; Array
          (
              [0] =&gt; hockey is best
              [category name] =&gt; hockey is best
              [1] =&gt; Sports
              [parent category] =&gt; Sports
          )
  
  )
  </code></pre>
  
  <p>I don't know and even not sure how can I display above data in that select list. How we do that? How can we do it using joins? If we use joins then do we need some array to store and sort the results? And also how do we do it using several queries in a loop? Which method will be best?</p>