|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
YesImSevy Smarty n00b
Joined: 09 Feb 2019 Posts: 2
|
Posted: Sat Feb 09, 2019 7:37 am Post subject: Smarty Foreach Statement with mysql PDO |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Feb 09, 2019 12:55 pm Post subject: |
|
|
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 |
|
YesImSevy Smarty n00b
Joined: 09 Feb 2019 Posts: 2
|
Posted: Sat Feb 09, 2019 4:46 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
|
|
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
|