x results per row in Smarty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

x results per row in Smarty

Post 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}
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Find out more about the modulus operator.
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post 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.
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post 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?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post 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?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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>
Post Reply