|
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 |
regnastor Smarty Rookie
Joined: 25 May 2003 Posts: 16
|
Posted: Sun Oct 26, 2003 5:05 pm Post subject: Value in one array as key of other array |
|
|
Hi,
i have two array, each containing several records of two different tables of my database.
One contains the existing calendars, the other one the entries. One field of the entry table contains the id of the calendar the entry belongs to.
The index of each array are the ids of the records.
Looks like this:
$calendars = array(
'10' => array(
'id' => 10,
'name' => "sample calendar"
),
'15' => array(
'id' => 15,
'name' => "another sample calendar"
)
)
$entries = array(
'500' => array(
'id' => 500,
'title' => "sample entry",
'calendarid' => 15
)
)
Now i want to list all entries and the name of the calendar it belongs to.
Code:
ID, Title, Calendar-Name<br>
{foreach from=$entries item=entry}
{$entry.id}, {$entry.title}, {$calendars[$entry.calendarid].name}<br>{/foreach}
Output:
ID, Title, Calendar-Name
500, sample entry,
But why?
If i use the following code it works:
ID, Title, Calendar-Name
{foreach from=$entries item=entry}
{assign var=calendarid value=$entry.calendarid}
{$entry.id}, {$entry.title}, {$calendars[$calendarid].name}<br>{/foreach}
Why is the first way wrong?
Thx in Advance,
Regnastor |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Oct 26, 2003 9:55 pm Post subject: |
|
|
easy:
$entry.calendarid = 10 (and 15)
BUT:
$calendars[10] = null
while
$calendars["10"] = array(
'id' => 10,
'name' => "sample calendar"
)
You are confusing (string) associative keys with (integer) indexes. In the second case, you obviate that by not accessing by your stored index values but instead, by the key value found in the array itself (ie: "10"). |
|
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
|