View previous topic :: View next topic |
Author |
Message |
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sat Jan 09, 2010 12:06 am Post subject: {foreach} looping with PHP iterator |
|
|
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 |
|
SoN9ne Smarty Rookie
Joined: 25 Feb 2010 Posts: 17
|
Posted: Thu Feb 25, 2010 11:07 pm Post subject: |
|
|
Thanks, just what I was looking for! |
|
Back to top |
|
ahoussam Smarty Rookie
Joined: 26 Jan 2010 Posts: 16
|
Posted: Thu Mar 11, 2010 1:36 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Mar 11, 2010 2:22 pm Post subject: |
|
|
The right syntax for accessing data in a section loop is
Code: | {section name=i loop=$row}
{$row[i]}
{/section} |
|
|
Back to top |
|
ahoussam Smarty Rookie
Joined: 26 Jan 2010 Posts: 16
|
Posted: Mon Mar 15, 2010 9:21 am Post subject: |
|
|
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 |
|
SoN9ne Smarty Rookie
Joined: 25 Feb 2010 Posts: 17
|
Posted: Thu Mar 18, 2010 1:52 am Post subject: |
|
|
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 |
|
ahoussam Smarty Rookie
Joined: 26 Jan 2010 Posts: 16
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jul 21, 2010 1:46 pm Post subject: |
|
|
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 |
|
mohamedmashhot Smarty n00b
Joined: 20 Dec 2010 Posts: 2
|
Posted: Mon Dec 20, 2010 10:45 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Dec 20, 2010 10:52 pm Post subject: |
|
|
try:
{if $smarty.foreach.photoesc.iterator is div by 3}
(use the foreach "name") |
|
Back to top |
|
mohamedmashhot Smarty n00b
Joined: 20 Dec 2010 Posts: 2
|
Posted: Tue Dec 21, 2010 12:26 am Post subject: |
|
|
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 |
|
|