Smarty foreach counter , reset after 3 element - php

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++;
}
?>

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}

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 nested if condition is not working properly?

I have written my code like this,
{if $quant eq 1}
{if $val neq ""}
.....//some code
{else}
.....//some code
{/if}
{else if $quant eq 0}
.....//some code
{/if}
but the above nested smarty if condition is not working as expected and it always give the results in else condition.Can anyone help me please, Don't know where am making mistake...
In smarty you have to write if else condition like that:
{if $quant eq 1}
{elseif $val neq ""}
.....//some code
{elseif $val neq "3"}
.....//some code
{elseif $quant eq 0}
.....//some code
{/if}
OR
{if $quant eq 1}
{if $val neq ""}
.....//some code
{else}
.....//some code
{/if}
{else}
{if $quant eq 0}
.....//some code
{/if}
.....//some code
{/if}
I hope this would help you.

Referencing array in smarty

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}

Categories