In my blade.php file, I'm trying to merge two foreach loop to display data in the table. I had tried use something like nested loop but it doesn't work.
#foreach($cat->products as $idx => $product)
<tr ng-init="item.items[{{$cat->id}}][{{$idx}}] = {};">
#foreach ($queryInv as $querInvs)
<td>{{$product->sku}}</td>
<td>{{$product->name}}</td>
<td class="text-right">{{{$querInvs->total or 0}}}</td>
</tr>
#endforeach
#endforeach
I just need to insert the total data into the table. Now the total is displayed correctly but it will duplicate the sku and name in the table.
Define an array which we will use as a "flag" and on each iteration check if the current product SKU is NOT in our "flag" array. If it isn't then we display and we add the current product SKU to the list of processed SKUs and continue as normal.
#php $skus = [] #endphp
#foreach($cat->products as $idx => $product)
#if (!in_array($product->sku, $skus))
<tr ng-init="item.items[{{$cat->id}}][{{$idx}}] = {};">
#foreach ($queryInv as $querInvs)
<td>{{$product->sku}}</td>
<td>{{$product->name}}</td>
<td class="text-right">{{{$querInvs->total or 0}}}</td>
#endforeach
</tr>
#php array_push($skus, $product->sku); #endphp
#endif
#endforeach
Note: You are using Laravel 4, the current version of Laravel is 5.7, I would absolutely update and use the latest version.
Reading Material
in_array
array_push
Related
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
Fetching the array data and using explode i have separated the content. Now, i want to print those data in the list. But products are printing in the line. I tried using "break tag" And "list tag" Every possible ways.
Here is the code
<tbody id="test">
#foreach ($data['samplingData'] as $key => $samplingData)
<tr> <td>{{$samplingData->doctor_name}}</td>
<td>{{$samplingData->date}}</td>
#foreach(explode(',', $samplingData->products) as $product)
<td> {{$product}} </td>
#endforeach
<td>{{$samplingData->quantity}}</td> </tr>
#endforeach
</tbody>
Data is printing in this way
Name date products quantity
a 12-12-12 a a a a 1 1 1 1
I want to print like this
Name date products quantity
a 12-12-12 a 1
a 1
a 1
I tried adding tr inside td and as well as list inside the td.
Please help me in formatting the data.
Basically you were creating <td> for-each $product.
<td>#foreach(explode(',', $samplingData->products) as $product) {{$product}} <BR/> #endforeach</td>
I have a table and I want to enumerate each element.
Something like this
#foreach($elements as $element)
<tr>
<td>{{ (isset($a))?$a++:($a = 1) }}</td>
<td>...
</tr>
#endforeach
I expect the table to begin with 1 and then count on, however, the first two columns are always 1. I have already solved this problem by giving the template an $a = 0; on the controller but I want to know why my first solution doesn't work and if there is a workaround
Laravel Blade has a very handy variable for loops, called the $loop variable. This lets you gather information about the current element of the list, including the index and iteration count.
So you can do just this:
#foreach($elements as $element)
<tr>
<td>{{ $loop->iteration }}</td>
<td>...
</tr>
#endforeach
I am using the paginator of Laravel to display 100 results per page. In the front of each row I'm currently displaying the ID of the database field. However, I want to display the count.
I've found some threads on stackoverflow solving this issue for Laravel 4
Where they say, you should solve it like this
<?php $count = $players->getFrom(); ?>
#foreach ($players as $player)
<tr>
<td>{{ $count++ }}. </td>
</tr>
#endforeach
or like this
<?php $count = $players->getFrom() + 1; ?>
#foreach ($players as $player)
...
The problem here, is that Laravel 5.1 doesn't have the getForm method anymore. The upgrade guide says to replace getFrom by firstItem, which I did. Unfortunetely, I'm not getting the correct numbers.
I tried it like this
<?php $count = $pages->firstItem() + 1 ?>
#foreach ($pages as $page)
#include('pages.singlepagebasic')
#endforeach
and like this
{{ $count = $pages->firstItem() }}
#foreach ($pages as $page)
#include('pages.singlepagebasic')
#endforeach
//pages.singlebasic.blade.php
{{ $count ++ }}
but my site always displays only the number "2" instead of counting up. How do I achieve that?
Just use this to get pages count
$players->lastPage()
or this to get items count
$players->total()
If you want every entry count DONT DO {{ $count++ }} because its echoing data. Instead do like that
<?php $count++; ?>
{{ $count }}
First of all get 100 players with paginate(100) method then you can get the index or count in the following way:
<?php $count = 1; ?>
#foreach ($players as $player)
<tr>
<td>{{$players ->perPage()*($players->currentPage()-1)+$count}}</td>
</tr>
<?php $count++; ?>
#endforeach
Here {{$players ->perPage()*($players->currentPage()-1)+$count}} will print the index or count like 1 to 100 on first page and 101 to 200 on next page and so on.
If you want to use laravel 5 loop itration
`#foreach ($players as $player)
<tr>
<td>{{$players->perPage()*($players->currentPage()-1)+$loop->iteration}}</td>
</tr>
#endforeach`
Hope this helps.
What i used to do in order to avoid php tags in blade templates in 4.2 was something like that
#foreach ($players as $key => $player)
<tr>
<td>{{ (Input::get('page', 1) - 1) * $players->getPerPage() + $key + 1 }}. </td>
</tr>
#endforeach
It's kinda strange but it will do the trick.
I have got my App returning this array: http://pastebin.com/3VKcjLMT
I'm trying to list the data on my laravel view as:
FAB0003 - Accessory Stand Short Section
-- MAC0010
-- CUT0010
FAB0004 - Accessory Stand Long Section
-- MAC0011
-- RAW0010
but I can't for the life of me figure out how to access the pieces of information beginning with FAB! My current view code is this:
#foreach ($product->fabrications as $items)
#foreach ($items as $item)
<tr>
<td></td>
<td>{{ $item->part_number }}</td>
</tr>
#endforeach
#endforeach
I would be so grateful if you could help out a PHP noob who has wasted hours on this :(
Try with -
#foreach ($product->fabrications as $key => $items)
$key will be the parent key.
Note : It should be a array key.