I have this report that Im trying to do, but I want to make the rows alternate colors. this is what I tried, but it does not work. What is the correct way to achieve this?
<div class="row">
{{$rowOrder = "even"}}
#foreach($data as $row)
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->adm_referraldate}}</span>
<span>{{$row->adm_number}}</span>
</div>
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->dmg_nhsnumber}}</span>
<span>{{$row->dmg_firstname." ".$row->dmg_surname}}</span>
<span>{{$row->dmg_dateofbirth." - (".$row->dmg_ageyears.")"}}</span>
<span>{{$row->dmg_sex}}</span>
</div>
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->dmg_nhsnumber}}</span>
<span>{{$row->dmg_firstname." ".$row->dmg_surname}}</span>
<span>{{$row->dmg_dateofbirth." - (".$row->dmg_ageyears.")"}}</span>
<span>{{$row->dmg_sex}}</span>
</div>
#endforeach
</div>
Replace
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
with
<?php $rowOrder = ($rowOrder == "odd") ? 'even' : 'odd'; ?>
or if you are using a Laravel 5.2 or up
#php($rowOrder = ($rowOrder == "odd") ? 'even' : 'odd')
Do the same for the line {{$rowOrder = "even"}}
If you used the {{$rowOrder = "even"}} it will echo out the result.
You can use modulo arithmetic to decide whether and index is odd or even:
$isEven = index % 2
If you combine this with a PHP ternary operator then you'd get this
{{ $loop->index % 2 ? 'odd': 'even' }}
see
https://davidwalsh.name/php-shorthand-if-else-ternary-operators
and
https://en.wikipedia.org/wiki/Modular_arithmetic
Here's a very easy solution:
#php $count = 0; #endphp
#foreach($data as $row)
<div class="{{ ++$count % 2 ? 'odd': 'even' }}">
{{ $row->name }}
</div>
#endforeach
Use variable $loop documentation ( $loop->even laravel 5.8, or ($loop->iteration % 2)laravel< 5.8 )
#foreach ($users as $user)
#if ($loop->even)
This is even.
#else
#endif
#endforeach
or
#foreach ($listObject as $Object)
<tr class="{{ ($loop->iteration % 2) ? 'odd' : 'even' }}">
#endforeach
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
possibly should be
{{ $rowLine = ($rowOrder == "odd" ? 'even' : 'odd') }}
Here's a working example for me: I left the dump output in it so you can see the actual number counting up. Hope it helps anyone who comes across this problem :). EDIT: Don't forget to add colors in your css file for .odd and .even!
#if(!empty($names))
{{-- SET VARIABLE + HIDE IT --}}
<div class="hide">{!! $number = 0 !!}</div>
#foreach($names as $n)
{{ dump($number) }}
<div class="{!! $number % 2 == 0 ? 'odd' : 'even' !!}">
{{-- UP VARIABLE + HIDE IT --}}
<div class="hide">{!! $number++ !!}}</div>
{{-- DISPLAY CONTENT —}}
{{ $n }}
</div>
#endforeach
#endif
Try getting the key from the foreach loop and run ($key % 2)
Basically Odd Number mod 2 always have a remainder
#foreach ($rows as $key => $row)
<div class="#if ($key > 0 && $key % 2) odd #else even #endif">
</div>
#endforeach
Related
I want to display an image for each entry in a list of posts. The name of the images are unique file names. They are all saved in a database (MySQL) table. There is more than one image for each post. The code works right. Except, when there is a post without an image filename. This is a possible scenario but i can't get the code to work. In the event there's no existing filename for a post, i want to display a default filename.
Here's my code:
Images logic:
/**
* Get a vehicle's top or main image data from the image table
*/
public function topImage($id)
{
$image = Vehiclefulldataimage::where('vehiclefulldatas_id', $id)
->orderBy('id', 'ASC')->first();
return $image;
}
Here's the blade view, Posts:
#if (count($posts) > 0)
#foreach($posts as $post)
<?php $imageFilename = (new \App\Models\Logic\Images)->topImage($post->id); ?>
<!--Item-->
<li>
<div class="preview">
#if ($imageFilename = 0)
<img src="{{ asset('images') . '/' . 'defaultImage.jpg' }}" alt="No photo">
#else
<img src="{{ asset('images') . '/' . $imageFilename->disk_image_filename }}" alt="{{ ucfirst($imageFilename->caption) . ' | ' . $post->title }}">
#endif
</div>
<div class="desc">
<h3>{{ str_limit($post->title, 100, '...') }}</h3>
</div>
</li>
<!--/Item-->
#endforeach
#endif
This is the error message i get:
"Trying to get property of Non Object"
Try the following code:
You are using an assignment operator instead of comparison operator
//#if (count($posts) > 0)
#if (!$posts->isEmpty())
#foreach($posts as $post)
<?php $imageFilename = (new \App\Models\Logic\Images)->topImage($post->id); ?>
<!--Item-->
<li>
<div class="preview">
//#if ($imageFilename = 0) this is an assignment operator not comparison operator
#if ($imageFilename->isEmpty())
<img src="{{ asset('images') . '/' . 'defaultImage.jpg' }}" alt="No photo">
#else
<img src="{{ asset('images') . '/' . $imageFilename->disk_image_filename }}" alt="{{ ucfirst($imageFilename->caption) . ' | ' . $post->title }}">
#endif
</div>
<div class="desc">
<h3>{{ str_limit($post->title, 100, '...') }}</h3>
</div>
</li>
<!--/Item-->
#endforeach
#endif
Check your if statement....what you're doing there is an assignment and not a condition test...you can include a column..with a string pointing to the path of your default image..and set it to default...then load it...as default image
All of the answers above fix your problem. However, I recommend you to fix this problem by using Laravel features, Mutators.
https://laravel.com/docs/5.8/eloquent-mutators
Laravel mutators allow you to format or modify your Model attribute.
Just in your Vehiclefulldataimage model write the code below:
public function getImageAttribute($value)
{
//can write more logic here.
return $value ?: 'path/to/your/default/image';
}
Then you don't care more if condition blade template. If your image is empty the mutator returns the default image. So, everywhere when your retrieve Vehiclefulldataimage instance the mutator works properly
Thanks everyone.
After looking at all the answers, i did a simple alteration to my code. I altered:
#if ($imageFilename = 0)
to:
#if ($imageFilename == null)
Thus, my code now looks:
#if (count($posts) > 0)
#foreach($posts as $post)
<?php $imageFilename = (new \App\Models\Logic\Images)->topImage($post->id); ?>
<!--Item-->
<li>
<div class="preview">
#if ($imageFilename == null)
<img src="{{ asset('images') . '/' . 'defaultImage.jpg' }}" alt="No photo">
#else
<img src="{{ asset('images') . '/' . $imageFilename->disk_image_filename }}" alt="{{ ucfirst($imageFilename->caption) . ' | ' . $post->title }}">
#endif
</div>
<div class="desc">
<h3>{{ str_limit($post->title, 100, '...') }}</h3>
</div>
</li>
<!--/Item-->
#endforeach
#endif
this for each gave me task where task slug is the same as URL now I need to get the first task after this one so I make redirect link later
#foreach ($Tasks as $Task)
#if ( $Task->slug == Request::segment(5) )
<h2> {{ $Task->task_name }} </h2>
#endif
#endforeach
I need to get the task how is after the one in URL from foreach
Try this code. I hope this will help you
#php
$i = 0
#endphp
#foreach ($Tasks as $Task)
#if ($i > 0)
// write your first task logic here
#endif
#if ( $Task->slug == Request::segment(5) )
<h2> {{ $Task->task_name }} </h2>
#php
$i = $i + 1
#endphp
#endif
#endforeach
Would Laravel's pagination give you the features you need more easily?
In your controller:
$tasks = Task::where('slug', request()->segment(5))->paginate();
You can the use the following in your blade:
<?php
$tasks->nextPageUrl()
?>
As I am new to Laravel so facing this problem and tried it in other ways but it is not working.
Here my blade file
products.blade.php
#foreach($products as $product)
<div class="women">
<h6>{{$product->title}}</h6>
<span class="size">XL / XXL / S </span>
<p ><em class="item_price">Rs.{{$product->price}}</em></p>
</div>
#if(count($product) == 3)
<div class="clearfix"></div>
#endif
#endforeach
Why this is not working
#if(count($product) == 3)
<div class="clearfix"></div>
#endif
Or how can I count the product in iteration and compare the count number in if statement?
You can use loop variable like this.
Instead of:
#if(count($product) == 3)
<div class="clearfix"></div>
#endif
you should use:
#if($loop->iteration == 3)
<div class="clearfix"></div>
#endif
But it's quite possible that you might need it after every 3 elements (3, 6, 9 and so on), so maybe better solution would be:
#if($loop->iteration % 3 == 0)
<div class="clearfix"></div>
#endif
You example didn't work because $product is just an object so count($product) will not have expected value. Also if you used count($products) (notice trailing s) it won't work because count of products is the same in each loop iteration.
If you have more or less than 3 products, your if statement is never going to show. What you're looking for is the third item in the products array, which you can do like this:
#foreach($products as $key => $product)
<div class="women">
<h6>{{$product->title}}</h6>
<span class="size">XL / XXL / S </span>
<p><em class="item_price">Rs.{{$product->price}}</em></p>
</div>
#if($key == 2)
<div class="clearfix"></div>
#endif
#endforeach
To get the index of the loop, use
#foreach ($athletes as $key=>$athlete)
// Some html goes here
{ { ++$key } }
#endforeach
add if condition to key
let me know how it goes
The array count will always be the same, inside or outside of the loop.
If you want to make a decision based on the current iteration, ex. "on every third product put a clearfix div", then apply the condition to the key if the key is numeric.
Blade provides a loop variable with an iteration property (starts at 1) to help with this, see here https://laravel.com/docs/5.6/blade#the-loop-variable
Example for every third product:
#foreach($products as $product)
...
#if ($loop->$loop->iteration%3 == 0)
<div class="clearfix"></div>
#endif
...
#endforeach
Example for the third product only:
#if ($loop->$loop->iteration == 3)
<div class="clearfix"></div>
#endif
I´m trying to output 2 different things. For example: if title is greater then 0 then do this. If not, do this.
I'm using DomDocument & Laravel 5.4
In my controller:
$title = $dom->getElementsByTagName('title');
To output on the page:
#foreach ($title as $node)
#if(!$node > 0)
{{'No title'}}
#else
{{$node->nodeValue, PHP_EOL}} <br />
#endif
#endforeach
The problem: If there is a title it displays the title. If there is no title it shows nothing. I want to display: "No title".
Why isn't this working?
You should do this:
#if (condition)
No title
#else
But I doubt !$node > 0 part does what you want.
It's better to use ternary operator:
#foreach ($title as $node)
{{ empty($node->nodeValue) ? '' : $node->nodeValue }} <br />
#endforeach
I have setup if conditions in my blade template with an if condition that consists of an or operator, but it is showing html even when the condition is false. Not sure how to fix that?
#if(count($images) || count($videos) > 1)
<div class="orbit" role="region" data-orbit id="slider">
<ul class="orbit-container">
<button class="orbit-previous"><span class="show-for-sr">Previous Slide</span>◀︎</button>
<button class="orbit-next"><span class="show-for-sr">Next Slide</span>▶︎</button>
#if(count($images) > 1)
#foreach($images as $image)
<li class="orbit-slide">
<img src="http://coop.app/imagecache/cover/{{ $image }}" class="header-img">
</li>
#endforeach
#endif
#if(count($videos) > 1)
#foreach($videos as $video)
<li class="orbit-slide">
<div class="flex-video">
<iframe src="{{ $video }}"></iframe>
</div>
</li>
#endforeach
#endif
</ul>
</div>
#endif
You write or (||) condition in if statement that means if one condition (count($images)) or count($videos) is true then if statement will work.
If two condition is false then your if statement will not work
I think your first condition is not correct : #if(count($images) || count($videos) > 1). The > 1 is not applied on both elements, you should try #if(count($images) > 1 || count($videos) > 1).