How to hide <section> with a child for in this case - php

I have a loop for in a <section> :
<section>
<h4 role="heading" aria-level="4">#lang('site.scope')</h4>
<ul>
#for( $i = 1; $i <= 4; $i++)
#if( $card->{"gen_champ__q" . $i } == 1 )
<li>#lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
#endif
#endfor
</ul>
</section>
But I don't have data that meets my condition if (== 1).
So, I would like to hide the complete section.
But I do not know how to do it, do you have an idea? If I go above my section, it will duplicate my content and that's not what I want.

You will write the "header" of the section with the first < li> (and only the first) you have to write. And you will write the "footer" of the section only if any < li> was written.
#for( $i = 1; $i <= 4; $i++)
#if( $card->{"gen_champ__q" . $i } == 1 )
#if (!isset($doSection) && $doSection=true)
<section>
<h4 role="heading" aria-level="4">#lang('site.scope')</h4>
<ul>
#endif
<li>#lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
#endif
#endfor
#if (isset($doSection))
</ul>
</section>
#endif
EDIT: Just upgraded to avoid unneeded variable initialization.

You can put one extra if before section starts.
#if($i>0)
<section>
<h4 role="heading" aria-level="4">#lang('site.scope')</h4>
<ul>
#for( $i = 1; $i <= 4; $i++)
#if( $card->{"gen_champ__q" . $i } == 1 )
<li>#lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
#endif
#endfor
</ul>
</section>
#endif

there is many way of doing this one of them is this .
In this when your condition meet then it print else not
#for( $i = 1,$j=1; $i <= 4; $i++)
#if( $card->{"gen_champ__q" . $i } == 1 )
#if($j==1&&$j--)
<section>
<h4 role="heading" aria-level="4">#lang('site.scope')</h4>
<ul>
#endif
<li>#lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
#endif
#endfor
#if($j==0)
</ul>
</section>
#endif
hope this will help you!

#for( $i = 1; $i <= 4; $i++)
#if( ! empty( $card->{"gen_champ__q" . $i })
&& $card->{"gen_champ__q" . $i } == 1 )
<li>#lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
#endif
#endfor

Related

Laravel Attempt to read property "produits" on null

whene i do the login action it return the error above where there is no user all work good
$currentTime = date("Y-m-d");
if(Auth::check()){
$user = Auth::user();
$favorie = $user->favorie;
$favprds = $favorie->produits;
}else{
$favprds = [];
}
this is my blade i believe that there is no probleme with blade
#for ( $i = 0; $i < count($favprds) ;$i++)
#if ($favprds[$i]->id == $pro->id)
<div class="tinv-wishlist-clear">
<a style="color:red;" href="{{ url('deleteFavorie/'.$pro->id) }}"><i class="fa fa-heart"></i></a>
</div>
#break
#elseif($favprds[$i]->id != $pro->id && $i == count($favprds)-1)
<div class="tinv-wishlist-clear">
<i class="klbth-icon-heart-1"></i>
</div>
#endif
#endfor
#if(count($favprds) == 0)
<div class="tinv-wishlist-clear">
<a style="color:#7f8c8d" href="{{ url('add-to-favorie/'.$pro->id) }}"><i class="klbth-icon-heart-1"></i></a>
</div>
#endif
it seems that the way i access the products is the wrong way thought i still have to test if the there's a probleme with adding to the favorite product
$currentTime = date("Y-m-d");
if ($favorie = auth()->user()->favorie ?? false) {
$favprds = $favorie->produits;
} else {
$favprds = [];
}

Is it possible to have 3 div elements and have your loop fill them in order?

Usually when I loop through a database table records, I put them in 1 div, however, I have been wondering whether it is possible to create 3 divs and then put one record in each div, then start from the first div again and rinse and repeat.
Example of how I've done it so far:
<div class="container">
#foreach($albumImages as $albumImage)
<div class="centeredImage stickyContainer" style="background-image: url('/storage/uploads/albums/{{$albumName}}/{{$albumImage->file_name}}')">
<a class='specialA' href=''></a>
</div>
#endforeach
</div>
As you can see in this case, all the records are in the container div.
Example of what I've been thinking about:
<div class="flex-grid">
<div class="col-l"></div>
<div class="col-c"></div>
<div class="col-r"></div>
</div>
and have the first record go in col-l, the second in col-c, the third in col-r and then start from col-l again.
Try this
<div class="flex-grid">
#php($count = 0)
#foreach($albumImages as $albumImage)
#if ($count % 3 == 0)
<div class="col-l"></div>
#elseif($count % 3 == 1)
<div class="col-c"></div>
#else
<div class="col-r"></div>
#endif
#php($count++)
#endforeach
</div>
You can use this code but I later will update my answer with more good solution
#php($count = 0)
#foreach($albumImages as $albumImage)
#if ($count % 3 == 0)
#php($albumImages1[] = $albumImage)
#elseif($count % 3 == 1)
#php($albumImages2[] = $albumImage)
#else
#php($albumImages3[] = $albumImage)
#endif
#php($count++)
#endforeach
#if (!empty($albumImages1))
#foreach($albumImages1 as $albumImage)
// your logic here
#endforeach
#endif
#if (!empty($albumImages2))
#foreach($albumImages2 as $albumImage)
// your logic here
#endforeach
#endif
#if (!empty($albumImages3))
#foreach($albumImages3 as $albumImage)
// your logic here
#endforeach
#endif
Also you can split three part your initial array make global helper functions.
define global function
function split_sequence_by_count ($array, $count) {
$result = [];
for ($i = 0; $i < $count; $i++) {
$result[$i] = [];
}
$_count = 0;
foreach ($array as $current) {
$index = $_count % 3;
$result[$index][] = $current;
$_count++;
}
return $result;
}
usage in blade
#php(list ($albumImages1, $albumImages2, $albumImages3) = split_sequence_by_count($albumImages, 3))
#foreach($albumImages1 as $albumImage)
// your logic here
#endforeach
#foreach($albumImages2 as $albumImage)
// your logic here
#endforeach
#foreach($albumImages3 as $albumImage)
// your logic here
#endforeach
Another way would be array_chunk then just run through the array with 2 nested loops. Should be self-explaining.
<?php
$people =
[
'John',
'Paul',
'Ringo',
'George'
];
$i=0;
$classes = ['col-a','col-b','col-c'];
foreach($people as $name) {
$class = $classes[$i++%3];
?>
<div class='<?=$class?>'>
<?=$name?>
</div>
<?php
}
Formatted output:
<div class='col-a'>
John
</div>
<div class='col-b'>
Paul
</div>
<div class='col-c'>
Ringo
</div>
<div class='col-a'>
George
</div>
Regarding placing each name in each column (as per your comment), we could wrangle the array format:
$classes = ['col-a','col-b','col-c'];
$i=0;
foreach($people as $name)
$columns[$classes[$i++%3]][] = $name;
?>
<?php foreach($columns as $class=>$column) { ?>
<div class='<?=$class?>'>
<?php foreach($column as $name) { ?>
<div class="name">
<?=$name?>
</div>
<?php } ?>
</div>
<?php } ?>
Formatted output:
<div class='col-a'>
<div class="name">
John
</div>
<div class="name">
George
</div>
</div>
<div class='col-b'>
<div class="name">
Paul
</div>
</div>
<div class='col-c'>
<div class="name">
Ringo
</div>
</div>

Laravel Only display first 5 array

$reviews has 10 arrays and I am trying to only display the first 5. This is what I have come up so far.
#for ($i = 0; $i < 6; $i++)
#foreach ($reviews as $reviews; i++)
<p>Body</p>
#endforeach
#endfor
Any idea why this isn't working?
Just use the take method and run a foreach loop, like so
#foreach($reviews->take(5) as $review)
<p>{{ $review->body }}</p>
#endforeach
I think that is much simpler and cleaner
Assuming you are using Collections you could use the take method (see here) method:
$collection = collect([0, 1, 2, 3, 4, 5]);
$chunk = $collection->take(3);
$chunk->all();
// [0, 1, 2]
Usage
$reviews2 = $reviews->take(5);
#foreach ($reviews2 as $review)
<p>{{ $review->body }}</p>
#endforeach
As you are asking .
Any idea why this isn't working?
Because you have syntax error here i++
#for ($i = 0; $i < 6; $i++)
#foreach ($reviews as $key=>$values)
<p>{{$i}} : Body</p>
#endforeach
#endfor
Note : changed $reviews row value as $values
You can use #if to limit the loop inside #foreach
#for ($i = 0; $i < 6; $i++)
#foreach ($reviews as $review)
{{$i++}}
#if ($i < 1)
<p>Body</p>
#endif
#endforeach
#endfor
You have written the wrong syntax of foreach
<?php $i = 1; ?>
#foreach ($reviews as $review)
<p>Body</p>
#if ($i == 5)
break;
#endif
<?php $i++; ?>
#endforeach

How can I add operator ternary in class on the view laravel blade?

I try like this :
#for($i = 0; $i < 5; $i++)
...
<div class="image ($i==0) ? 'image-main' : ''">
...
#endfor
But it does not work.
It seems the way of writing is incorrect.
How can I solve this problem?
In laravel's blade file you need to use {{}} to execute php code.
{{ ($i == 0) ? 'image-main' : '' }}
You need to use {{ }}
#for($i = 0; $i < 5; $i++)
...
<div class="image {{ ($i==0) ? 'image-main' : '' }}">
...
#endfor
<div class="{{ ($i == 0) ? 'image-main' : '' }}"></div>
try with this,
#for($i = 0; $i < 5; $i++)
<div class="image{{ ($i==0) ? 'image-main' : '' }}">
#endfor

ErrorException in Macroable.php line 81: Method lastPage does not exist. ---- laravel 5.2

Trying to achiveve the pagination of my user list (working fine), but on my filter page ( http://wasamar.dev/admin#users#filter?filter=new ) it give me this error,
ErrorException in Macroable.php line 81:
Method lastPage does not exist. (View: C:\laragon\www\wasamar\resources\views\pagination\limit_links.blade.php) (View: C:\laragon\www\wasamar\resources\views\pagination\limit_links.blade.php)
This is the paginator render
#include('pagination.limit_links', ['paginator' => $newestUsers])
THE CUSTOM PAGINATION VIEW
<!--
https://stackoverflow.com/questions/28240777/custom-pagination-view-in-laravel-5
Author: Mantas D
-->
<?php
// config
$link_limit = 7; // maximum number of links (a little bit inaccurate, but will be ok for now)
?>
#if ($paginator->lastPage() > 1)
<div class="pagination-centered">
<ul class="pagination">
<li class="montserrat-font {{ ($paginator->currentPage() == 1) ? ' unavailable' : '' }}">
First
</li>
#for ($i = 1; $i <= $paginator->lastPage(); $i++)
<?php
$half_total_links = floor($link_limit / 2);
$from = $paginator->currentPage() - $half_total_links;
$to = $paginator->currentPage() + $half_total_links;
if ($paginator->currentPage() < $half_total_links) {
$to += $half_total_links - $paginator->currentPage();
}
if ($paginator->lastPage() - $paginator->currentPage() < $half_total_links) {
$from -= $half_total_links - ($paginator->lastPage() - $paginator->currentPage()) - 1;
}
?>
#if ($from < $i && $i < $to)
<li class="montserrat-font {{ ($paginator->currentPage() == $i) ? ' current' : '' }}">
{{ $i }}
</li>
#endif
#endfor
<li class="montserrat-font {{ ($paginator->currentPage() == $paginator->lastPage()) ? ' unavailable' : '' }}">
Last
</li>
</ul>
</div>
#endif
based on this ( Reference Author: Mantas D ) Then i tried this
{{ $newestUsers->links() }} and i got this error too.
ErrorException in Macroable.php line 81:
Method links does not exist. (View: C:\laragon\www\wasamar\resources\views\main_app\admin\users#filter.blade.php)
so what did i do wrong?
The fix for this was easy: I had forgotten to add the paginate(30) method to the Users model. Adding that will solve the problem.

Categories