This code works, however in my learning of Laravel, I want to know if using Blade+Laravel syntax, can be better implemented
<?php
$i = 1;
while ($i <= (5 - $post->images->count())) {
echo '<div class="col"> </div>';
$i++;
}
?>
Thanks
https://laravel.com/docs/5.5/blade#loops
I would suggest using a for loop instead of a while loop in this case:
#for ($i = 1; $i <= (5 - $post->images->count()); $i++)
<div class="col"> </div>
#endfor
I'm not sure is the best way to do, but works.
<?php $z = 0; ?>
#while ($z < 3)
{{ "test".$z }}
<?php $z++ ?>
#endwhile
#php
$i = 0;
#endphp
#while (++$i <= (5 - $post->images->count()))
<div class="col">
</div>
#endwhile
#for ($i = 0; $i <= (5 - $post->images->count()); $i++)
<div class="col"> </div>
#endfor
Yes, there is. Templating is made just for that, you can see how similar things are done with the docs : laravel blade : loops
#for ($i = 0; $i < $post->images->count()); $i++)
<div class="col"> </div>
#endfor
The better solution would be to use #foreach
#foreach( $image as $post->images )
<div class="col"> </div>
#endforeach
In this specific case beacause you are looping count() you should use foreach, however you may also be interested #forelse:
#forelse($image as $img)
<div class="col"> {{ $img }}</div>
#empty
<div class="col"> NO IMAGES </div>
#endforesle
Related
I have a div that displays items , quantities and total prices for each item using a foreach loop. I now want to get the total amount and dont seem to get it.
This is my blade file
<div class="order-summary">
<div class="order-col">
<div><strong>PRODUCT</strong></div>
<div><strong>TOTAL</strong></div>
</div>
<div class="order-products">
#if(session('cart'))
#foreach(session('cart') as $id => $details)
<?php
$total = 0;
$total += $details['price'] * $details['quantity'] ;
$total_amount += $details['price'] * $details['quantity'];
?>
<div class="order-col">
<div>
<h4 class="nomargin">{{$details['quantity'] }}x {{ $details['name'] }}</h4>
</div>
<div>KSh {{ $total }}</div>
</div>
#endforeach
#endif
</div>
<hr>
<div class="order-col">
<div>Shiping</div>
<div><strong>KSh 300</strong></div>
</div>
<hr>
<div class="order-col">
<div><strong>TOTAL</strong></div>
<?php $total_amount += $total?>
<div><strong class="order-total">KSh {{ $total_amount }} </strong></div>
</div>
</div>
I have also tried <div><strong class="order-total">KSh <?php echo $total_amount?> </strong></div> but still does not work.
I get an error of undefined variable total_amount. How do I go about it?
The variables $total and $total_amount must be outside of the foreach
here I have rewritten your code
<div class="order-summary">
<div class="order-col">
<div><strong>PRODUCT</strong></div>
<div><strong>TOTAL</strong></div>
</div>
#php $total = 0; $total_amount = 0; #endphp
<div class="order-products">
#if(session('cart'))
#foreach(session('cart') as $id => $details)
#php
$total += $details['price'] * $details['quantity'] ;
$total_amount += $details['price'] * $details['quantity'];
#endphp
<div class="order-col">
<div>
<h4 class="nomargin">{{$details['quantity'] }}x {{ $details['name'] }}</h4>
</div>
<div>KSh {{ $total }}</div>
</div>
#endforeach
#endif
</div>
<hr>
<div class="order-col">
<div>Shiping</div>
<div><strong>KSh 300</strong></div>
</div>
<hr>
<div class="order-col">
<div><strong>TOTAL</strong></div>
#php $total_amount += $total; #endphp
<div><strong class="order-total">KSh {{ $total_amount }} </strong></div>
</div>
</div>
i have made a loop to increment the date but then it loops as many times as the if condition becomes true.
#foreach($c as $cc)
#for($i = $range_from; $i <= $range_to; $i++)
<div class="text-center">
{{ date('m-d-y', strtotime($i)) }}
<div style="margin-bottom: 20px;">
#if( $cc->attendance_date == date('Y-m-d', strtotime($i)) )
<br><h1>present</h1><br>
#else
<br>absent<br>
#endif
</div>
</div>
#endfor
#endforeach
I tried Route::view(), Route::redirect(), redirect(), url() all of these function in <a> tag. But after using them <a> tag got hidden.
What's function I have to use to get filter data using month.
#for($i = 0,$carbon->month = 1; $i < 12; $i++)
<a class="btn-disable btn btn-sm primary-btn" target="_blank" href="{{ Route::view('/virtualcabinet/vc_auto_datatable', 'viewName'); }}">
{{$carbon->format('M')}}
</a>
<?php $carbon->month = ++$carbon->month; ?>
#endfor
You have redundant ';' after your route definition:
#for($i = 0,$carbon->month = 1; $i < 12; $i++)
<a class="btn-disable btn btn-sm primary-btn" target="_blank" href="{{ Route::view('/virtualcabinet/vc_auto_datatable', 'viewName') }}">
{{$carbon->format('M')}}
</a>
<?php $carbon->month = ++$carbon->month; ?>
#endfor
I am using laravel-snappy & wkhtmltopdf 0.12.2.4 (unpatched qt) which doesn't support the --header-html switch so I have to manually create a header but the problem is the header and footer only appear on the first page. Is it possible to add a header and footer to every page without upgrading or downgrading wkhtmltopdf?
html
<div class="container">
<div class="header">
<img src="{{URL ('/images/logo.png')}}" alt="logo" width="125" height="50"/>
<h2>Parts Orientation</h2>
</div>
<div class="row marginBody">
#foreach ($parts as $part)
<div class="col-xs-2 marginThumbnail">
<div class="border text-center">
<img class="img" src="data:image/jpeg;base64, {{$part->image}}"
alt="Error" width="120" height="120"/>
<p class="displayText {{$part->class}}">
{{$part->name}}
</p>
</div>
</div>
#endforeach
<div class="footer">
<p> {{$date}} </p>
</div>
</div>
</div>
</body>
</html>
controller
public function printPDF(Request $request)
{
$parts = Parts::all();
$date = $this->getTime();
$pdf = SnappyPdf::loadView('parts.print.landscape', ["parts" => $parts, "date" => $date->toFormattedDateString()])
->setOrientation('landscape')
->setPaper('a4');
}
}
I was able to figure out a solution that works for me. Its very messy but it works
controller
public function printPDF(Request $request)
{
$parts = Parts::all();
$date = $this->getTime();
// divide by 36 because I can fit 36 items on each page
$totalPages = ceil(count($parts)/36);
$pdf = SnappyPdf::loadView('parts.print.landscape', ["parts" => $parts, "date" => $date->toFormattedDateString(), "totalPages" => $totalPages])
->setOrientation('landscape')
->setPaper('a4');
}
}
html
<body>
#php
$count = 0;
$page = 0;
#endphp
<div class="container">
<div class="header">
<img src="{{URL ('/img/logo.png')}}" alt="logo" width="125" height="50"/>
<h2>PARTS ORIENTATION</h2>
</div>
<div class="row marginBody">
#foreach ($parts as $part)
#php
// 36 is the total number of items I can fit on a page
// so after 36 I add a header and footer
if ($count === 36) {
echo '<div class = "newPageHeader">';
echo '<h2>PARTS ORIENTATION</h2>';
echo '</div>';
$count = 0;
$page++;
}
#endphp
<div class="col-xs-2 outline">
<div class="border text-center">
<img class="img" src="data:image/jpeg;base64, {{$part->image}}"
alt="Error" width="127" height="127"/>
<p class="displayText {{$part->c,ass}}">
{{$part->text}}
</p>
</div>
</div>
#php
$count++;
if ($count == 36) {
// 36 is the total number of items I can fit on a page
// so after 36 I add a header and footer
$page = ($page == 0) ? 1 : $page;
echo '<footer class="footer">';
echo "<span class='dateFooter'> $date </span>";
echo "<span class='pageFooter'> $page of $totalPages </span>";
echo '</footer>';
}
elseif ($totalPages == $page) {
echo '<footer class="lastFooter">';
echo "<span class='dateFooter'> $date </span>";
echo "<span class='lastPageFooter'> $page of $totalPages </span>";
echo '</footer>';
}
#endphp
#endforeach
</div>
</div>
</body>
I have this code:
<? $counter = 1 ?>
<div id="container_blog_all">
#foreach ($posts as $post)
<div class="blog_block" style="#if($counter % 3 == 0) margin-right:0px #endif">
<img class="blog_block_cover" src="{{ URL::to('uploads/blog/cover/'.$post->cover) }}">
<div class="blog_block_date">{{ $post->date }}</div>
<div class="blog_block_sep"></div>
<div class="blog_block_title">{{ $post->title }}</div>
</div>
<? $counter++; ?>
#endforeach
</div>
And I am getting this error, any reasons why??
I can see the the variable counter is defined, so why am I getting this error?
You're missing a semicolon:
<? $counter = 1 ?>
should be
<? $counter = 1; ?>
Looks like laravel doesn't like php short tags, it is better you use <?php $counter = 1 ?> rather than <? $counter = 1 ?>.That should resolve the error also.