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

How to reference arrays created with Append

 
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
zbert
Smarty Rookie


Joined: 11 Dec 2003
Posts: 30
Location: New York

PostPosted: Fri Feb 13, 2004 5:01 am    Post subject: How to reference arrays created with Append Reply with quote

I've used append successfully to create some arrays from my query and then pass to my template files. My questions is this; "How can I use the array created with append in my PHP code?

Code:
while (!$result->EOF){
   $smarty->append(array(
         "datax" => $result->fields["title"],
         "datay" => $result->fields["number"]));
   $result->MoveNext();
}


I want to use $datax and $datay to do some other things before I display my templates. I want to use these arrays to create a graph using JpGraph. However, when I try the following, there is no data in $datax or $datay.

Code:
// Setup X-axis labels
$graph->xaxis->SetTickLabels($datax);

// Create the bar pot
$bplot = new BarPlot($datay);

Hope that make sense.

--zbert
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Feb 13, 2004 8:31 am    Post subject: Reply with quote

You get get you template-variables back with $smarty->get_template_vars($varname) (see the manual for that).

But I'd build the array in php and assign it as a whole, if I want to use it php afterwards anyway:

[php:1:9ce2c70f03]
$datax = array();
$datay = array();
while (!$result->EOF){
/* append values */
$datax[] = $result->fields["title"];
$datay[] = $result->fields["number"];
$result->MoveNext();
}
$smarty->assign(array('datax'=>$datax, 'datay'=>$datay));

// ....

use $datax and $datay from php here.
[/php:1:9ce2c70f03]

you can also assign the arrays by reference:
[php:1:9ce2c70f03]
$smarty->assign('datax', $datax);
$smarty->assign('datay', $datay);
[/php:1:9ce2c70f03] if you add_by_ref, then then all changes done to $datax are also visible in the assigned {$datax}.

[EDIT: fixed array-syntax as pointed out by zbert below]


Last edited by messju on Fri Feb 13, 2004 12:48 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
zbert
Smarty Rookie


Joined: 11 Dec 2003
Posts: 30
Location: New York

PostPosted: Fri Feb 13, 2004 12:43 pm    Post subject: Reply with quote

Thanks so much. That worked perfectly. One note though. I had to add the $ sign in front of the arrays to make it work.
[php:1:f39f3c1636]<?php
$datax[] = $result->fields["title"];
$datay[] = $result->fields["number"];
?>[/php:1:f39f3c1636]
When I get some time, I'll go back and check out template-variables. Thanks for pointing me in the right direction.

--zbert
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Feb 13, 2004 12:47 pm    Post subject: Reply with quote

zbert wrote:
One note though. I had to add the $ sign in front of the arrays to make it work.


Ah, sure, sorry. I must admit i don't run-and-test all code I enter in this forum's textarea.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
zbert
Smarty Rookie


Joined: 11 Dec 2003
Posts: 30
Location: New York

PostPosted: Fri Feb 13, 2004 12:54 pm    Post subject: Reply with quote

Definitley not a knock on you. I understand. Just wanted to make sure the next person that comes along has the correct info. Smile

--zbert
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 -> 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