how to create <ul> thing for each 10 <li> created? - php

I need to create every 10 records of the new <ul> to store the rest.
The idea is that every 10 <li> he creates another <ul> block that will contain 10 more and so on.
What is returned from the bank in blade:
As I'm trying to do with the following result now:
Can someone help me?

Via array_chunk() you can split your array into chunks of 10 items (or less if it's the last chunk).
#php
$chunks = array_chunk($category->recursiveChildren, 10);
#endphp
#foreach($chunks as $chunk)
<ul>
#foreach($chunk as $child)
<li>{{ $child }}</li>
#endforeach
</ul>
#endforeach

Related

Prevent foreach from loop html tag multiple time in blade laravel

I'm trying to loop data using foreach and I want to skip the html tag after the first item being looped.
I've tried like the code bellow, but the html tag <p> still being looped multiple time. What I want is the <p> tags only looped once
#foreach ($store_icon as $key => $icon)
#if ($key < 1)
<p class="available-at">Also available at:</p>
#endif
#endforeach
Result example:
What I want is like this:
Also Available at
- Product 1
- Product 2
But using the code above it resulted like this:
Also Available at
- Product 1
Also Available at
- Product 2
Thanks
I am not sure why it is not working, It must work and working the same code for as well, you can try --
#foreach ($store_icon as $key => $icon)
#if ($key == 0)
<p class="available-at">Also available at:</p>
#endif
#endforeach
OR you can use like this
#if(!empty($store_icon))
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach
#endif
Whats problem? Just take it out from loop.
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach

Display nested JSON format on Laravel blade

JSON format is saved in MySql column like this:
[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5}]}]
And I did this in Laravel Blade;
#php
$json = $categories->column;
$array= json_decode($json , true);
#endphp
#foreach ($array as $key => $value)
<li>{{ $value["id"] }}</li>
#endphp
And I get a result like this;
1
2
3
But I can't get children results. What should I do for it? Thanks for your help.
Try this code, You should have another loop for children if exists.
#php
$json = $categories->column;
$array= json_decode($json , true);
#endphp
#foreach ($array as $key => $value)
<li>{{ $value["id"] }}</li>
//another loop for children if exists
#if (isset($value["children"]))
#foreach ($value["children"] as $child_key => $child_value)
<li class="childs">{{ $child_value["id"] }}</li>
#endforeach
#endif
#endforeach
Your output will be this.
1
2
3
4
5

PHP - "n" number of children in a list

This question is in relation to: Group by in Laravel 5
Essentially, what I have have is a series of "Groups" and each group can have a as many child groups as they want. So it could look something like this:
Group 1
Group 1 Child
Group 1's child
Group 1 1's Child
Group 2
At the moment, I am only able to display the group, and their children, using:
<ul>
#foreach($contents as $content)
<li>{{$content->title}}</li>
#if($content->children->count() > 0)
<ul>
#foreach($content->children as $childContent)
<li>{{$childContent->title}}</li>
#endforeach
</ul>
#endif
#endforeach
</ul>
If I want to add more layers of children, I have to code this, with another if statement as well as a foreach statement. Obviously this is not practical when a group has n^3,... number of children.
Is there a dynamic, while-loop approach to solving this problem? Any help would be greatly appreciated!!
What you need is a recursive partial - a one that would load itself as long as there are some more group levels to be displayed.
// list_group.blade.php
<li>
{{ $content->title }}
#if($content->children->count() > 0)
<ul>
#foreach($content->children as $childContent)
#include('list_group', array('content' => $childContent))
#endforeach
</ul>
#endif
</li>
//in your template
<ul>
#foreach($contents as $content)
#include('list_group', array('content' => $content))
#endforeach
</ul>

blade templates recursive includes

I have an array of items that represent the file and dir structure of a directory on the server.
The $items array is constructed like this:
Array
(
[folder1] => Array
(
[folder1_1] => Array
(
[0] => filenameX.txt
[1] => filenameY.txt
)
)
[pages] => Array
(
)
[0] => filename.txt
[1] => filename1.txt
)
what we want, is essentially <ul> with <li> for every node.
the resulting HTML should be something like
folder1/
folder1_1/
filenameX.txt
filenameY.txt
pages/
filename_1.txt
filename_2.txt
Now, my question has to do with nested includes with laravel's blade templating engine.
I have a view list.blade.php with the following contents
<div class="listing">
#include('submenu', array('items', $items))
</div>
and I pass it the array like this:
View::make('list')->with('items', $items)
the included template (submenu.blade.php) has the following:
<ul>
#foreach($items as $key=>$value)
#if (is_array($value))
<li>{{$key}}/
#include('submenu', array('items', $value))
</li>
#else
<li>{{$value}}</li>
#endif
#endforeach
</ul>
I #include the same template from within itself but with the new data, in case the $value is an array (directory)
First of all, is this at all possible?
If not, is there another way to achive the desired result?
TIA,
Yes, this is indeed possible.
However, there's an issue in your includes, you have:
#include('submenu', array('items', $value))
It should be:
#include('submenu', array('items' => $value))
It's worth noting also another hidden blade statment, #each. You can use this instead of looping through the array yourself, something like this:
<ul>
#each('item.detail', $items, 'item')
</ul>
Then you create a new blade file named item.detail and pop what you previously had in your loop in that file. It helps to clean up your view from having more and more nested loops.
The data for the item when you are inside your new blade file will be held in the third parameter, in this case $item
Instead of using array, use an eloquent collection. Instead of using #include, use \View::make. It cleans up the code a bit. Here is an example drop down menu for the Foundation 5 framework, using an eloquent model with parent/child relationship:
My model has a parent->child relationship
public function children() {
return $this->hasMany('Category', 'parent_id');
}
I generate my results like so in my controller
$categories = \Category::where('parent_id', '=', '0')->with('children')->get();
Blade template: _partials.dd-menu.blade.php
<ul class="{{$class}}">
#foreach($items as $item)
<?php
$active = $item->id == \Input::get('category') ? 'active' : '';
$hdd = $item->children->count() ? 'has-dropdown' : '';
?>
<li class="{{$hdd}} {{$active}}">
{{$item->name}}
#if ($item->children->count())
{{ View::make('_partials.dd-menu')->withItems($item->children)->withClass('dropdown')}}
#endif
</li>
#endforeach
In your parent blade:
<nav class="top-bar" data-topbar role="navigation">
<ul class="title-area">
<li class="name">
<h1>Categories</h1>
</li>
<!-- Remove the class "menu-icon" to get rid of menu icon. Take out "Menu" to just have icon alone -->
<li class="toggle-topbar menu-icon"><span>Menu</span></li>
</ul>
<section class="top-bar-section">
<!-- Right Nav Section -->
{{ View::make('_partials.dd-menu')->withItems($categories)->withClass('right')}}
</section>
</nav>

Fat Free Framework: how create dynamic menu with submenu

I have this table
id children voce alias pubblicato
11 NULL Chi Siamo chi-siamo 1
12 11 Chi Siamo - Sub chi-siamo-sub 1
So the id 12 is children (submenu) of 11.
With this block i can print the first level of a menu
<repeat group="{{ #result }}" key="{{ #ikey }}" value="{{ #voce }}">
<li>{{ trim(#voce.voce) }}</li>
</repeat>
(result is result of query SQL).
Of course i need to obtain that in my scheme menu will be (pseudo // bootstrap html code)
<li>ID 11 menu
<ul class="dropdown-menu">
<li>Id 12 menu</li>
</ul>
So, in (very!) pseudo code
if (children is not null) {
don't echo </li>
echo <ul class="dropdown">
echo voce where children == parent
}
Thank you very much.
PS If you think that my table need to be edit, don't worry, tell me your best solution!
i did this the following way:
// load page tree
$pages = $model->find();
$pageTree = array();
$pagesByID = array();
foreach($pages as $index => $page)
$pagesByID[$page->_id] = $page->cast();
// reorder to tree
foreach ($pagesByID as &$value)
if ($parent = $value['pid'])
$pagesByID[$parent]['childs'][] = &$value;
else
$pageTree[] = &$value;
$pageTree is now a multi-dimensional array, with child keys, if that page has some childs.

Categories