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

A alternative for the while() loop

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
Jackie
Smarty n00b


Joined: 30 Dec 2010
Posts: 1

PostPosted: Thu Dec 30, 2010 10:26 pm    Post subject: A alternative for the while() loop Reply with quote

Hi,

I am going to show you how to create a while() loop in smarty.

The PDO example

First lets make connection to the MySQL database

Code:
$dbh = new PDO("mysql:host=xxx;dbname=xxx", "xxx", "xxx");


Make a query

Code:
$stmt = $dbh->query("SELECT * FROM table_name");


We set the results in an array()

Code:
$results = array();


The while loop

Code:
while($res = $stmt->fetch(PDO::FETCH_ASSOC))
{
    $results[] = $res;
}


And do this in your template file to show the reccords

Code:
{section name=example loop=$results}
    {$results[example].itsworks}
{/section}


I hope i helped you all.

Sorry for my bad English, i am dutch Very Happy
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 -> Smarty Development 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