I have a project where I'm pulling posts from the database and rendering on the home page view. Like a Blog. (On the home page I limit this to 3 posts)
I have 6 rows in the table, and would like to style the output based on ODD and Even rows.
Here is my controller:
public function index()
{
$counter = Post::count();
$posts= DB::table('posts')->orderBy('id', 'DESC')->limit(3)->get();
return view('home',compact('posts','counter'));
}
I want the even numbered rows to have <div class="even"> and the odd numbered rows to have <div class="odd">
When I dd on $counter I get the value 6. This is correct. I have 6 rows in the table.
What I'm currently trying based on other articles I've found:
#foreach ($posts as $post)
#if($counter % 2 == 0)
<div class="even">{{$post->title}}</div>
#else
<div class="odd">{{$post->title}}</div>
#endif
This doesn't do anything. Still outputs the rows as 6,5,4,3,2,1
So how can I write the IF Statement inside my Foreach loop to say...
if ($counter == odd)
<div class="odd">
else
<div class="even">
The order I'm looking for is:
Odd
Even
Odd
Even
$counter is a static variable, so calling $counter % 2 == 0 will always show the same result.
If you are using Laravel 5.4+, there is a $loop variable included in the #foreach(). So you can access your mod division within the loop.
Here is the example for Laravel 5.4+
#foreach ($posts as $post)
#if($loop->iteration % 2 == 0)
<div class="even">{{$post->title}}</div>
#else
<div class="odd">{{$post->title}}</div>
#endif
#endforeach
Laravel 5.8.5 add even and odd Boolean flags in the Blade loop variable
Now you can use:
$loop->even or $loop->odd
Instead of
$loop->iteration % 2
Reference link
You need to increment the counter on each iteration.
#php
$counter = 1;
#endphp
#foreach ($posts as $post)
#if($counter % 2 == 0)
<div class="even">{{$post->title}}</div>
#else
<div class="odd">{{$post->title}}</div>
#endif
#php
$counter++;
#endphp
#endforeach
Related
I'm trying to display a specific set of references on my laravel page by iterating through an foreach loop and styling odd and even iterations differently with #class loop odd / loop even.
However I as I begin the foreach loop with an #if, only some make it into the loop while some are skipped, which messes up my odd / even (e.g. #1, #2 and #4 make it into the loop which means I have two elements styled with loop even.
How can I skip the ones that don't make it into the loop so I can display the result with alternating styling?
#foreach ($references as $reference )
#if(isset($reference['is_startpage'])
&& $reference['is_startpage'] != 1
&& isset($reference['content']['featured'])
&& $reference['content']['featured'] == 1)
<div #class([
'row py-5' => $loop->odd,
'row flex-row-reverse py-5'=> $loop->even,
])></div>
#endif
#endforeach
As you say, not every loop iteration prints the <div> element, so $loop->even and $loop->odd variables are not valid in your case for implementing that specific logic. However, there are many ways to achieve what you want. The first way that comes into my mind is using a specific counter for that, i.e.:
#php $featuredCount = 0; #endphp
#foreach ($references as $reference)
#if(
isset($reference['is_startpage'])
&& $reference['is_startpage'] != 1
&& isset($reference['content']['featured'])
&& $reference['content']['featured'] == 1
)
#php $featuredCount++; #endphp
<div #class([
'row py-5', // These two are always present, no need to conditionally rewrite them
'flex-row-reverse' => $featuredCount % 2 == 0, // 'Even' check
])></div>
#endif
#endforeach
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
I am using blade for the first time, and i need to change the styling of this div element depending on how many properties are available. I need to write an if statement which hides the div if equal or less than one and add a different class if equal to 2.
how would i write this using blade or php?
#if (isset($participatingProperties) && !empty($participatingProperties) && is_array($participatingProperties))
<?php $i = 0; ?>
#foreach ($participatingProperties as $key => $property)
#if ($i++ % 3 === 0)
<div class="item item2 {{ $i < 3 ? 'active' : '' }}">
#endif
What I understand is :
You have a $participatingProperties array and one div to print.
When it has 0 or 1 element, nothing happens.
When it has 2 elements, the div prints with a specific style.
When it has 3 or more elements, the div prints with another style.
(No rule for more than 3 elements).
I would keep it simple with count :
#php($count = count($participatingProperties))
#if ($count > 1)
<div
class="{!! ($count == 2) ? 'class-2' : 'class-3-or-more' !!}"
>
div content
</div>
#endif
If you don't need a specific style for every amount, it's even simpler :
#if (count($participatingProperties) > 1)
<div class="myclass">
div content
</div>
#endif
Using both empty and isset is redundant. And I think you should not have to check if $participatingProperties is an array, instead make sure it always is in your backend. If you return at least an empty array, count will return 0.
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 the following foreach loop that I'm getting good results with however when it gets to the elseif statement I'm attempting to see if two of the players are on the same team or not and if they are then have them listed together separated with a & symbol. Currently I am getting the following still.
Player 1 vs. Player 2 vs. Player 3 vs. Player 4
This is okay however Player 1 and Player 2 are on the same team. So its not seeing them as on the same team for some reason. Does someone see what my issue is?
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($match->players->count() - 1 != $key)
vs.
#elseif ($match->players[$key - 1]->team_id == $player->team_id)
&
#endif
#endforeach
EDIT: I changed the data a little but still should work.
http://pastebin.com/AdyzemC4
It was tricky to puzzle together your $match variable :)
#if($match->players->count() - 1 != $key)
Your $match->players->count() is always equal to the value of the number of players(say 'n'). So your 'if' ONLY checks the 'n-1' != current key ($key). [Which is true except for the last key, so you get all 'vs'].
This should work:
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($player->team_id != $match->players[$key + 1]->team_id)
vs.
#elseif ($player->team_id == $match->players[$key + 1]->team_id)
&
#endif
#endforeach
In this we are checking if the current player's team is the same as the next player's team [$key+1].
Note:
You need to stop the loop for the last player, since $key+1 will go outside your array and you will get an offset error.
Therefore add another if:
#foreach ($match->players AS $key => $player)
#foreach ($player->list as $member)
{{ $member->player_name }}
#endforeach
#if($key + 1 < $match->players->count())
#if($player->team_id != $match->players[$key + 1]->team_id)
vs.
#elseif ($player->team_id == $match->players[$key + 1]->team_id)
&
#endif
#endif
#endforeach