Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Dynamic table columns -->yet another way<--

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
iDİlE
Smarty Rookie


Joined: 18 Aug 2004
Posts: 9

PostPosted: Thu Aug 26, 2004 2:29 am    Post subject: Dynamic table columns -->yet another way<-- Reply with quote

Well I ran into a prob when using {section} and adding html inside it with text and images. Thanks to a good friend helping there is yet another way.
First MYSql array with DB:
Code:

#assign number of rows
$cols_per_row = 3
$results = "SELECT * FROM your_table WHERE  your where
$res = $db->query($results);
    while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
   $array[] = $row;
   }
$smarty->assign('cols_per_row', $cols_per_row);
$smarty->assign("array", $array);


Now your corresponding .tpl:
Code:

<table>
{section name=sec1 loop=$array}
{if %sec1.index% % $cols_per_row == 0}
<tr>
{/if}
<td>phone: {$array[sec1].phone}<br /><img src="path{$array[sec1].i}.gif" /></td>
{if ((%sec1.index% + 1) % $cols_per_row) == 0 || %sec1.last%}
</tr>
{/if}
{/section}
</table>



Hope this helps someone!

iDİle
Back to top
View user's profile Send private message
Ceph
Smarty Rookie


Joined: 25 Nov 2003
Posts: 24

PostPosted: Sun Sep 05, 2004 1:44 am    Post subject: Reply with quote

And something similiar, minus the section structure (very simple):

Code:

{assign var="i" value=0}
{foreach from=$data item=item}
{if !$i}<tr>{/if}
<td>{$item->firstname} {$item->lastname}</td>
{assign var="i" value=$i+1}
{if $i == 4}</tr>{assign var="i" value=0}{/if}
{/foreach}
{if !$i}</tr>{/if}
[/code]
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP