Smarty - Fill a table with Arrays - php

In my PHP class I'm sending over some arrays, for every contract the customer has a row in the table should be filled.
I got some mistakes with my date:
on line 70 "{$date[$id] | date_format: %d %m %Y}" - Unexpected "|"
also some errors for the other arrays
<table class="table table-hover">
<thead>
<tr>
<td>ID</td>
<td>Datum</td>
<td>Preis</td>
<td>Verbrauch</td>
<td>Abnahmestellen</td>
<td>Status</td>
</tr>
</thead>
<tbody>
{foreach from=$contractCount item=item key=key}
<tr>
<td>
<input type="button" style="text-align: center" width="200em" value="Gehe zu ">{$id+1}
</td>
<td>
{$date[$id] | date_format: %d %m %Y}
</td>
<td>
{$KWhRate[$id]}
</td>
<td>
{$totalUsage[$id]}
</td>
<td>
{$consumptionsPoints[$id]}
</td>
<td>
{if $status[$id] == 0}
<span {literal}style="background-color:#ff9933"{/literal}>In Bearbeitung</span>
{/if}
{if $item->status[$id] == 1}
<span {literal}style="background-color:#ff0000"{/literal}>Abgeschlossen</span>
{/if}
{if $item->status[$id] == 2}
<span {literal}style="background-color:#33cc33"{/literal}>Stoniert</span>
{/if}
</td>
</tr>
{/foreach}
</tbody>
</table>

Related

How display the first element of a list ? Smarty, Prestashop module

I'm trying to change a view in a Prestashop Module. I would like to display just the first one case of a list.
Here, is the original list:
{if count($tax_details)>0 && $use_taxes}
<br />
<br />
<table cellpadding="3">
<thead>
<tr style="color:#FFFFFF; background-color: #4D4D4D; font-weight:bold; padding:2px;">
<th style="width: 40%;">{l s='TAX DETAILS' mod='opartdevis'}</th>
<th style="width: 20%;">{l s='Tax rate' mod='opartdevis'}</th>
<th style="width: 20%;">{l s='Total without tax' mod='opartdevis'}</th>
<th style="width: 20%;text-align: right;">{l s='Total tax' mod='opartdevis'}</th>
</tr>
</thead>
{foreach $tax_details as $tax}
<tr>
<td style="width: 40%;">{$tax.prefix|escape:'htmlall':'UTF-8'}</td>
<td style="width: 20%;">{$tax.name|escape:'htmlall':'UTF-8'}</td>
<td style="width: 20%;">{Tools::displayPrice($tax.total_ht)}</td>
<td style="width: 20%;text-align: right;">{Tools::displayPrice($tax.total_tax)}</td>
</tr>
{/foreach}
</table>
{/if}
It display this :
And I would like to display just this number in yellow on another place.
This is my code (it situated on the same page than the original list):
{if count($tax_details)>0}
<tr>
<td colspan="7" style="text-align:right;">
Total éco taxes :
</td>
<td colspan="1" style="text-align:right;">
<span id="total_price_remiseincl">
{Tools::displayPrice($tax_details[0].total_tax)}
</span>
</td>
</tr>
{/if}
I have this error:
I tried many ways to write it, but I don't find how to do.
Could you help me pls ? I'm new with Smarty.
Thanks in advance
Malaury
You need to set up a loop again
{assign 't' '0'}
{foreach $tax_details as $tax}
{if $t === '0'}
{Tools::displayPrice($tax.total_tax)}
{assign 't' '1'}
{/if}
{/foreach}
Alternatively, you can use the 'first' properly like below. (docs)
In your foreach, use,
{if $smarty.foreach.tax_details.first}
<tr>
<td colspan="3"></td>
<td style="width: 20%;text-align: right;">
{Tools::displayPrice($tax.total_tax)}
</td>
</tr>
{/if}

Nested Table Loop Laravel 5.4

I am having some trouble writing some nested HTML Tables from my Laravel Collection.
My Model
public static function offers($userId)
{
return DB::table('users_vehicles')
->join('dealer_offers', 'users_vehicles.id', '=', 'dealer_offers.vehicle_id')
->where('user_id', '=', $userId)
->select('users_vehicles.*', 'dealer_offers.*');
}
My Controller
public function dash()
{
$userCars = UserVehicles::offers(Auth::user()->id)->get();
return view('customer.dash', compact('userCars'));
}
My Collection Results
In My View blade Template
#foreach( $userCars->chunk(1) as $userCarChunk)
<div id="no-more-tables">
<table class="table table-bordered table-hover">
<caption>Offers for My Mazda 326 </caption>
<thead>
<tr>
<th class="numeric">DEALER RATING</th>
<th class="numeric">AMOUNT </th>
<th class="numeric"> 3 DAYS To Accept </th>
</tr>
</thead>
<tbody>
#foreach( $userCarChunk as $car)
<tr>
<td data-title="First Name">
<div class="rating">
<label>
<input type="radio" name="rating" value="5" title="5 stars"> 5
</label>
</div>
</td>
<td data-title="Last Name"> 55 000 </td>
<td data-title="Username">
Accept
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endforeach
Front-End View
Question
How can I create a nested HTML Table for all the user vehicles, so that I can write the vehicle name and all the offers for that particular vehicle?
I hope my question is clear.
Try it like this :
<div id="no-more-tables">
<table class="table table-bordered table-hover">
<caption>Offers for My Mazda 326 </caption>
<thead>
<tr>
<th class="numeric">DEALER RATING</th>
<th class="numeric">AMOUNT </th>
<th class="numeric"> 3 DAYS To Accept </th>
</tr>
</thead>
<tbody>
#foreach( $userCars->chunk(1) as $userCarChunk)
#foreach( $userCarChunk as $car)
<tr>
<td data-title="First Name">
<div class="rating">
<label>
<input type="radio" name="rating" value="5" title="5 stars"> 5
</label>
</div>
</td>
<td data-title="Last Name"> 55 000 </td>
<td data-title="Username">
Accept
</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
</div>
Answer
My Model UserVehicles.php
public function offers() {
return $this->hasMany(DealerOffers::class, 'vehicle_id');
}
My Controller CustomerController.php
$offersToCustomer = UserVehicles::with('offers')->get();
Dashboard Blade template
#foreach( $offersToCustomer as $offer)
<div id="no-more-tables_">
<table class="table table-bordered table-hover">
<caption>Offers for My :: <b style="color: #00BFF0"> {{ $offer->vehicle_make }} {{ $offer->vehicle_model }} </b> </caption>
<thead>
<tr>
<th class="numeric">DEALER RATING</th>
<th class="numeric">AMOUNT REQUESTED </th>
<th class="numeric">AMOUNT OFFERED </th>
<th class="numeric"> Expires :: {{ Carbon\Carbon::parse($offer->offer_expiry)->format('d-m-Y') }} </th>
</tr>
</thead>
<tbody>
#foreach( $offer->offers as $subOffer)
<tr>
<td data-title="DEALER RATING">
<input id="rating" name="rating" class="rating rating-loading" value="3" data-min="0" data-max="5" data-step="1" />
</td>
<td data-title="AMOUNT REQUESTED">R {{ number_format($offer->vehicle_amount_asked, 2, '.', ' ') }} </td>
<td data-title="AMOUNT OFFERED">R {{ number_format($subOffer->offer_amount, 2, '.', ' ') }} </td>
<td data-title="ACTIONS">
<button data-offer_id="{{ $offer->id }} " data-vehicle_id="{{ $subOffer->vehicle_id }}"
class="btn btn-xs btn-success accept-offer">Accept</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endforeach
Output

Smarty new table row every nth time

Hi I'm making this HTML email and thanks to the good old Outlook 07 that adds a really nice page break I kinda have to change my logic. Currently my html looks something like this:
<table>
<tr> //I want for every 6th element to create a new <tr>
<td>
<table>
{foreach from=$data.elements item=element name=elements}
{if $smarty.foreach.elements.iteration is odd}
<tr>
<td>
{/if}
<table align="{if $smarty.foreach.elements.iteration is odd}left{else}right{/if}">
Some content
</table>
{if $smarty.foreach.elements.iteration is odd}
<table align="left" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" height="40" style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;"> </td>
</tr>
</table>
{else}
</td>
</tr>
{/if}
{/foreach}
</table>
</td>
</tr>
</table>
Right now after the 6th element it creates this page break so I want is when I have let's say 12 elements each six to have their own table row with table inside.
The following code should work for you:
<table>
{foreach from=$data.elements item=element name=elements}
{if $smarty.foreach.elements.iteration mod 6 eq 1}
<tr>
<td>
<table>
{/if}
{if $smarty.foreach.elements.iteration is odd}
<tr>
<td>
{/if}
<table align="{if $smarty.foreach.elements.iteration is odd}left{else}right{/if}">
Some content
</table>
{if $smarty.foreach.elements.iteration is odd}
<table align="left" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" height="40" style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;"> </td>
</tr>
</table>
{else}
</td>
</tr>
{/if}
{if $smarty.foreach.elements.iteration mod 6 eq 0 or $smarty.foreach.elements.last}
</table>
</td>
</tr>
{/if}
{/foreach}
</table>
but because of many tables I cannot check it for sure. You could also look at Break UL tag after 'n' times in while-loop where I answered how to do the same in PHP.

Using "if" "else" in smarty table?

Here's the code I'm using for my table:
<tbody>
{foreach key=num item=referral from=$referrals}
{if $referral.commission neq "$0.00 USD"}
<tr>
<td>{$referral.date}</td>
<td>{$referral.service}</td>
<td>{$referral.amountdesc}</td>
<td>{$referral.commission}</td>
<td>{$referral.status}</td>
</tr>
{/if}
{foreachelse}
<tr>
<td colspan="6">{$LANG.norecordsfound}</td>
</tr>
{/foreach}
</tbody>
Basically if $referral.commission is $0.00 USD then I don't want that row to show. However, right now those rows are showing and I'm not sure what I'm doing wrong...
It's your quotes. Use single quotes and change the if statement to '$0.00 USD' instead of using double quotes.
<tbody>
{foreach key=num item=referral from=$referrals}
{if $referral.commission != '$0.00 USD'}
<tr>
<td>{$referral.date}</td>
<td>{$referral.service}</td>
<td>{$referral.amountdesc}</td>
<td>{$referral.commission}</td>
<td>{$referral.status}</td>
</tr>
{/if}
{foreachelse}
<tr>
<td colspan="6">{$LANG.norecordsfound}</td>
</tr>
{/foreach}
</tbody>
try this:
<tbody>
{foreach key=num item=referral from=$referrals}
{if $referral.commission neq "$0.00 USD"}
<tr>
<td>{$referral.date}</td>
<td>{$referral.service}</td>
<td>{$referral.amountdesc}</td>
<td>{$referral.commission}</td>
<td>{$referral.status}</td>
</tr>
{else}
<tr>
<td colspan="6">{$LANG.norecordsfound}</td>
</tr>
{/if}
{/foreach}
</tbody>
For more details to use if/else inside foreach loop refer http://www.smarty.net/docs/en/language.function.foreach.tpl

Table width with Smarty

How to use alternative width in table with php? I used this way but getting Error.
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="{if $label_payment}{echo = 50%}{else}{echo = 100%}{/if}">Shipping Address
</td>
<td>Payment Address
</td>
</tr>
<tr>
<td width="{if $label_payment}{echo = 50%}{else}{echo = 100%}{/if}">{$address_label_shipping}
</td>
{if $address_label_payment}
<td width="50%">{$address_label_payment}
</td>
{if}
</tr>
</table>
Is this the correct way?
{if $label_payment}{echo = 50%}{else}{echo = 100%}{/if}
There is no echo:
{if $label_payment}50%{else}100%{/if}
leading to:
<td width="{if $label_payment}50%{else}100%{/if}">
Try this...
<td {if $label_payment}width="50%"{else}width="100%"{/if}>Shipping Address

Categories