Smarty modulus? - php

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>

Related

every 3 and 2 iterations add a div row

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>

module product tab slider of prestashop

This is items.tpl module of Prestashop CMS, it shows random productes in home page, i need to not show products quantity =0.
sry for my bad English.
Thanks for help guys.
{if isset($products) && $products}
<div class="{if isset($SNSPRT_EFFECT)}{$SNSPRT_EFFECT}{/if} product_list products-grid grid {if isset($class) && $class} {$class}{/if}">
{if isset($ajax_start) && $ajax_start}
{assign var='nbstart' value=$ajax_start}
{else}
{assign var='nbstart' value=0}
{/if}
{counter start=$nbstart skip=1 print=false name=i assign="i"}
{foreach from=$products item=product name=products}
<div class="ajax_block_product item item-animate{if isset($item_class) && $item_class} {$item_class}{/if}">
{counter name=i}
{include file="$tpl_dir./product-blockgrid.tpl"}
</div>
{if $i % $SNSPRT_XS == 0}<div class="clearfix visible-xs"></div>{/if}
{if $i % $SNSPRT_SM == 0}<div class="clearfix visible-sm"></div>{/if}
{if $i % $SNSPRT_MD == 0}<div class="clearfix visible-md"></div>{/if}
{if $i % $SNSPRT_LG == 0}<div class="clearfix visible-lg"></div>{/if}
{/foreach}
</div>
{addJsDefL name=min_item}{l s='Please select at least one product' js=1}{/addJsDefL}
{addJsDefL name=max_item}{l s='You cannot add more than %d product(s) to the product comparison' sprintf=$comparator_max_item js=1}{/addJsDefL}
{addJsDef comparator_max_item=$comparator_max_item}
{addJsDef comparedProductsIds=$compared_products}
{/if}
Your problem is not here in TPL file. It is on PHP associated module file. You should look for module displaying products in home page (like blockbestsellers or blocknewproducts) and look for main PHP module file. Inside you should find the MySQL request. For example from blocknewproducts:
protected function getNewProducts()
{
if (!Configuration::get('NEW_PRODUCTS_NBR'))
return;
$newProducts = false;
if (Configuration::get('PS_NB_DAYS_NEW_PRODUCT'))
$newProducts = Product::getNewProducts((int) $this->context->language->id, 0, (int)Configuration::get('NEW_PRODUCTS_NBR'));
if (!$newProducts && Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY'))
return;
return $newProducts;
}
You should change this $newProducts = Product::getNewProducts((int) $this->context->language->id, 0, (int)Configuration::get('NEW_PRODUCTS_NBR')); for your own MySQL sentence keeping original behavior but avoiding random.
Good luck

PHP : How to use smarty section for the following output?

I am using the even and odd logic for that but not getting the output as i need
for example i have array of
$data = array(1000,1001,1002, 1003,1004,1005);
$smarty->assign('data',$data);
{section name=i loop=$data}
{section}
so the output i need is :
<div>
<dl>1000</dl>
<dl>1001</dl>
</div>
<div>
<dl>1002</dl>
<dl>1003</dl>
</div>
<div>
<dl>1004</dl>
<dl>1005</dl>
</div>
According to offical document http://www.smarty.net/docsv2/en/language.function.section.tpl
Try following codes:
<div>
{section name=i loop=$data}
<dl>{$data[i]}</dl>
{if $smarty.section.data.index > 0 && $smarty.section.data.index % 2 == 0 && $smarty.section.data.index < $smarty.section.customer.total -1}
</div><div>
{/if}
{/section}
</div>
You should do it this way:
{section name=i loop=$data}
{if $smarty.section.i.index %2 == 0}
<div>
{/if}
<dl>{$data[i]}</dl>
{if $smarty.section.i.index %2 == 1 || $smarty.section.i.last}
</div>
{/if}
{/section}
<div> should be rather created in section because when there weren't be any items you probably wouldn't like to create empty <div>. Last condition $smarty.section.i.last is added in case you have for example 5 elements and in that case you of course make sure your div is closed.

Smarty - How can i set a max ( 5 ) pages to show on pagination from a calling query

Wondering how to set a max number of results from a query, for example if there is more than 10 result pages display 1,2,3,4,5...and last (10) and so on.. I am using smarty and could not figure how to make work.
<!-- .pagination starts -->
<div class="pagination-wrapper1">
<ul class="pagination">
{if $active_page eq $smarty.section.page_num.index+1}
<li class="previous-off"><i class="icon-arrow-left"></i> Previous</li>
{else}
<li class="previous-on"><i class="icon-arrow-left"></i> Previous</li>
{/if}
{section name="page_num" loop=$pages}
{if $active_page eq $smarty.section.page_num.index+1}
<li class="active">{$smarty.section.page_num.index+1}</li>
{else}
<li>{$smarty.section.page_num.index+1}</li>
{/if}
{/section}
{if $active_page eq $smarty.section.page_num.index}
<li class="next-off">Next <i class="icon-arrow-right"></i></li>
{else}
<li class="next">Next <i class="icon-arrow-right"></i></li>
{/if}
<div class="clear"></div>
</ul>
</div><!-- /.pagination ends -->
This is how my pagination works currently:
And this is an example of how should be...hope someone can help.
I dont know about smarty. I think its some sort of template engine.
But i can give you the idea how to do it with php.
so in the pagination you will always know which page you are currently in. So in this case you are in page 39.
what you can do is run two loops, one decrementing the value of current page 5 times like
38 37 36 35 34 in the page
the other one will increment current page by 5
40 41 42 43 44 in the same page
so your page will echo these in the same div so you will end up with the pagination showing aboe.
ofcourse you need to do checks to make sure while decrementing the value doesnt go below 1
and while incrementing the value doent go above maximum page.
Hope this helps you.
OR
You can run a single loop starting at current_page -5 and going all the way to current_page+5
I am sure you can adjust the logic to show 5 pages instead of 10

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