Referencing array in smarty - php

I am trying to reference an array through a function I am calling
$bio['results'] = $db->query(sprintf('SELECT * FROM `athlete_bio_results` WHERE `PersonID` = %d ORDER BY `athlete_bio_results`.`Type` DESC, `athlete_bio_results`.`Date` DESC;', $id));
$smarty->assign(value($params, 'to', 'athlete'), $bio);
In my view I am referencing through a foreach loop some of the fields through this array.
How would I reference a field outside of this foreach loop for checking..For instance in the foreach loop I can go {$result.Type) which will bring up everything in the db under that field. I want to check for a certain type before the foreach loop. My problem is referencing that piece of data outside the foreach loop. How would I achieve this? Thanks!
{if $athlete.results['Type'] == 'National'} <---- This is not working for me.
{foreach $athlete.results as $result}
{$result.Date|substr:0:-6}
{$result.Event} -
{$result.Result}
{if $result.Junior == 'no' || $result.Junior == ''}
{else}
{'(Jr. Div.)'}
{/if}
<br />
{/foreach}
{/if}
EDIT
This is the code that works
{foreach $athlete.results as $result}
{if $result['Type'] == 'National'}
{$result.Date|substr:0:-6}
{$result.Event} -
{$result.Result}
{if $result.Junior == 'no' || $result.Junior == ''}
{else}
{'(Jr. Div.)'}
{/if}
<br />
{/if}
{/foreach}
Thanks for your help in guiding me to the right solution :)

Since you're accessing the first element of the array, use the key [0]:
{if $athlete.results[0]['Type'] == 'National'}
Actually I'm not certain that will work. If it doesn't, try this syntax instead.
{if $athlete.results[0].Type == 'National'}
EDIT
Should you be checking Type inside the foreach?
{foreach $athlete.results as $result}
{* Check inside the loop... *}
{if $result['Type'] == 'National'}
{$result.Date|substr:0:-6}
{$result.Event} -
{$result.Result}
{if $result.Junior == 'no' || $result.Junior == ''}
{else}
{'(Jr. Div.)'}
{/if}
<br />
{/if}
{/foreach}

Related

Symfony If Statements

I am hoping to get some assistance with Symfony 'if' statements.
Essentially I am trying to do, if the price value is equal to or greater than $500, display 'Free Shipping', if otherwise, display 'Click & Collect'.
This is a for a prestashop theme and I have the below coding after the initial if statement
{else}
{if $pricediplay ==> 500} {l s='Free Shipping!'}{/if}
{else}
{if $pricediplay ==< 499.99} {l s='Click & Collect'}{/if}
{/if}
The whole coding of this part is:
{if $option.total_price_with_tax && !$option.is_free &&
(!isset($free_shipping) || (isset($free_shipping) && !$free_shipping))}
{if $use_taxes == 1}
{if $priceDisplay == 1}
{convertPrice price=$option.total_price_without_tax}
{if $display_tax_label}
{l s='(tax excl.)'}
{/if}
{else}
{convertPrice price=$option.total_price_with_tax}
{if $display_tax_label}
{l s='(tax incl.)'}
{/if}
{/if}
{else}
{convertPrice price=$option.total_price_without_tax}
{/if}
{else}
{if $pricediplay ==> 500}
{l s='Free Shipping!'}
{/if}
{else}
{if $pricediplay ==< 499.99}
{l s='Click & Collect'}
{/if}
{/if}
Any help would be greatly appreciated.
Thanks,
Cohen
It's hard to tell from your posting what you're trying to achieve. i suspect the changes you want might be something like this:
{if $option.total_price_with_tax && !$option.is_free &&
(!isset($free_shipping) || (isset($free_shipping) && !$free_shipping))}
{if $use_taxes == 1}
{if $priceDisplay == 1}
{convertPrice price=$option.total_price_without_tax}
{if $display_tax_label}
{l s='(tax excl.)'}
{/if}
{else}
{convertPrice price=$option.total_price_with_tax}
{if $display_tax_label}
{l s='(tax incl.)'}
{/if}
{else}
{convertPrice price=$option.total_price_without_tax}
{/if}
{/if}
{else}
{if $priceDisplay >= 500}
{l s='Free Shipping!'}
{/if}
{else}
{if $priceDisplay <= 499.99}
{l s='Click & Collect'}
{/if}
{/if}
In smarty the comparison operators are like in many languages >= and <=. => has a different meaning in PHP, and smarty is translated into PHP before execution.
If it is supposed to be a template using twig if statements must be written as follow:
{% if ... %}
//your own logic...
{% endif %}
You can use both :
{% elseif ... %}
{% else %}
to make different cases.
You can set variables like this:
{% set var='toto' %}
and display a variable like this:
{{ var }}
And finally comparison operators are written as they are pronounced:
greater than or equal : >=
less than or equal : <=

Prestashop $category variable doesn't work within another if

I use Prestashop and I need to add a specific slideshow for the category->id 1 on the top of the category's products, and it's need to be within the code below:
I tried to insert this code:
{if category->id == '1'}
...
{/if}
Within this code:
{if isset($category)}
{if $category->id AND $category->active}
{if $scenes || $category->description || $category->id_image}
...
{if $scenes}
...
{include file="$tpl_dir./scenes.tpl" scenes=$scenes}
{if $category->description}
...
{if Tools::strlen($category->description) > 350}
...
{else}
{$category->description}
{/if}
...
{/if}
...
{else}
THERE'S WHERE I TRIED INSERT THE CODE
{/if}
{/if}
{/if}
{/if}
And it doesn't work.. why?
Regards,
Because you have placed in wrong position :)
{if isset($category)}
{if $category->id AND $category->active}
{if $scenes || $category->description || $category->id_image}
...
{if $scenes}
...
{include file="$tpl_dir./scenes.tpl" scenes=$scenes}
{if $category->description}
...
{if Tools::strlen($category->description) > 350}
...
{else}
{$category->description}
{/if}
...
{/if}
...
{else}
THERE'S WHERE I TRIED INSERT THE CODE (WRONG)
{/if}
{/if}
THERE'S WHERE YOU HAVE TO PLACE THE CODE
{if $category->id eq 1}
/* some stuff */
{/if}
{/if}
{/if}
For the next time fix the indentation and you reach the goal by yourself ;)
Insert it before the last
{else}

Smarty foreach counter , reset after 3 element

I want to create foreach smarty loop with counter and 3 "if" conditions inside.
After my counter value is more than 3 I want to reset counter value and get back to first condition of If
This is my code
{foreach $itemscollection as $singleitem name=smartyloop}
{assign var="counter" value=$smarty.foreach.smartyloop.iteration}
{if $counter == 1}
<h1>I am the one</h1>
{/if}
{if $counter == 2}
<h1>I am second</h1>
{/if}
{if $counter == 3}
<h1>I am third</h1>
{/if}
{if $counter > 3}
{$counter = 1}
{/if]
{/foreach}
So for example If I have 4 elements to place into foreach output should look like
I am the one
I am second
I am third
I am the one
Now it's not working and i don't know why.
Can somebody please help me and tell how to resolve that problem ?
{assign var=counter value=1}
{foreach $itemscollection as $singleitem name=smartyloop}
{if $counter == 1}
<h1>I am the one</h1>
{/if}
{if $counter == 2}
<h1>I am second</h1>
{/if}
{if $counter == 3}
<h1>I am third</h1>
{/if}
{if $counter > 3}
{assign var=counter value=1}
{/if]
{$counter++}
{/foreach}
this might work
I know this is an old one but try to use counter as a custom function from smarty:
{foreach $itemscollection as $singleitem name=smartyloop}
{counter assign='pos'} {* assign counter to variable *}
{if $pos == 1}
<h1>I am the one</h1>
{/if}
{if $pos == 2}
<h1>I am second</h1>
{/if}
{if $pos == 3}
<h1>I am third</h1>
{counter start=0} {*reset counter*}
{/if}
{/foreach}
Had to go back to CMS Made Simple to make something related ;)
Try like
{if $counter%3 eq 0 && $counter gt 2}
{assign var=counter value=1}
{/if}
You can use {cycle} http://www.smarty.net/docs/en/language.function.cycle.tpl
{cycle name="counter" values="1,2,3"}
Or you could use the Modulus(%) operator http://php.net/manual/en/language.operators.arithmetic.php
{$counter = ($smarty.foreach.smartyloop.iteration % 3) + 1}
This solution is working
I dont't know why , maybe foreach checks conditions from last to first .
{foreach $blogscollection as $singleblog name=smartyloop}
{assign var="counter" value=$smarty.foreach.smartyloop.iteration}
{if $counter>3}
{assign var=counter value=1}
{/if}
{if $counter == 1}
<h1>I am the one</h1>
{/if}
{if $counter == 2}
<h1>I am the second</h1>
{/if}
{if $counter == 3}
<h1>I am the third</h1>
{/if}
{/foreach}
<?php
$array=array(' the ','hap',' pop' ,' grand'); // assume this is your array to pass in for each
$says=array(' the one','second',' third'); // store your word to appear
$count=0;
foreach ($array as $arr){
if($count == 3) $count=0;
print "<h1>I am $says[$count]<h1>";
$count++;
}
?>

Delete first element after if statement

Need to delete first element after all this statements, i try to add key=i to second foreach, and add {if $i!=0}, but it dont work for me, because i have multilevel system and after all if statements first $i is different for all pages.
{foreach from=$page.path item=e}
{assign var=element value=0}
{foreach from=$menu3 item=r}
{if $e.page_id==$r.page_id}{assign var=element value=$r}{/if}
{if $element._left < $r._left && $element._right > $r._right}
{if $r._level==$element._level+1 && $r._level==$page._level}
{$r.name}
{/if}
{/if}
{/foreach}
{/foreach}
P.S. Sorry for my English.
Add a name to you foreach and do something like this to get index
{foreach from=$items key=myId item=i name=foo}
{$smarty.foreach.foo.index} <!-- It will show index -->
{/foreach}
I assume you don't want to display first element in each of the menus you have, right?
{foreach from=$page.path item=e}
{assign var=first_skipped value=0}
{assign var=element value=0}
{foreach from=$menu3 item=r}
{if !$first_skipped}
{assign var=first_skipped value=1}
{else}
{if $e.page_id==$r.page_id}{assign var=element value=$r}{/if}
{if $element._left < $r._left && $element._right > $r._right}
{if $r._level==$element._level+1 && $r._level==$page._level}
{$r.name}
{/if}
{/if}
{/if}
{/foreach}
{/foreach}

Smarty how to get a first index from foreach?

Construction is this:
<!-- projects list -->
{if !empty($userObjects)}
<select id="projects-list" tabindex="1" name="project">
{if !isset($selected)}<option value="0">Choose project</option>{/if}
{foreach from=$userObjects item=v}
<option value="{$v.Id}" {if $selected==$v.Id}selected="selected"{/if} }>{$v.Name}
{* if it's 1st element *}
{if $smarty.foreach.v.index == 0}
{if isset($limit)}<br /><span id="projlimit">{$limit}</span> {$currency->sign}{/if}
{/if}
</option>
{/foreach}
</select>
as you can see I did
{if $smarty.foreach.v.index == 0}
but it's going wrong. In this case all the options elemets has a $limit value. How to make it good? I need only first one.
I don't want to appear rude, but Bondye's answer will not work in all cases. Since PHP's arrays are ordered maps, the value of the first key will not always be 0.
In these cases you can use the #index, #iteration or #first properties. More details are in the smarty foreach documentation at http://www.smarty.net/docs/en/language.function.foreach.tpl#foreach.property.iteration
One of the possible solutions to your question is bellow:
{foreach $rows as $row}
{if $row#iteration == 1}
First item in my array
{/if}
{/foreach}
You can use this code:
{foreach from=$userObjects item=v name=obj}
{if $smarty.foreach.obj.first}
This is the first item
{/if}
{if $smarty.foreach.obj.last}
This is the last item.
{/if}
{/foreach}
Could you do this by the array key?
{foreach from=$rows key=i item=row}
{if $i == 0}
First item in my array
{/if}
{/foreach}

Categories