PHP+smarty looping question[SOLVED]

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
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

PHP+smarty looping question[SOLVED]

Post 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:
Last edited by the_last_tamurai on Sun Apr 22, 2007 9:39 pm, edited 1 time in total.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post 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
User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

Post by the_last_tamurai »

thanx GeXus

you remind me the way

I'll code it my way .

thnx again :)
User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

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