Page 1 of 1

PHP+smarty looping question[SOLVED]

Posted: Sat Apr 21, 2007 4:53 pm
by the_last_tamurai
I'm sure that many developer experienced moments like this

when you're sure you did code this before but your mind stop processing :(

Any how , my problem is : I have an array fetched from DB (multi-dimensional)

I want to loop throw this array and display it with n record per table row.

Like that :
{section name=i loop=$result}
<!--some html tags-->
<td>{$result.foo}</td>
<!--some html tags-->
{/section}


and the result is something like :
<tr>
<td>result1</td>
<td>result2</td>
<td>result3</td>
</tr>
<tr>
<td>result4</td>
<td>result5</td>
<td>result6</td>
</tr>
.
.
.etc


I think it's **** easy :roll:

Posted: Sat Apr 21, 2007 6:01 pm
by GeXus
You have to use foreach for multi-dimentional arrays, section tags only do single. Here is an example using iteration for getting a table layout..

Code: Select all

{* The header block is output every five rows *}
<table>
{foreach from=$items key=myId item=i name=foo}
  {if $smarty.foreach.foo.index % 5 == 0}
     <tr><th>Title</th></tr>
  {/if}
  <tr><td>{$i.label}</td></tr>
{/foreach}
</table>
re: http://smarty.php.net/manual/en/languag ... oreach.php

Posted: Sat Apr 21, 2007 7:02 pm
by the_last_tamurai
thanx GeXus

you remind me the way

I'll code it my way .

thnx again :)

Posted: Sun Apr 22, 2007 7:49 pm
by the_last_tamurai
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I found another pretty code

[syntax="smarty"]{assign var=max_in_row value=3}

{assign var=col_count value=1}
<table>
{foreach from=$products item=product}
   {if $col_count == 1}<tr>{/if}
   <td>{$product.prod_name|escape}</td>
   
   {assign var=col_count value=$col_count+1}
   {if $col_count > $max_in_row}</tr>{assign var=col_count value=1}{/if}
{/foreach}

{if $col_count > 1}
   {math equation="x + 1 - y" x=$max_in_row y=$col_count assign=cols}
   {section name=myloop loop=$cols}
   <td>&nbsp;</td>
   {/section}
   </tr>
{/if}
</table>
impressive , huh


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]