Passing a smarty foreach cycle variable into php code - php

I have foreach loop in smarty:
{foreach from=$clients item=client}
<tr class="{cycle values="erow,"} elements">
<td class="tdcenter no-label">{$client->id}</td>
<td>{$client->name}</td>
<td>{$client->email}</td>
<td>{$client->phone}</td>
<td>{php} echo get_client_profit($client->name);{/php}</td>
</tr>
{/foreach}
I need to pass smarty variable {$client->name} to php function get_client_profit. How should I do that? Is it possible?

I did this on my development machine and it works:
{foreach from=$clients item=client}
<tr class="{cycle values="erow,"} elements">
<td class="tdcenter no-label">{$client->id}</td>
<td>{$client->name}</td>
<td>{$client->email}</td>
<td>{$client->phone}</td>
<td>{get_client_profit($client->name)}</td>
</tr>
{/foreach}
Simply call the function without the {php} tags

Try this :
{foreach from=$clients item=client}
<tr class="{cycle values="erow,"} elements">
<td class="tdcenter no-label">{$client->id}</td>
<td>{$client->name}</td>
<td>{$client->email}</td>
<td>{$client->phone}</td>
<td>
{assign var="clientname" value=$client->name }
{php} echo get_client_profit($smarty->get_template_vars('clientname'));{/php}</td>
</tr>
{/foreach}

Related

Php Smarty Variable with value if else

I'm sorry, I do not know how to ask the question because my knowledge is small.
I'll give an example of what I'm trying to do:
The message: no records found if you have no ticket in the department "7"
{if $ticket.did["7"] == ''}
No records found!
{/if}
My Whmcs tpl code:
{foreach from=$tickets item=ticket}
{if $ticket.did == '7'}
<tr>
<td>#{$ticket.tid}</td>
<td>{$ticket.title}</td>
<td>{$ticket.status}</td>
<td>View</td>
</tr>
{/if}
{/foreach}
{if $ticket.did["7"] == ''}
<tr>
<td colspan="6" class="textcenter">{$LANG.norecordsfound}</td>
</tr>
{/if}
Thanks!
You can initialize new variable and set it true when the condition in loop will be satisfied.
{foreach from=$tickets item=ticket}
{if $ticket.did == '7'}
{assign var="ticketFound" value="true"}
<tr>
<td>#{$ticket.tid}</td>
<td>{$ticket.title}</td>
<td>{$ticket.status}</td>
<td>View</td>
</tr>
{/if}
{/foreach}
{if !$ticketFound}
<tr>
<td colspan="6" class="textcenter">{$LANG.norecordsfound}</td>
</tr>
{/if}

Smarty total amount sum

how can I count the final price to pay if I have the following code
$Total
:
{foreach from=$items key=k item=item}
<tr>
<td class="align_left"><strong>{$item.Title}</strong></td>
<td><img src="products_images/{$item.ImagePath}" width="60" height="60" alt="{$item.Title}" /></td>
<td>{$item.quantity}</td>
<td>{$item.Price}<span class="s_currency s_after"> лв.</span></td>
<td>{math equation="x * y" x=$item.Price y=$item.quantity}<span class="s_currency s_after"> лв.</span></td>
</tr>
{/foreach}
<tr class="last">
<td class="align_right" colspan="4"><strong>Всичко:</strong></td>
<td class="s_secondary_color"><span class="s_currency s_before">$</span>{$Total}</td>
</tr>
I did not understand why you need it so, but do it:
NOTICE: for Smarty 3.
{$Total = 0 }
{foreach from=$items key=k item=item}
<tr>
<td class="align_left"><strong>{$item.Title}</strong></td>
<td><img src="products_images/{$item.ImagePath}" width="60" height="60" alt="{$item.Title}" /></td>
<td>{$item.quantity}</td>
<td>{$item.Price}<span class="s_currency s_after"> лв.</span></td>
<td>{math equation="x * y" x=$item.Price y=$item.quantity}<span class="s_currency s_after"> лв.</span></td>
{$Total = $Total+ ($item.Price*$item.quantity) }
</tr>
{/foreach}
<tr class="last">
<td class="align_right" colspan="4"><strong>Всичко:</strong></td>
<td class="s_secondary_color"><span class="s_currency s_before">$</span>{$Total}</td>
</tr>
Better to use MySQL query with GROUP BY and SUM() if individual amounts store in DB
use assign in ur loop and add the values to variable {$total}
{assign var=total value=0}
better loop through ur array in php and calculate total, if data coming from database, then query sum

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

Smarty object array access

I have result from a pdo object fetch all as std object result which iam goiing trough like this.
{foreach from=$users key=userID item=row}
<tr>
<td>
{$row->name}
</td>
{foreach from=$regions key=regionId item=region}
<td>
{assign var=id value=$row->id}{assign var=cid value=$region->id} <input type="checkbox" name="regionChecked[{$region->id}][]" value="{$row->id}" {if $assighnedUsers.$id.$cid eq 1}checked="checked"{/if}>
</td>
{/foreach}
<td>
{$row->id|#var_dump} <input type="checkbox" name="regionDefault" value="{$row->id}" {if $loleq $row->id}checked="checked"{/if} >
</td>
<td data-user="{$userID}">
{$deleteButton}
</td>
</tr>
{/foreach}
Now my question is can i avoid the
{assign var=id value=$row->id}{assign var=cid value=$region->id}
Somehow? i tried varios combinations but none were working.
like $region.$row->id
Instead of -> use . and try
{assign var=id value=$row.id}{assign var=cid value=$region.id}

Smarty table even odd

I'm is using PHP smarty templater. I need to create even odd row highlighting.
Please, send me example how to do that.
also I have variable:
$smarty.foreach.product.index
For this kind of situation smarty has method called {cycle}
<table>
{foreach $products as $product}
<tr class="{cycle values="odd,even"}">
<td>{$product.name}</td>
</tr>
{/foreach}
</table>
The result for this will be:
<table>
<tr class="odd">
<td>1st product</td>
</tr>
<tr class="even">
<td>2nd product</td>
</tr>
<tr class="odd">
<td>3rd product</td>
</tr>
</table>
In you stylesheet file define properties for odd and even lines like this:
tr.even td{background: #CCCCCC;}
tr.odd td{background: #EFEFEF;}
<table>
{foreach key=i item=row from=$items}
<tr{if $i%2==1} bgcolor=#e4e4e4{/if}><td>{$i}</td></tr>
{/foreach}
</table>
{section name=myloop start=0 loop=10 step=1}
<tr class="{if $smarty.section.myloop.index is even}tr_even{else}tr_odd{/if}"><td>{$smarty.section.myloop.index}</td></tr>
{/section}

Categories