|
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 |
zbert Smarty Rookie
Joined: 11 Dec 2003 Posts: 30 Location: New York
|
Posted: Fri Feb 13, 2004 5:01 am Post subject: How to reference arrays created with Append |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Feb 13, 2004 8:31 am Post subject: |
|
|
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 |
|
zbert Smarty Rookie
Joined: 11 Dec 2003 Posts: 30 Location: New York
|
Posted: Fri Feb 13, 2004 12:43 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Feb 13, 2004 12:47 pm Post subject: |
|
|
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 |
|
zbert Smarty Rookie
Joined: 11 Dec 2003 Posts: 30 Location: New York
|
Posted: Fri Feb 13, 2004 12:54 pm Post subject: |
|
|
Definitley not a knock on you. I understand. Just wanted to make sure the next person that comes along has the correct info.
--zbert |
|
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
|