View previous topic :: View next topic |
Author |
Message |
anything7pob Smarty Rookie
Joined: 26 Mar 2017 Posts: 26
|
Posted: Sun Mar 26, 2017 5:00 pm Post subject: set smarty assign within php iteration loop? |
|
|
<?php
$stmt = $reg_user->runQuery("SELECT * FROM page WHERE address='index'");
$stmt->execute (array());
$row = $stmt->fetch(PDO::FETCH_ASSOC);
foreach $row as $key => $value{
$smarty->assign('{$key}', {$value} );
}
$smarty->display('index.tpl');
?>
Hi I am struggling to get my function to work. I guess it is self explainitory but anyway, I have all my pages stored in a database, I want to pull the values from the database and assign them to smarty variables with their names inherited from the array key ie. my database headers.
I feel like I am pretty close?
Do I need to return the statement or echo it or make variables to be returned and used outside the function brackets?
I`m not getting a lot of feedback from my system after installing the templating system.... |
|
Back to top |
|
anything7pob Smarty Rookie
Joined: 26 Mar 2017 Posts: 26
|
|
Back to top |
|
anything7pob Smarty Rookie
Joined: 26 Mar 2017 Posts: 26
|
Posted: Sun Mar 26, 2017 5:53 pm Post subject: |
|
|
nope still stuck!
Would be really nice if this is possible. |
|
Back to top |
|
anything7pob Smarty Rookie
Joined: 26 Mar 2017 Posts: 26
|
Posted: Sun Mar 26, 2017 5:57 pm Post subject: |
|
|
$smarty->assign('firstparastyle', $row ['firstParaStyle']);
$smarty->assign('mainColumn', $row ['mainColumn']);
I can do this but it seems wrong.. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Mar 28, 2017 2:05 am Post subject: |
|
|
It seems you're just making a newbie mistake in trying to do everything at once.
Don't do it.
Don't assign within the loop. Prepare an array as you see it fit, then assign it once. |
|
Back to top |
|
anything7pob Smarty Rookie
Joined: 26 Mar 2017 Posts: 26
|
Posted: Tue Mar 28, 2017 3:25 pm Post subject: |
|
|
Yes you are right, thank you for replying. I have just began to try to implement smarty on my site. I am a roofer and my only experience with computer programming is building my own site. (rooftiler.co.uk)
and messing a little bit (lots of help with the backend) with phpLD (changemyroof.com) which is what brought me here.
I understand a little better now I think.
$stmt = $reg_user->runQuery("SELECT * FROM page ");
$stmt->execute (array());
$row3 = $stmt->fetchall();
foreach ($row3 as &$value) {
echo $id = $row3 [$i][0];
echo $metaTitle = $row3 [$i][1];
etc
$i++;
}
That is where I am at the moment.
I am making progress and the learning process still has attached sence of achievement = still enjoyable which is great. |
|
Back to top |
|
anything7pob Smarty Rookie
Joined: 26 Mar 2017 Posts: 26
|
Posted: Tue Mar 28, 2017 3:30 pm Post subject: |
|
|
and I managed to get setup over wamp so feedback is restored, I dont honestly understand how or why yet but it works for me at this moment.
It will however annoy me until I understand but it is currently back burnered...
but its exactly the same setup......no, no time......should have been doing it locally anyway...savepoints are good.... |
|
Back to top |
|
anything7pob Smarty Rookie
Joined: 26 Mar 2017 Posts: 26
|
Posted: Tue Mar 28, 2017 3:54 pm Post subject: |
|
|
I need to re order my database to make it easy to work with simple arrays |
|
Back to top |
|
|