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

{foreach} looping with PHP iterator

 
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
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sat Jan 09, 2010 12:06 am    Post subject: {foreach} looping with PHP iterator Reply with quote

You can loop an iterator with the Smarty 3 {foreach} loop, much the same way you can loop an iterator in PHP with foreach(). This comes in very handy when want to loop over SQL result sets as efficiently as possible.

Here is an example of iterating over a result set using PDO.

PHP:

Code:
<?php

include('Smarty.class.php');

$smarty = new Smarty;

$dsn = 'mysql:host=localhost;dbname=test';
$login = 'test';
$passwd = 'test';

// setting PDO to use buffered queries in mysql is
// important if you plan on using multiple result cursors
// in the template.

$db = new PDO($dsn, $login, $passwd, array(
   PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));

$res = $db->prepare("select * from users");
$res->execute();
$res->setFetchMode(PDO::FETCH_LAZY);

// assign to smarty
$smarty->assign('res',$res);

$smarty->display('index.tpl');

?>


index.tpl:

Code:
{foreach $res as $r}
 {$r.id}
 {$r.name}
{/foreach}


This is assuming the users table has columns named id and name.

What is the advantage of an iterator vs. looping over a plain old array? With an array, all the results are accumulated into memory before being looped. With an iterator, each result is loaded/released within the loop. This saves processing time and memory, especially for very large result sets.


Last edited by mohrt on Mon Aug 02, 2010 7:03 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
SoN9ne
Smarty Rookie


Joined: 25 Feb 2010
Posts: 17

PostPosted: Thu Feb 25, 2010 11:07 pm    Post subject: Reply with quote

Thanks, just what I was looking for! Very Happy
Back to top
View user's profile Send private message
ahoussam
Smarty Rookie


Joined: 26 Jan 2010
Posts: 16

PostPosted: Thu Mar 11, 2010 1:36 pm    Post subject: Reply with quote

Hi
I would like to use smarty file to display data

in php file i used:

$conn = oci_connect('username', 'passwd', ip-server/My_db');
$query = 'select * from my_table';
$stid = oci_parse($conn, $query);
oci_execute($stid, OCI_DEFAULT);
$row = oci_fetch_array($stid, OCI_ASSOC);

now I passed $row to other php file call smarty file
and in smarty file i wrote:

{section name=i start=0 loop=$row}
{row.i}
{/section
but no data !
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Mar 11, 2010 2:22 pm    Post subject: Reply with quote

The right syntax for accessing data in a section loop is
Code:
{section name=i loop=$row}
{$row[i]}
{/section}
Back to top
View user's profile Send private message
ahoussam
Smarty Rookie


Joined: 26 Jan 2010
Posts: 16

PostPosted: Mon Mar 15, 2010 9:21 am    Post subject: Reply with quote

Thank you,
yes, I emerged from the inquiry, but I have a problem!
There is data in the table is written in Arabic language has appeared on my page characters are illegible
How to solve this problem?
thanks
Back to top
View user's profile Send private message
SoN9ne
Smarty Rookie


Joined: 25 Feb 2010
Posts: 17

PostPosted: Thu Mar 18, 2010 1:52 am    Post subject: Reply with quote

Does this allow for use of continue in the foreach loop also? I am working on a foreach loop but I can't seem to figure out how to get it to continue...
Back to top
View user's profile Send private message
ahoussam
Smarty Rookie


Joined: 26 Jan 2010
Posts: 16

PostPosted: Thu Mar 18, 2010 8:00 am    Post subject: Reply with quote

thanks
you can see at this subject http://www.smarty.net/forums/viewtopic.php?p=63427#63427[/url]
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jul 21, 2010 1:46 pm    Post subject: Reply with quote

SoN9ne wrote:
Does this allow for use of continue in the foreach loop also? I am working on a foreach loop but I can't seem to figure out how to get it to continue...


use an {if}{/if} statement to skip output on the given loop. Typically you want to build your result sets so they can be simply looped in the template. Only assign what is necessary, and in a structure that is ready for presentation.
Back to top
View user's profile Send private message Visit poster's website
mohamedmashhot
Smarty n00b


Joined: 20 Dec 2010
Posts: 2

PostPosted: Mon Dec 20, 2010 10:45 pm    Post subject: Reply with quote

i have code
Code:



{foreach name="photoes" from=$allmain item="maincat"}
                           <td valign="top" width="50%">
                           <div class="moduletable_yjnf7">
                              <h3>
                              <a href="newssection.php?secid={$maincat.id}">
                              {$maincat.s_name}</a><a style="BACKGROUND-IMAGE: url(templates/default/images/show.jpg); WIDTH: 18px; DISPLAY: block; FLOAT: left; HEIGHT: 16px" class="collapse"></a></h3>
                              <div class="yj_news_flash_c">
                                 <div style="POSITION: absolute; TOP: 110px; FONT-WEIGHT: bold; LEFT: 30px; dispaly: inline"></div>
                                 <div style="POSITION: absolute; BOTTOM: 0px; FONT-WEIGHT: bold; LEFT: 30px; dispaly: inline">
                                    <a href="http://wafdnews.com/index.php?option=com_mysection&view=section&id=5">
                                    <img border="0" src="templates/default/images/more.gif"></a></div>
                                 <ul class="nw_block">
                                        {foreach name="photoesc" item=i from=$alls item="subcat"}

                                                {if $subcat.c_id eq $maincat.id}
                                       {if $smarty.foreach.subcat.index %3==0}
                                       <li>
                                    <a title="{$subcat.n_title}" href="news.php?newsid={$subcat.id}">
                                 {$subcat.n_title}</a></li>
                                 {/if}
                                 {/if}
                                            {/foreach}
                                 </ul>
                                 <br>
                              </div>
                           </div>
                           </td>
                           {if not $smarty.foreach.last and $smarty.foreach.photoes.iteration is div by 2}
                           </tr>
                           {/if}
                                    {/foreach} 
                        

in the code i want to show 3 rows by
Code:
               {if $smarty.foreach.subcat.index %3==0}


but when i put the code don't work if i have more than 3 rows in database
can i found any help
i need it necessary
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Dec 20, 2010 10:52 pm    Post subject: Reply with quote

try:

{if $smarty.foreach.photoesc.iterator is div by 3}

(use the foreach "name")
Back to top
View user's profile Send private message Visit poster's website
mohamedmashhot
Smarty n00b


Joined: 20 Dec 2010
Posts: 2

PostPosted: Tue Dec 21, 2010 12:26 am    Post subject: Reply with quote

Code:
{if $smarty.foreach.photoesc.iterator is div by 3}


this for main foreach
Code:
                        {foreach name="photoes" from=$allmain item="maincat"}


this is code i want to modify it

Code:
<ul class="nw_block">
                                        {foreach name="photoesc" item=i from=$alls item="subcat"}
      
                                       {if $smarty.foreach.subcat.index % 3 == 0}
                                       
                                       
                                       <li>
                                    <a title="{$subcat.n_title}" href="news.php?newsid={$subcat.id}">
                                 {$subcat.n_title}</a></li>

                                 {/if}
                                            {/foreach}
                                 </ul>
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