I've got a problem with my section loops. I have 3 different section loops and I have to display only 5 results (doesn't matter if first section loop's got 5 results or 2results from first, 1 from second and 2 from last section loop). Max in first loop is set to 5 and inside I add counter to check if there are less than 5 results if there are less results than 5 I start second loop and Now there is a problem when i set max in second loop max-(value of my counter) it ignore this value and always display 5 results from second loop, third section loop is not displayed, that's ok, but second should have max set to 1. Please excuse my bad english. Thank you very much for advice. Have a nice day.
Demo: Upcoming events module on right bottom
Code:
{assign var="counter" value=1}
{section name=ArtCat2 loop=$ArtCat2 max=5}
{assign var="cc" value=$counter++}
CC:{$cc}
<br/>
<br/>
{foreach from=$category item=CAT}
<div>
<h2 class="h2">
<a href="/{$country}/{$lang}/{$CAT.ID}_{$CAT.friendlyTitle}/{$ArtCat2[ArtCat2].ID}_{$ArtCat2[ArtCat2].friendlyTitle}.html?do=article">
{$ArtCat2[ArtCat2].Title}
</a>
</h2>
<span class="small2">
<b>{$ArtCat2[ArtCat2].Date} | {$ArtCat2[ArtCat2].Location}</b>
</span>
{$ArtCat2[ArtCat2].ShortText}
<a class="link3"
href="/{$country}/{$lang}/{$CAT.ID}_{$CAT.friendlyTitle}/{$ArtCat2[ArtCat2].ID}_{$ArtCat2[ArtCat2].friendlyTitle}.html?do=article">
<span class="small2"></span></a>
</div>
<br class="clear"/>
<br/>
{/foreach}
{* Online *}
{/section}
{if $cc >= 5}
{else}
{section name=ArtCat3 loop=$ArtCat3 max=5-$cc}
{assign var="cc2" value=$counter++}
CC: {$cc}
<br/>
<br/>
CC2:{$cc2}
<br/>
<br/>
{foreach from=$category item=CAT}
<div>
<h2 class="h2">
<a href="/{$country}/{$lang}/{$CAT.ID}_{$CAT.friendlyTitle}/{$ArtCat3[ArtCat3].ID}_{$ArtCat3[ArtCat3].friendlyTitle}.html?do=article">
{$ArtCat3[ArtCat3].Title}
</a>
</h2>
<span class="small2">
<b>{$ArtCat3[ArtCat3].Date} | {$ArtCat3[ArtCat3].Location}</b>
</span>
<br/>
{$ArtCat3[ArtCat3].ShortText}
<a class="link3"
href="/{$country}/{$lang}/{$CAT.ID}_{$CAT.friendlyTitle}/{$ArtCat3[ArtCat3].ID}_{$ArtCat3[ArtCat3].friendlyTitle}.html?do=article">
<span class="small2"></span></a>
</div>
<br class="clear"/>
<br/>
{/foreach}
{/section}
{/if}
{* International *}
{if $cc+$cc2 >= 5}
{else}
{section name=ArtCat4 loop=$ArtCat4 max=5-$cc+$cc2}
{foreach from=$category item=CAT}
<div>
<h2 class="h2">
<a href="/{$country}/{$lang}/{$CAT.ID}_{$CAT.friendlyTitle}/{$ArtCat4[ArtCat4].ID}_{$ArtCat4[ArtCat4].friendlyTitle}.html?do=article">
{$ArtCat4[ArtCat4].Title}
</a>
</h2>
<span class="small2">
<b>{$ArtCat4[ArtCat4].Date} | {$ArtCat4[ArtCat4].Location}</b>
</span>
<br/>
{$ArtCat4[ArtCat4].ShortText}
<a class="link3"
href="/{$country}/{$lang}/{$CAT.ID}_{$CAT.friendlyTitle}/{$ArtCat4[ArtCat4].ID}_{$ArtCat4[ArtCat4].friendlyTitle}.html?do=article">
<span class="small2"></span></a>
</div>
<br class="clear"/>
<br/>
{/foreach}
{/section}
{/if}
I cannot at the moment test it, but I think the problem is in that line:
{section name=ArtCat4 loop=$ArtCat4 max=5-$cc+$cc2}
It should be rather:
{section name=ArtCat4 loop=$ArtCat4 max=5-$cc-$cc2}
because earlier you check condition {if $cc+$cc2 >= 5}
Related
I am blocking on PHP / Smarty logic.
I am looking to do from an array of products this:
<div class="products" itemscope="" itemtype="http://schema.org/ItemList">
<div class="row">
<article></article>
<article></article>
<article></article>
</div>
<div class="row">
<article></article>
<article></article>
</div>
<div class="row">
<article></article>
<article></article>
<article></article>
</div>
<div class="row">
<article></article>
<article></article>
</div>
....
</div>
I would like to have the possibility of creating a row of 3 lines then a row of 2 lines etc ...
This is what my smarty looks like:
<div class="products{if !empty($cssClass)} {$cssClass}{/if}" itemscope itemtype="http://schema.org/ItemList">
{foreach name="products" from=$products item="product" key="position"}
{include file="catalog/_partials/miniatures/product.tpl" product=$product position=$position}
{/foreach}
</div>
Do you have any idea how I could do this please?
Thanks for your help.
You probably want to control the row opening/closing and column counting in the foreach loop. You don't want that kind of logic in a template dedicated to displaying a single item.
To do this, we need a variable to determine how many columns to display - we'll toggle this from 2 to 3 for each row. We will also need a counter that we can reset at the end of each row to keep track of how many columns have been rendered within the current row. Lastly, we need to make sure we close the current row when we reach the end of the products array regardless of the column count.
<div class="products{if !empty($cssClass)} {$cssClass}{/if}" itemscope itemtype="http://schema.org/ItemList">
{* Assign a column limit *}
{assign var=colLimit value=3}
{* Create a counter for our columns *}
{counter start=0 name=colCounter assign=colCount print=false}
{foreach name="products" from=$products item="product" key="position"}
{* Open a row when the column count is zero *}
{if $colCount==0}
<div class="row">
{/if}
{* Increment the counter *}
{counter name=colCounter}
{*
Include the product template.
We can either pass the column count or $product#index/$product#iteration to the position arg,
or this may not be needed after implementing column logic in the loop
*}
{include file="catalog/_partials/miniatures/product.tpl" product=$product position=$colCount}
{* If this is the last column to display in the row or the last product in the array, close the row *}
{if $colCount==$colLimit || $product#last}
</div>
{* Toggle the column limit *}
{if $colLimit==3}
{assign var=colLimit value=2}
{else}
{assign var=colLimit value=3}
{/if}
{* Reset the counter *}
{counter start=0 name=colCounter assign=colCount print=false}
{/if}
{/foreach}
</div>
This code shows me all selections I have made.
How can I filter this and only show the selection of a specific Group?
For example {if $sConfigurator.groupID == 113}
{$configurator = $sArticle.sConfigurator}
{foreach $configurator as $configuratorGroup}
{foreach $configuratorGroup.values as $option}
{if $option.selected}
<div class="selected">
<div class="group">{$configuratorGroup.groupname}</div>
<div class="option">{$option.optionname}</div>
</div>
{/if}
{/foreach}
{/foreach}
I'm also not sure what you mean or what configurator your code is about...
Maybe just add the condition to the if?
{if $option.selected && $configuratorGroup.id == 113}
<div class="selected">
<div class="group">{$configuratorGroup.groupname}</div>
<div class="option">{$option.optionname}</div>
</div>
{/if}
I have a array of images in order:
1-3 are main images.
3+ are thumbnails.
Q) How can I split the thumbs on 2 lines.
E.g:
<div class="main-images">
{section name="i" loop=$images.rows}
{assign var="rows" value=$images.rows[i]}
{if $smarty.section.i.index <= 2}
<img />
{/if}
{/section}
</div>
<div class="thumbs-images">
{math assign=thumbs_count equation="total - other" total=$images.rowcount other=3}
{section name="i" loop=$images.rows}
{assign var="rows" value=$images.rows[i]}
{if $smarty.section.i.index >= 3}
<img />
{/if}
{/section}
</div>
So I need to add something {if $thumbs_count >= $smarty.section.i.index}<div style="clear:both" />{/if} so this appears half way through the second loop.
Managed to figure this out.
{math assign="total_row_split" equation="floor((total - main) / division)" total=$images.rows|#count division=2 main=3}
{counter start=0 print=false assign="thumbs_count"}
{section name="i" loop=$images.rows}
{assign var="rows" value=$images.rows[i]}
{if $smarty.section.i.index >= 3}
{if $thumbs_count == $total_row_split}<br style="clear:both" />{/if}
<a href="{$HOME}/get/image{$rows.filename.fvalue}" rel="fancy" title="{$rows.title.value|default:$product.name.fvalue}">
<img src="{$HOME}/get/image/120{$rows.filename.fvalue}" alt="{$rows.title.value|default:$product.name.fvalue} Picture" />
</a>
{counter print=false}
{/if}
{/section}
I have the categories & subcategories. I want to select the all parent categories & subcategories. But want to show only 3-4 subcategories under each parent category.
My Php Code
/******Start Categories********/
$ca=mysql_query('select * from category where parent_id=0');
while($ca1 = mysql_fetch_array($ca))
{
$ca2[]=$ca1;
}
$smarty->assign('ca2',$ca2);
/******End Categories********/
/******Start SubCategories********/
$sub=mysql_query('select * from category where parent_id!=0 ');
while($sub1 = mysql_fetch_array($sub))
{
$sub2[]=$sub1;
}
$smarty->assign('sub2',$sub2);
/******End SubCategories********/
And Tpl Code:
<div id="sub_ltcol">
{section name=loopc loop=$ca2}
<div id="gr_design">
<div id="gr_head">
<h4>{$ca2[loopc].category_name}</h4>
</div>
{section name=loops loop=$sub2}
{if $ca2[loopc].category_id eq $sub2[loops].parent_id}
<div id="gr_body">
<ul>
<li>
<div class="arw_icon"><img src="images/arw_icon.png"></div>
{$sub2[loops].category_name}
</li>
<div class="dotted_line"></div>
</ul>
</div>{/if}
{/section}
</div>
{/section}
</div>
I want to show the <li> tag only 3-4 times.
You can try
{section name=loops loop=$sub2}
{if $ca2[loopc].category_id eq $sub2[loops].parent_id AND $smarty.section.loops.iteration <= 4}
...
{/if}
{/section}
You can use this method:
<div id="sub_ltcol">
{section name=loopc loop=$ca2}
<div id="gr_design">
<div id="gr_head">
<h4>{$ca2[loopc].category_name}</h4>
</div>
{section name=loops loop=$sub2}
{if $ca2[loopc].category_id eq $sub2[loops].parent_id}
{if $smarty.foreach.loops.index lte 3}
<div id="gr_body">
<ul>
<li>
<div class="arw_icon"><img src="images/arw_icon.png"></div>
{$sub2[loops].category_name}
</li>
<div class="dotted_line"></div>
</ul>
</div>{/if}
{/if}
{/section}
</div>
{/section}
</div>
if you will require to show only 3 subcategories, take only this much from DB and show it.
you can use limit attribute for same
/******Start SubCategories********/
$sub=mysql_query('select * from category where parent_id!=0 limit 3');
it help to improve page loading performance also.
<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.