every 3 and 2 iterations add a div row - php

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>

Related

How to filter a loop with php/smarty?

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}

SMARTY - maximum in section doesn't work as it should

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}

adjusting css with a site using smarty php

I am trying to adjust some boxes on a site using smarty.
three sections populate data to the boxes all three sections share the same class. I am trying to figure out where to go to add a new classes or remove them if necessary but this is gibberish to me.
If someone can make sense of this I would appreciate it.
this file seems to be calling all the classes that makes sense to me. Now how to change them beats me.
classifieds.tpl
{if count($from)}
<div class="classifieds">
<div class="list">
{foreach from=$from item=item key=key name=tmp}
{if $item.featured}<div class="featured">{/if}
<div class="listItemDiv">
<div class="listRowDiv">
{if $item.photo}<div class="image">
<div class="photo-photo">
<div>
{html_image file="$url/photos/`$item.photo`"}
</div>
</div>
</div>{/if}
<div class="title">{$item.title}</div>
<div class="description">{$item.description}</div>
{include file="$theme_template_root/items/actions.tpl"}
</div>
</div>
{if $item.featured}</div>{/if}
{/foreach}
</div></div>
{else}
<div class="centerText">{$language.nothing_found}</div>
{/if}
This file seems to add the heading of each section but then call a function? maybe? To populate the box with the correct data?
<div class="columnSmall">
{if $tip_message}
{include file="boxes/tip.tpl" tip=$tip_message}
{/if}
{include file="boxes/greyTop/open.tpl" secheader=$language.quick_search}
<div class="quickSearchForm">{include file="forms/simple.tpl" form="classifieds_qsearch"}</div>
{include file="boxes/greyTop/close.tpl"}
{if $banners.3}
<div class="banner-side">{banner area="3"}</div>
{/if}
</div>
<div class="columnWide">
{include file="boxes/white/open.tpl"}
<h2>{$language.featured_classifieds}</h2>
{include file="lists/classifieds.tpl" from=$featured.cards}
<h2>{$language.popular_classifieds}</h2>
{include file="lists/classifieds.tpl" from=$popular.cards}
<h2>{$language.new_classifieds}</h2>
{include file="lists/classifieds.tpl" from=$new_classifieds.cards}
{include file="boxes/white/close.tpl"}
</div>

Smarty :Truncate no. of divs in {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.

Smarty modulus?

I'm in a loop to display products...
4 per row, unlimited rows'
I need to know if it's the nth entry... example every 4 items... So I know its the first column as in
item 1, item5, item 9 etc...
Or last item
item 4, item 8, item 12
Tried these where
{foreach from=$sproducts item="product" name="sproducts"}
{counter assign="bobis" name="bobis" }
{if $bobis is div by 4|| $laster ==1}
{if $bobis mod 4 == 0}
{if $bobis !=4 && $bobis !=8 && $bobis != 12}
Any simple way?
If I understand the question right, just put a col- class on your item:
<div class="col-{$bobis mod 4}">...</div>
You should get the following:
<div class="col-1">...</div>
<div class="col-2">...</div>
<div class="col-3">...</div>
<div class="col-4">...</div>
<div class="col-1">...</div>
<div class="col-2">...</div>
...and so on
If you are using tables this is something I pulled from a script I am currently working on and adapted to your code somewhat. You probably would have to make some changes but it somewhat gives you an idea.
<table>
{foreach from=$sproducts item="product" name="sproducts"}
{if $product#first}<tr>{/if}
<td>{$product}</td>
{if $product#last}</tr>
{else}{if $product#iteration is div by 4}</tr><tr>
{/if}
{/if}
{/foreach}
</table>

Categories