Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Smarty Foreach Statement with mysql PDO

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
YesImSevy
Smarty n00b


Joined: 09 Feb 2019
Posts: 2

PostPosted: Sat Feb 09, 2019 7:37 am    Post subject: Smarty Foreach Statement with mysql PDO Reply with quote

So ive been trying for a few hours on how to do a foreach statement with smarty displaying rows in my database using PDO. I have tried multiple things to make it work with a foreach but yet nothing ever works. If someone could help me out and send me the code it would be amazing!

Code I use to select * from my table.
Code:

$stmt = $conn->prepare("SELECT * FROM staff");
$stmt->execute();
$staff_results = $stmt->fetch(PDO::FETCH_ASSOC);


(sorry if im in wrong section im new here)[/code]
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1577

PostPosted: Sat Feb 09, 2019 12:55 pm    Post subject: Reply with quote

Best is to PDOStatement::fetchAll() everything into a template variable.

Code:
$smarty->assign("variable", $stmt->fetchAll());
$smarty->display("template.smarty");


P.S.
Strongly recommend https://phpdelusions.net/pdo#why before you make any more mistakes.
Back to top
View user's profile Send private message
YesImSevy
Smarty n00b


Joined: 09 Feb 2019
Posts: 2

PostPosted: Sat Feb 09, 2019 4:46 pm    Post subject: Reply with quote

AnrDaemon wrote:
Best is to PDOStatement::fetchAll() everything into a template variable.

Code:
$smarty->assign("variable", $stmt->fetchAll());
$smarty->display("template.smarty");


P.S.
Strongly recommend https://phpdelusions.net/pdo#why before you make any more mistakes.


But with this would I just need to put

$smarty->assign('staff_results', $staff_results = $stmt->fetch(PDO::FETCH_ASSOC));


Or how would I do this. Also what is the code that would actually go into the .tpl files... Im so confused.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1577

PostPosted: Mon Feb 11, 2019 7:39 am    Post subject: Reply with quote

Documentation is at https://www.smarty.net/docs/en/
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General 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