View previous topic :: View next topic |
Author |
Message |
BassPlayer Smarty Rookie
Joined: 17 Sep 2003 Posts: 6
|
Posted: Wed Sep 17, 2003 7:38 am Post subject: Using arrays and assign |
|
|
So I must be missing something here in regards to arrays. If I have existing arrays in php that I don't know the keys or values and I want to assign them smarty vars how would I do that? Something like er this?
$smarty = new Smarty;
foreach ($array as $key=>$val) {
$smarty->assign('smarty_array' array($key => $val));
{
Thanks |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Sep 17, 2003 10:19 am Post subject: |
|
|
you mean you have
$array = array('a1'=>'v1', 'a2'=>'v2')
and want to assign 2 variables to smarty $a1 and $a2 ???
you can do this explicitely:
Code: | foreach ($array as $key=>$val) {$smarty->assign($key, $val); } |
or implicitely:
Code: | $smarty->assign($array); |
(if the first parameter of assign is an array smarty assigns all elements automagically for you ) |
|
Back to top |
|
BassPlayer Smarty Rookie
Joined: 17 Sep 2003 Posts: 6
|
Posted: Wed Sep 17, 2003 4:23 pm Post subject: |
|
|
Thanks you for your response. I kinda do the first way now. Nice to know about the automagic way, thanks. So I assume in examples you gave I would end up with a number of discreet vars named after what ever the index is.
For example:
if I have
Code: |
$array = array('one'=>'1', 'two'=>'2', 'three'=>'3')
$smarty->assign($array);
|
I would be able to reference {$one}, {$two} and {$three} in my tpl files.
This is close to what I'm doing now.
What I'd like to do is somehow get the the whole array assigned in a way that I can use the smarty functions to index them by the $var.index method. Like most people I have an indexed array(s) that I use to interate through and index a bunch of other associative arrays to build a number of tables.
So if I had:
Code: |
$opponent = array('team1', 'team2', 'team3')
$wins = array('team1'=>'5', 'team2'=>'10', 'team3'=>'0');
$losses = array('team1'=>'0', 'team2'=>'1', 'team3'=>'0');
|
How do I get these arrays assiged so I can do the eqivalent of this php code with smarty code in the templates.
Code: |
foreach ($opponents as $team) {
echo "<tr><td>$team</td><td>$wins[$team]</td><td>$losses[$team]</td></tr>\r";
}
|
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Sep 17, 2003 4:29 pm Post subject: |
|
|
[php:1:53f4ce0aa1]<?php
$smarty->assign('opponents', $opponents}
$smarty->assign('wins', $wins}
$smarty->assign('losses', $losses}
?>[/php:1:53f4ce0aa1]
Code: | {foreach from=$opponent item=$team}
<tr>
<td>{$team}</td>
<td>{$wins.$team}</td>
<td>{$losses.$team}</td>
</tr>
{/foreach} |
|
|
Back to top |
|
BassPlayer Smarty Rookie
Joined: 17 Sep 2003 Posts: 6
|
Posted: Wed Sep 17, 2003 4:43 pm Post subject: |
|
|
Saweeet! I begin to grasp the concept. Thank you!
Runs off to start coding.... |
|
Back to top |
|
BassPlayer Smarty Rookie
Joined: 17 Sep 2003 Posts: 6
|
Posted: Thu Sep 18, 2003 4:06 am Post subject: |
|
|
One more question o gurus of smarty
How do I assign
Code: |
$opponent_stats['$opponent']['wins'] = "0";
$opponent_stats['$opponent']['losses'] = "0";
$opponent_stats['$opponent']['kills'] = "0";
$opponent_stats['$opponent']['deaths'] = "0";
$opponent_stats['$opponent']['lose_ratio'] = "0";
$opponent_stats['$opponent']['death_ratio'] = "0";
|
How would I assign $opponent_stats
Code: |
$smarty->assign('opponents', array($opponents));
$smarty->assign('opponent_stats', array($opponent_stats));
|
And iterate through them
Code: |
{foreach from=$opponents item=$opponent}
<tr>
<td align="center">{$opponent}</td>
<td align="center">{$opponent_stats.$opponent.wins}</td>
<td align="center">{$opponent_stats.$opponent.losses}</td>
<td align="center">{$opponent_stats.$opponent.lose_ratio}</td>
<td align="center">{$opponent_stats.$opponent.kills}</td>
<td align="center">{$opponent_stats.$opponent.deaths}</td>
<td align="center">{$opponent_stats.$opponent.death_ratio}</td>
<tr>
{/foreach}
|
Thank you for taking the time answer. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Sep 18, 2003 4:31 am Post subject: |
|
|
To make this work, you need to do three subtle things.
The first two apply in all like instances:
1] $opponent_stats[$opponent]['wins'] = "0"; // dynamic key not quoted
2] $smarty->assign('opponents', $opponents); // already an array
The last relates just to the single template statement:
3] {foreach from=$opponents item=opponent} {* use literal name for item *}
Otherwise, its a go!
HTH |
|
Back to top |
|
BassPlayer Smarty Rookie
Joined: 17 Sep 2003 Posts: 6
|
Posted: Thu Sep 18, 2003 5:12 am Post subject: |
|
|
Works like a charm. ThanKs! |
|
Back to top |
|
|