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

set smarty assign within php iteration 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 -> General
View previous topic :: View next topic  
Author Message
anything7pob
Smarty Rookie


Joined: 26 Mar 2017
Posts: 26

PostPosted: Sun Mar 26, 2017 5:00 pm    Post subject: set smarty assign within php iteration loop? Reply with quote

<?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.... Sad
Back to top
View user's profile Send private message Send e-mail
anything7pob
Smarty Rookie


Joined: 26 Mar 2017
Posts: 26

PostPosted: Sun Mar 26, 2017 5:23 pm    Post subject: Reply with quote

http://www.smarty.net/forums/viewtopic.php?t=16633

I think I found it..
Back to top
View user's profile Send private message Send e-mail
anything7pob
Smarty Rookie


Joined: 26 Mar 2017
Posts: 26

PostPosted: Sun Mar 26, 2017 5:53 pm    Post subject: Reply with quote

nope still stuck!

Would be really nice if this is possible.
Back to top
View user's profile Send private message Send e-mail
anything7pob
Smarty Rookie


Joined: 26 Mar 2017
Posts: 26

PostPosted: Sun Mar 26, 2017 5:57 pm    Post subject: Reply with quote

$smarty->assign('firstparastyle', $row ['firstParaStyle']);
$smarty->assign('mainColumn', $row ['mainColumn']);

I can do this but it seems wrong..
Back to top
View user's profile Send private message Send e-mail
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Mar 28, 2017 2:05 am    Post subject: Reply with quote

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
View user's profile Send private message
anything7pob
Smarty Rookie


Joined: 26 Mar 2017
Posts: 26

PostPosted: Tue Mar 28, 2017 3:25 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
anything7pob
Smarty Rookie


Joined: 26 Mar 2017
Posts: 26

PostPosted: Tue Mar 28, 2017 3:30 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
anything7pob
Smarty Rookie


Joined: 26 Mar 2017
Posts: 26

PostPosted: Tue Mar 28, 2017 3:54 pm    Post subject: Reply with quote

I need to re order my database to make it easy to work with simple arrays
Back to top
View user's profile Send private message Send e-mail
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 -> 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