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

Small problem with DB variables assignment

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
vijaynair
Smarty n00b


Joined: 24 Mar 2004
Posts: 3
Location: Mumbai, India

PostPosted: Wed Mar 24, 2004 12:53 pm    Post subject: Small problem with DB variables assignment Reply with quote

Hello,

I know that this may have been discussed here earlier. However, I tried to find a solution without success. The problem is as under.

I have a database table which contains multiple records. I reference this record as below:

$db->trans("check for data") which returns "Data updated on 1/3/2004"

Like this there are about 500 record in the table. I want to asign the whole recordset $db->trans to a smarty variable and reference it in the template. Something like follows:

$smarty->assign("mytrans",array($db->trans)); and reference it by
{ $mytrans."check for data" } or by {$mytrans->"check for date " }.

This is not working. I have to assign each and every key & value combination to separate variables in smarty to get it working.

I also tried to create a Plugin Function which will accept the key e.g "check for data" as parameter and return the value. IT is returning Array as answer and not the value. Also, the function is takin in Array as the parameter passed onto it and not treating it as literal value.

Any and every help in this matter is highly appreciated.

Thanks and regards
_________________
Vijayakumar Nair
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Mar 24, 2004 2:02 pm    Post subject: Reply with quote

when you say recordset, I think of something like an indexed array of associative arrays, eg:
Code:
$test = array(array('a'=>1, 'b'=>2), array('a'=>3,'b'=>4);


I would assign this to smarty like:
Code:
$smarty->assign('test', $test);

With this structure, a lot can be done...

Lets say I wanted to look at the 'a' field of the second record:
Code:
{$test[1].a}


I could loop the records:
Code:
{foreach from=$test item=record}
    a = {$record.a}, b= {$record.b}</br>
{/foreach}


I could even loop the fields:
Code:
{foreach from=$test item=record}
    {foreach from=$record key=name item=value}
        {$name} = {$value}</br>
    {/foreach}
{/foreach}


However, to ensure least headaches, ensure that your key names have no spaces. This is not supported:
Code:
{$test[0]."some key with spaces"}


If you want that, you need a temporary var to hold the key name (but this can introduce other problems)
Code:
{assign var=temp value="some key with spaces"}
{$test[0].$temp}


Note that the example that loops the fields above can assign key names into vars for you, but obviously, only when performing a foreach loop.

HTH
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 -> Tips and Tricks 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