Page 1 of 1

x results per row in Smarty

Posted: Sat Nov 18, 2006 2:07 pm
by DaveTheAve
Alright, this might be a little hard to explan so I'll use a quick ASCII diagram:

This is what is currently prints:
x
x
x
x
x
x
x

This is how I wish for it to be:
x x x
x x x
x x x
x x x
x x x
(Where the number of x's per row can be manually set)

Is there anyway in smarty to do this? Here is my current smarty code:

Code: Select all

{section name=i loop=$categorys}
	<tr onclick="location.href='{$url_prefix}cats.php?action=view_cat&cat_id={$categorys[i].cat_id}'" onmouseover="style.backgroundColor='#FF0';" onmouseout="style.backgroundColor='transparent';">
		<td><b>{$categorys[i].name}</b></td>
		<td>{$categorys[i].description}</td>
	</tr>
{/section}

Posted: Sat Nov 18, 2006 5:44 pm
by Ambush Commander
Find out more about the modulus operator.

Posted: Sat Nov 18, 2006 6:03 pm
by DaveTheAve
lol, I thought this whole thing would revolve around the %. But that still leaves me where I started. Before I posted this I researched that operator and was lost about how that applies to Smarty. In short, I'm still completely lost.

Posted: Wed Nov 22, 2006 3:17 pm
by DaveTheAve
Alright so, It's been a few days, I've looked into modulus a little more (Got sidetracked and implemented pagination) and i'm still lost about this. Any help? Anyone?

Posted: Wed Nov 22, 2006 3:36 pm
by Ambush Commander
The modulus operator returns the remainder when you divide two integers. Ex. 5 % 3 = 2, 4 % 2 = 0. What this means is that you guarantee the result of $int % $divisor to be an integer from 0 to $divisor - 1. When it equals zero, you can use it to initialize a new row.

Posted: Wed Nov 22, 2006 6:34 pm
by DaveTheAve

Code: Select all

<tr>
<td id='all_cats' onclick="location.href='{$url_prefix}categorys.php?action=view_cat&cat_id=0'">
	<b>All Categorys</b>
</td>
{section name=i loop=$categorys}
	{if ($smarty.section.i.index+1)%5 == 0}
		<tr>
	{/if}
		<td id='{$categorys[i].type}' onclick="location.href='{$url_prefix}categorys.php?action=view_cat&cat_id={$categorys[i].cat_id}'">
			<b>{$categorys[i].title}</b>
		</td>
	{if $smarty.section.i.index_next+1%5 == 0 || $smarty.section.i.last == 1}
		</tr>
	{/if}
{/section}
That seems to work, notice any flaws?

Posted: Thu Nov 23, 2006 4:43 am
by jmut
Something like this should do the trick.
not tested.
Have not checked on your solution, might be as working as this one.

Code: Select all

<tr>
{foreach...}
some <td>
                    <!-- {counter assign="cntr"} -->
                    {if $cntr % 3 eq 0}
                    </tr><tr>  //go to new row
                    {/if}
{/foreach}
</tr>