tags:

views:

335

answers:

3

I have two loops running in my code, I want to use an element from an array as the key in a second array, but am unsure how to do this with Smarty.

"$dateAndUserForEdit.$key.edit_id" contains an integer (pulled from the db)

I want to use that value as the key in a second loop, which runs fine if I harcode in the integer:

{foreach from=$historyOfRepair.9 key=key item=i}

Pseudo code for the sort of thing I've been trying is:

{foreach from=$historyOfRepair.{$dateAndUserForEdit.$key.edit_id} key=key item=i}

But of course, this doesn't work! Can anybody help?

A: 

I'm only commenting here because no one has given you any advice yet... I have never used Smarty as I've always just made my own templating systems (So I might be ignorant here and my advice useless)...

Can you just construct your array without smarty and then pass it to Smarty for display? Personally, rather than mess with custom template engine code, that's probably what I would do for something other than basic stuff.

Joe Bubna
+4  A: 

Something like next may work (cannot test currently):

{assign var=edit_id value=$dateAndUserForEdit.$key.edit_id}
{foreach from=$historyOfRepair.$edit_id key=key item=i}
Arvo
Arvo is right I've ran into the same problem and this was the solution I used.
JD
A: 

I would agree with Arvo. You will want to assign the id to a temporary variable so that it can be used as a substitution in the foreach loop. Note that in his example code, the value of $key will be replaced with the key of the current item of the $historyOfRepair array. Otherwise that works ( tested too :) ).

Rhinosaurus