Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Proposed Solution</h2> <p>This following example gives a little more than you ask for, but it's a really nice way of doing it and still demonstrates where the information comes from at each stage.</p> <p>It uses the following table structure:</p> <pre><code>+--------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | parent | int(10) unsigned | NO | | NULL | | | name | varchar(45) | NO | | NULL | | +--------+------------------+------+-----+---------+----------------+ </code></pre> <p>Here it is:</p> <pre><code>&lt;?php // Connect to the database mysql_connect('localhost', 'root', ''); mysql_select_db('test'); echo '&lt;pre&gt;'; $categories = Category::getTopCategories(); print_r($categories); echo '&lt;/pre&gt;'; class Category { /** * The information stored in the database for each category */ public $id; public $parent; public $name; // The child categories public $children; public function __construct() { // Get the child categories when we get this category $this-&gt;getChildCategories(); } /** * Get the child categories * @return array */ public function getChildCategories() { if ($this-&gt;children) { return $this-&gt;children; } return $this-&gt;children = self::getCategories("parent = {$this-&gt;id}"); } //////////////////////////////////////////////////////////////////////////// /** * The top-level categories (i.e. no parent) * @return array */ public static function getTopCategories() { return self::getCategories('parent = 0'); } /** * Get categories from the database. * @param string $where Conditions for the returned rows to meet * @return array */ public static function getCategories($where = '') { if ($where) $where = " WHERE $where"; $result = mysql_query("SELECT * FROM categories$where"); $categories = array(); while ($category = mysql_fetch_object($result, 'Category')) $categories[] = $category; mysql_free_result($result); return $categories; } } </code></pre> <h2>Test Case</h2> <p>In my database I have the following rows:</p> <pre><code>+----+--------+-----------------+ | id | parent | name | +----+--------+-----------------+ | 1 | 0 | First Top | | 2 | 0 | Second Top | | 3 | 0 | Third Top | | 4 | 1 | First Child | | 5 | 1 | Second Child | | 6 | 2 | Third Child | | 7 | 2 | Fourth Child | | 8 | 4 | First Subchild | | 9 | 4 | Second Subchild | +----+--------+-----------------+ </code></pre> <p>And thus the script outputs the following (lengthy) information:</p> <pre><code>Array ( [0] =&gt; Category Object ( [id] =&gt; 1 [parent] =&gt; 0 [name] =&gt; First Top [children] =&gt; Array ( [0] =&gt; Category Object ( [id] =&gt; 4 [parent] =&gt; 1 [name] =&gt; First Child [children] =&gt; Array ( [0] =&gt; Category Object ( [id] =&gt; 8 [parent] =&gt; 4 [name] =&gt; First Subchild [children] =&gt; Array ( ) ) [1] =&gt; Category Object ( [id] =&gt; 9 [parent] =&gt; 4 [name] =&gt; Second Subchild [children] =&gt; Array ( ) ) ) ) [1] =&gt; Category Object ( [id] =&gt; 5 [parent] =&gt; 1 [name] =&gt; Second Child [children] =&gt; Array ( ) ) ) ) [1] =&gt; Category Object ( [id] =&gt; 2 [parent] =&gt; 0 [name] =&gt; Second Top [children] =&gt; Array ( [0] =&gt; Category Object ( [id] =&gt; 6 [parent] =&gt; 2 [name] =&gt; Third Child [children] =&gt; Array ( ) ) [1] =&gt; Category Object ( [id] =&gt; 7 [parent] =&gt; 2 [name] =&gt; Fourth Child [children] =&gt; Array ( ) ) ) ) [2] =&gt; Category Object ( [id] =&gt; 3 [parent] =&gt; 0 [name] =&gt; Third Top [children] =&gt; Array ( ) ) ) </code></pre> <h2>Example Usage</h2> <p>I'd suggest creating some kind of recursive function if you're going to create menus from the data:</p> <pre><code>function outputCategories($categories, $startingLevel = 0) { $indent = str_repeat(" ", $startingLevel); foreach ($categories as $category) { echo "$indent{$category-&gt;name}\n"; if (count($category-&gt;children) &gt; 0) outputCategories($category-&gt;children, $startingLevel+1); } } $categories = Category::getTopCategories(); outputCategories($categories); </code></pre> <p>which would output the following:</p> <pre><code>First Top First Child First Subchild Second Subchild Second Child Second Top Third Child Fourth Child Third Top </code></pre> <p><em>Enjoy</em></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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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