Turn database result into array

前端 未结 5 961
忘掉有多难
忘掉有多难 2020-11-28 09:19

I have just made the update/add/delete part for the \"Closure table\" way of organizing query hierarchical data that are shown on page 70 in this slideshare: http://www.slid

5条回答
  •  温柔的废话
    2020-11-28 09:32

    Proposed Solution

    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.

    It uses the following table structure:

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

    Here it is:

    ';
    
        $categories = Category::getTopCategories();
        print_r($categories);
    
        echo '
    '; 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->getChildCategories(); } /** * Get the child categories * @return array */ public function getChildCategories() { if ($this->children) { return $this->children; } return $this->children = self::getCategories("parent = {$this->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; } }

    Test Case

    In my database I have the following rows:

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

    And thus the script outputs the following (lengthy) information:

    Array
    (
        [0] => Category Object
            (
                [id] => 1
                [parent] => 0
                [name] => First Top
                [children] => Array
                    (
                        [0] => Category Object
                            (
                                [id] => 4
                                [parent] => 1
                                [name] => First Child
                                [children] => Array
                                    (
                                        [0] => Category Object
                                            (
                                                [id] => 8
                                                [parent] => 4
                                                [name] => First Subchild
                                                [children] => Array
                                                    (
                                                    )
    
                                            )
    
                                        [1] => Category Object
                                            (
                                                [id] => 9
                                                [parent] => 4
                                                [name] => Second Subchild
                                                [children] => Array
                                                    (
                                                    )
    
                                            )
    
                                    )
    
                            )
    
                        [1] => Category Object
                            (
                                [id] => 5
                                [parent] => 1
                                [name] => Second Child
                                [children] => Array
                                    (
                                    )
    
                            )
    
                    )
    
            )
    
        [1] => Category Object
            (
                [id] => 2
                [parent] => 0
                [name] => Second Top
                [children] => Array
                    (
                        [0] => Category Object
                            (
                                [id] => 6
                                [parent] => 2
                                [name] => Third Child
                                [children] => Array
                                    (
                                    )
    
                            )
    
                        [1] => Category Object
                            (
                                [id] => 7
                                [parent] => 2
                                [name] => Fourth Child
                                [children] => Array
                                    (
                                    )
    
                            )
    
                    )
    
            )
    
        [2] => Category Object
            (
                [id] => 3
                [parent] => 0
                [name] => Third Top
                [children] => Array
                    (
                    )
    
            )
    
    )
    

    Example Usage

    I'd suggest creating some kind of recursive function if you're going to create menus from the data:

    function outputCategories($categories, $startingLevel = 0)
    {
        $indent = str_repeat("    ", $startingLevel);
    
        foreach ($categories as $category)
        {
            echo "$indent{$category->name}\n";
            if (count($category->children) > 0)
                outputCategories($category->children, $startingLevel+1);
        }
    }
    
    $categories = Category::getTopCategories();
    outputCategories($categories);
    

    which would output the following:

    First Top
        First Child
            First Subchild
            Second Subchild
        Second Child
    Second Top
        Third Child
        Fourth Child
    Third Top
    

    Enjoy

提交回复
热议问题