Nested condition resulting empty page in smarty - php

I am trying to filter out the prices with a nested foreach and if statement in smarty, but whenever i use if statement inside foreach loop it results in blank page.
Here is code causing issue
{foreach $value.Regstration as $price}
{if($price.price gt 0)}
<br>{var_dump($price.price)} <br>
{/if}
{/foreach}
Even i tried with this
{foreach $value.Regstration as $price}
{if(1==1)}
<br>{var_dump($price.price)} <br>
{/if}
{/foreach}
This is also returning blank page
But without if statement i am getting result like this
float(14.95)
float(-1)
float(-1)
float(-1)
So can anyone tell me where i am doing wrong or if not can anyone tell me how can i filter the prices equal to or lower then zero.

Try this:
{foreach $value.Regstration as $price}
{if 1 == 1}
<!--
For debug:
{$price|var_dump}
-->
<br>{$price.price|var_dump}<br>
{/if}
{/foreach}

Related

issue with a smarty nested foreach loop

The variable I am stating with is a string of HTML code. I am trying to explode on "line break", then explode the result on a "|".
here is the code I am using
{assign var="featuresdescarray" value="<br />"|explode:$product.featuresdesc}
{foreach from=$featuresdescarray key=k item=i}
{assign var="featuresdescarrayexploded" value="|"|explode:$i}
{foreach from=$featuresdescarrayexploded key=key item=item}
{if $item eq "yes"}
yes-textreplaced
{elseif $item eq "no"}
no-textreplaced
{else}
{$item}<br />
{/if}
{/foreach}
{/foreach}
So the fist time through the nested foreach the text gets replaced as I expect. after that it seems to fail the if, elseif and just use the else every time.
If i take out the if statement and put in a {$key} : {$item} line it looks like it should work.
Not sure what I am missing
OK I am not sure why this works and the original code does not but here is what is working for anyone trying something like this.
I took out the yes and no and replaced it with 1 and 0
{assign var="featuresdescarray" value="<br />"|explode:$product.featuresdesc}
{foreach from=$featuresdescarray key=k item=i}
{if $i|strstr:"|"}
{assign var="featuresdescarrayexploded" value="|"|explode:$i}
{foreach from=$featuresdescarrayexploded key=key item=item}
{if $key eq 0 and $item eq 1}
<div class="productFeatureDescYes">{$featuresdescarrayexploded[1]}<br /></div>
{elseif $key eq 0 and $item eq 0}
<div class="productFeatureDescNo">{$featuresdescarrayexploded[1]}<br /></div>
{/if}
{/foreach}
{else}
{$i}<br />
{/if}
{/foreach}
I added a if statement to search the variable $i for a "|" if it has one then it is exploded and wrapped in the proper div. if not it is just displayed (to be wrapped in a div later).
I also run the if statement on the key[0] section in the second foreach statement.
The danger of self taught coding i guess, sometime it is just trial and error but I always like to find out what i did wrong and this time it is not the case.
I am not sure why the 0's and 1's worked better then "no" and "yes" so if anyone can answer the original question that would be great.

In if statement double use $ (smarty)

Question about smarty if statement.
I have create a counter and i need the value of the counter in a if statement.
The counter looks like this:
{assign var="counter" value=1}
It looks like this:
{if $task.check_counter == "on"} checked {/if}
It have to looks like:
{if $task.check_1 == "on"} checked {/if}
Is this possible?
UPDATE:
Part of the code:
{assign var="counter" value=1}
{foreach from=$service item=sert}
{if $sert.parent_id == $task.task_id}
<tr>
<td>{$sert.name}</td>
<td>{$address_detail.start}</td>
<td style="text-align: center;">
<div>
<input type="checkbox"
class="icheck" name="c_{$counter}"
id="icheck_{$counter}"
{if $task.check_$counter == "on"} checked {/if} >
</div>
</td>
</tr>
{assign var="counter" value=$counter + 1}
{/if}
{/foreach}
99 times out of 100 where you are using "variable variables", you should actually be defining an array.
If instead of dynamically naming your items as check_1, check_2, etc, you defined an array of items called check_list, you could just do this: {$task.check_list.$counter}
Note that you can even do this with form fields, as PHP will turn fields like <input name="task[check][1]" /> into appropriate arrays when it processes the form submission.
That said, since this particular case isn't actually a variable name but an array key within the $task array, you can define the whole key as a dynamic string, and look up using that:
{assign var=array_key value='check_'|cat:$counter}
{if $task.$array_key == "on"}
You could try this;
Assign your smarty variable like;
$smarty->assign('counter', 1);
Then try this
{if $task.check_$smarty->get_template_vars('counter') == "on"} checked {/if}

Using smaryt to set a last class for every 4 results not working as expected

I have been reading up on best method in smarty to set every 4th item in the loop a class, and came across the use of "interation" however the example code below is kind of working but it is apply the class to everything after the 4th one and im wanting to apply a last class on every 4th one. Is this possible using this method?
{foreach from=$product.image_pairs item="image_pair" name="additional_images"}
{if $image_pair}
{if $image_pair.image_id == 0}
{assign var="img_id" value=$image_pair.detailed_id}
{else}
{assign var="img_id" value=$image_pair.image_id}
{/if}
{if $smarty.foreach.additional_images.iteration is div by 4}
{assign var="last_image" value="last"}
{/if}
{include file="common_templates/image.tpl" images=$image_pair object_type="detailed_product" link_class="cm-thumbnails-mini $last_image" image_width=$th_size image_height=$th_size show_thumbnail="Y" show_detailed_link=false obj_id="`$preview_id`_`$img_id`_mini" make_box=true wrap_image=true}
{/if}
{/foreach}
Result:
<a class="">content</a>
<a class="">content</a>
<a class="">content</a>
<a class="">content</a>
<a class="last">content</a>
<a class="last">content</a>
<a class="last">content</a>
{assign var="last_image" value="last"}
Will assign this variable on 4th iteration, and this will prevail for next iterations. You should add else and assign empty string to this variable.

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}

Arrays in smarty?

<div id="favorite-first" class="">
{foreach from=$arrSection key=k item=v}
{if $k==$selectedSection}
{$v}
{/if}
{/foreach}
</div>
<div id="favorite-toggle"><br></div>
<div id="favorite-inside" class="slideUp">
{foreach from=$arrSection key=k item=v}
{if $k==$selectedSection}
{else}
<div class="favorite-action" id="{$k}">{$v}</div>
{/if}
{/foreach}
</div>
If $arrSection array returns only one value (i.e $k). I need to hide div (favorite-toggle,favorite-inside) How can i do this in smarty
If $arrSection array returns only one value
{if count($arrSection) eq 1}
there is only one item
{else}
there is > one or zero items
{/if}
If you give the "foreach" a name argument you can access certain foreach properties:
{foreach from=$arrSection key=k item=v name=NAME}
Let's say the number of iterations in total:
$smarty.foreach.NAME.total
Then you now how often smarty will loop and if it's in your case only one time.

Categories