Alexander Kim
Alexander Kim

Reputation: 18392

How to add additional values in to array

I am trying to make nested categories in Laravel. I have created following models: Category.php, ItemsHelper.php (this one for displaying nested categories).

ItemsHelper.php:

  class ItemsHelper {

    private $items;

    public function __construct($items) {
        $this->items = $items;
    }

    public function htmlList() {
        return $this->htmlFromArray($this->itemArray());
    }

    private function itemArray() {
        $result = [];
        foreach($this->items as $item) {
            if ($item->parent_id == 0) {
                $result[$item->title] = $this->itemWithChildren($item);
            }
        }
        return $result;
    }

    private function childrenOf($item) {
        $result = [];
        foreach($this->items as $i) {
            if ($i->parent_id == $item->id) {
              $result[] = $i;
            }
        }
        return $result;
    }

    private function itemWithChildren($item) {
        $result = [];
        $children = $this->childrenOf($item);
        foreach ($children as $child) {
            $result[$child->title] = $this->itemWithChildren($child);
        }
        return $result;
    }

    private function htmlFromArray($array) {
        $html = '';
        foreach($array as $k => $v) {
            $html .= "<ul>";
            $html .= "<li>" . $k . "</li>";
            if(count($v) > 0) {
                $html .= $this->htmlFromArray($v);
            }
            $html .= "</ul>";
        }
        return $html;
    }
}

Then i am printing nested list of data in my index.blade.php:

{{ $itemsHelper->htmlList() }}

However this prints only plain titles and i need to convert them in to a links to my category, e.g. htmlFromArray() method:

$html .= "<li><a href="/category/$k->id">" . $k . "</a></li>";

method itemArray():

$result[$item->title] = $this->itemWithChildren($item);

How can i add to this array id of each item?

Upvotes: 0

Views: 55

Answers (1)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

You should probably change methods itemArray, itemWithChildren and htmlFromArray this way:

private function itemArray()
{
    $result = [];
    foreach ($this->items as $item) {
        if ($item->parent_id == 0) {
            $record['title'] = $item->title;
            $record['id'] = $item->id;
            $record['children'] = $this->itemWithChildren($item);
            $result[] = $record;
        }
    }
    return $result;
}

private function itemWithChildren($item)
{
    $result = [];
    $children = $this->childrenOf($item);
    foreach ($children as $child) {
        $record['title'] = $child->title;
        $record['id'] = $child->id;
        $record['children'] = $this->itemWithChildren($child);
        $result[] = $record;
    }
    return $result;
}

private function htmlFromArray($array)
{
    $html = '';
    foreach ($array as $item) {
        $html .= "<ul>";
        $html
            .=
            '<li><a href="/category/' . $item['id'] . '">' . $item['title']
            . "</a></li>";
        if (count($item['children']) > 0) {
            $html .= $this->htmlFromArray($item['children']);
        }
        $html .= "</ul>";
    }
    return $html;
}

Now you can store in your arrays id, title and list of element children so you can access iun htmlFromArray all those elements attributes.

Upvotes: 1

Related Questions