Re: More complex table
| From: | Justin Patrin | Date: | Sat, 06 Dec 2003 19:20:52 +0000 |
| Subject: | Re: More complex table | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-10644@lists.php.net to get a copy of this message | ||
Just create the table in the template, not in PHP. You could use something likle this:
<table>
{section name=image loop=$pic_fn}
{if image%2 == 0}
<tr>
{/if}
<td><img src="{$pic_fn[image]}" alt="{$pic_desc[image]}"/><br/>info: {$pic_info[image]}</td>
{if image%2 == 0}
</tr>
{/if}
{/section}
</table>
And if that's too much for you, write a custom funtion for smarty that does the same thing.
You could also do the same thing with an array of associative arrays (records) and a foreach (which, IMHO, is much cleaner as all info for one "section" is in one array).
If the array looked like:
$pics = array(array('fn' => '1.jpg', 'desc' => 'me', 'info' => '1024 x 768'),
array('fn' => '2.jpg', 'desc' => 'jack', 'info' => '800 x 600')
);
which is how an associative array fetch from a DB would give them to you, you could use this in smarty:
<table>
{foreach from=$pics item=image key=num}
{if num%2 == 0}
<tr>
{/if}
<td><img src="{$image.name}" alt="{$image.desc}"/><br/>info: {$image.info}</td>
{if num%2 == 0}
</tr>
{/if}
{/section}
</table>
--paperCrane <Justin Patrin>
Janos Rusiczki wrote:
Hello, Say, I have the following data: $pic_fn = array('1.jpg', '2.jpg', '3'.jpg'); $pic_desc = array('me', 'jack', 'george'); $pic_info = array('1024 x 768', '800 x 600', '640 x 480'); I would like to generate a table. If I use 'html_table' I can use the following: {html_table loop=$pic_fn cols=2 table_attr='border="0"'} resulting in: <table border="0"> <tr><td>1.jpg</td><td>2.jpg</td></tr> </tr><td>3.jpg</td><td> </td></tr> </table> However I'd like to get something more complex, namely (from the data I presented above): <table border="0"> <tr> <td><img src="1.jpg" alt="me"><br />info: 1024 x 768</td> <td><img src="2.jpg" alt="jack"><br />info: 800 x 600</td> </tr> <tr> <td><img src="3.jpg" alt="george"><br />info: 640 x 480</td> <td> </td> </tr> </table> I think it would be a good ideea to extend 'html_table'-s functionality to include ways of dealing with these kinds of situations. Until then I'm asking for an elegant, template based, solution, because I have one but it involves building the whole table in PHP. And then, what's the point of using the template engine? :) Thanks, -janos-