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.
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
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
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.
Is there a way to do a foreach on for example your products and sort this by their category_id and show the category name above the products. (I'm using laravel 4 as framework)
Example what the output should look like:
Chairs
wooden chair number 1
pretty chair number 1
pretty chair number 2
Doors
tall door number 1
white door number 1
Key question: How can i make a foreach without having the categoryname above each row ?
Hopefully i made my question/problem clear and can someone help me out!
This is what i got so far:
Though, when i do my foreach in the blade i will get the error: " Undefined index "
The foreach i used in my controller:
foreach($results as $result) {
$category[$result->category_id][] = $result;
}
My foreach in the blade:
#foreach ($category as $key => $product)
<tr>
<td>{{ $product[$key]['created_at'] }}</td>
<td>{{ $product[$key]['order_id'] }}</td>
<td>{{ $product[$key]['name'] }}</td>
</tr>
#endforeach
you are attempting to use category_id to fetch $product from $products, which is array with 0-X indexes. for sake of simplicity i will ignore html formatting and syntax since i never used framework mentioned...
#foreach ($category as $key => $products)
echo 'Category #', $key
#foreach ($products as $product)
<tr>
<td>{{ $product['created_at'] }}</td>
<td>{{ $product['order_id'] }}</td>
<td>{{ $product['name'] }}</td>
</tr>
#endforeach
#endforeach
not completely sure i understand the question. but if im trying to organize an array.. i might use a conditional statement inside the foreach loop. I'm not sure your question is very clear.. see if this helps at all
foreach($results as $result) {
// just an example.. i narrow down what I push into my category array
if($result->type == 'whatever'){
$category[$result->category_id][] = $result;
}
}