0

I have the following array from a database query.
I would like to create a treeview where the parent is 'win_name' and the child is 'item_name'. I have tried combinations of foreach and while loops, other answeres in this forum, online tutorials, jquery plugins and jstree over the last couple of days. I cant seem to get it to function

parent = "win_name"
--child = "item_name"

array(857) {
     [0]=>
     object(stdClass)#11 (5) {
       ["item_name"]=>
       string(6) "$rocks"
       ["item_id"]=>
       string(4) "3045"
       ["win_name"]=>
       string(12) "Tequila Mods"
       ["win_id"]=>
       string(1) "3"
       ["sales_mode"]=>
       string(1) "1"
     }
     [1]=>
     object(stdClass)#24 (5) {
       ["item_name"]=>
       string(13) "Queso Fundido"
       ["item_id"]=>
       string(4) "1101"
       ["win_name"]=>
       string(6) "Snacks"
       ["win_id"]=>
       string(2) "29"
       ["sales_mode"]=>
       string(1) "1"
     }
     [2]=>
     object(stdClass)#25 (5) {
       ["item_name"]=>
       string(9) "Texaz Dip"
       ["item_id"]=>
       string(4) "1102"
       ["win_name"]=>
       string(6) "Snacks"
       ["win_id"]=>
       string(2) "29"
       ["sales_mode"]=>
       string(1) "1"
     }
     [3]=>
     object(stdClass)#26 (5) {
       ["item_name"]=>
       string(10) "Summer Sea"
       ["item_id"]=>
       string(4) "1481"
       ["win_name"]=>
       string(6) "Snacks"
       ["win_id"]=>
       string(2) "29"
       ["sales_mode"]=>
       string(1) "1"
     }
  • 2
    When you are learning to program in PHP it is advisable to start with simple things. Work your way up from there. With practice you will learn to do things you now find difficult. – KIKO Software Mar 08 '18 at 23:23
  • [Did you mean recursion](https://www.google.com/search?safe=off&q=recursion)? – ficuscr Mar 08 '18 at 23:25
  • I like the answer here: https://stackoverflow.com/questions/29384548/php-how-to-build-tree-structure-list – ficuscr Mar 08 '18 at 23:28

1 Answers1

0
<?php

class treeView
{
    public $treeview = array();

    // In the construct, we transform your current array to an array that we can use
    function __construct($main)
    {
        $main = (array) $main;

        foreach($main as $view)
        {
            // We add the parent ONLY if we don't already have it
            $this->treeview['parent'][$view['win_name']]['win_name'] = $view['win_name'];
            $this->treeview['parent'][$view['win_name']]['win_id'] = $view['win_id'];
            $this->treeview['parent'][$view['win_name']]['sales_mode'] = $view['sales_mode'];

            // Adding all of the childs
            $this->treeview['child'][$view['win_name']][$view['item_id']]['item_name'] = $view['item_name'];
            $this->treeview['child'][$view['win_name']][$view['item_id']]['item_id'] = $view['item_id'];
        }
    }

    public function buildTree()
    {
        // Looping through each parent and outputting it's content and it's children
        foreach($this->treeview['parent'] as $branch)
        {
            if(isset($this->treeview['child'][$branch['win_name']]))
            {
                echo "<ol>";
                echo "<li>Win Name: {$branch['win_name']}</li>";
                echo "<li>Win ID: {$branch['win_id']}</li>";
                echo "<li>Sales Mode: {$branch['sales_mode']}</li>";
                $this->buildChild($branch['win_name']);         // Our call which builds the children
                echo "</ol>";
            }
            else
            {
                echo "<ol>";
                echo "<li>Win Name: {$branch['win_name']}</li>";
                echo "<li>Win ID: {$branch['win_id']}</li>";
                echo "<li>Sales Mode: {$branch['sales_mode']}</li>";
                echo "</ol>";
            }
        }
    }

    private function buildChild($parent)
    {
        // Looping through each child... technically we could make this recursive and allow children to have children
        foreach($this->treeview['child'][$parent] as $child)
        {
            echo "<li style=\"list-style-type: none;\">";
            echo "<ol>";
            echo "<li>Item Name: {$child['item_name']}</li>";
            echo "<li>Item ID: {$child['item_id']}</li>";
            echo "</ol>";
            echo "</li>";
        }
    }
}

$array = array (
    array (
        'item_name' => "\$rocks",
        'item_id' => "3045",
        'win_name' => "Tequila Mods",
        'win_id' => "3",
        'sales_mode' => "1",
    ),
    array (
        'item_name' => "Queso Fundido",
        'item_id' => "1101",
        'win_name' => "Snacks",
        'win_id' => "29",
        'sales_mode' => "1",
    ),
    array (
        'item_name' => "Texaz Dip",
        'item_id' => "1102",
        'win_name' => "Snacks",
        'win_id' => "29",
        'sales_mode' => "1",
    ),
    array (
        'item_name' => "Summer Sea",
        'item_id' => "1481",
        'win_name' => "Snacks",
        'win_id' => "29",
        'sales_mode' => "1",
    )
);

// Initiating our class
$tree = new treeView($array);

// Building our tree
$tree->buildTree();

?>

That would generate the following list

<ol><li>Win Name: Tequila Mods</li><li>Win ID: 3</li><li>Sales Mode: 1</li><li style="list-style-type: none;"><ol><li>Item Name: $rocks</li><li>Item ID: 3045</li></ol></li></ol><ol><li>Win Name: Snacks</li><li>Win ID: 29</li><li>Sales Mode: 1</li><li style="list-style-type: none;"><ol><li>Item Name: Queso Fundido</li><li>Item ID: 1101</li></ol></li><li style="list-style-type: none;"><ol><li>Item Name: Texaz Dip</li><li>Item ID: 1102</li></ol></li><li style="list-style-type: none;"><ol><li>Item Name: Summer Sea</li><li>Item ID: 1481</li></ol></li></ol>

To be honest, this is very dirty and ugly. I made this code to work with your array, but it's not the best approach. A better approach is to use PDO, and use FETCH_GROUP to group by win_name (the parent) and get the desired array that way.

Won't be bad to study Veerendra's solution 'Thank you ficuscr' and possibly try to implement a similar system with your requirements.

ezw
  • 338
  • 2
  • 12
  • This is fantastic! Yielded the result and learned a lot in the process!. I am currently working on the PDO FETCH_GROUP suggestion as well. – Jeff Petersen Mar 09 '18 at 22:34