开发者

Multi dimensional array iteration Smarty

I have two arrays. In first array I have Type and Zone field names. These two field names have multiple values. I am using smarty. I want the select name from the first array and options for each dropdown will come from the second array. This is what I have tried :

{foreach from=$field_names item=fld_name}
        <tr>
            <td width="3%" height="15" style="font-weight:bold;height:30px; padding-top:5px;">&nbsp;</td>
            <td width="32%" height="15" valign="middle" style="font-weight:bold;height:30px; padding-top:5px;">&nbsp;{$fld_name}:</td>
            <td width="65%" height="15" valign="middle" style=" padding-top:5px;"> 
                <select name="{$fld_name}" id="{$fld_name}" style="width:95px">
                    <option value="-1">Any</option>
                    {foreach from=$field_values item=fld_val key=key}
                        <option value="{$fld_val.$key.value}">{$fld_val.$key.value}</option>
                    {/foreach}
                </select> 
            </td>
        </tr>
        {/foreach}

This is the output of arrays :

This is the Output of first array:

Array ( [2] => Type [1] => Zone )

This is second array: Array ( [0] => Array ( [0] => Array ( [productid] => 141 [fieldid] => 2 [value] => Laptop )

        [1] => Array
            (
                [productid] => 191
                [fieldid] => 2
                [value] => Books
            )

        [2] => Array
            (
                [productid] => 177
                [fieldid] => 2
                [value] => Printer
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [productid] => 141
                [fieldid] => 1
                [value] => 3
            )

        [1] => Array
            (
                [productid] => 191
                [fieldid] => 1
                [value] => 4
            )

        [2] => Array
            (
                [productid] => 177
                [fieldid] => 1
                [value] => 2
            )

    )

)

I am getting the same options in both the dropdown. What Iam 开发者_如何学Cdoing wrong please help..

Thanks in advance.


First off; the first array has id's 2 and 1 mapped to respectively Type and Zone. Your first smarty foreach will loop through that array and product Type and Zone as values for the $fld_name variable.

The second foreach, however, is being done without any relation to the first; so both iterations through that array will be the same.

I believe I have a similar situation in one of my Smarty templates; but since I'm at work I can't take a look at them :(.

Anyways; I believe I made either an object or an array with names as indexes; so in your case, the options for Type would be in the second array under a 'Type' index, same for Zone. That way, you can foreach through the second array part that belongs to the given key:

{foreach $field_names as $field_name}
    ...
    {foreach $field_values[$field_name] as $field_value}
        ...
    {/foreach}
{/foreach}

Your second array would change from having '0' and '1' as indexes for the value arrays, to 'Type' and 'Zone'. Such a construction should be able to work.

For a codepad example of the above, see http://codepad.org/kzfaYuSd. I believe that somewhat does the same as what you are trying to get done with the arrays. It still means changing the arrays; but otherwise you'll probably be doing a lot of logic in the smarty template, which really makes them unreadable. Unfortunately codepad doesn't have smarty; but I know smarty can do the same with the foreach statements :).

A secondary approach is to create a object for a field, consisting of the field name and field values as properties:

$items = new array();
$item1 = new StdObject();
$item1->field_name = "Type";
$item1->field_values = new array("Value1", "Value2");
$items[] = $item1;

And then in smarty (assuming $items is mapped to $items):

{foreach $items as $item}
    $item->field_name
    {foreach $item->field_values as $value}
         $value
    {/foreach}
{/foreach}

(ofcourse including your own code around those values :)).

Codepad illustrating the latter: http://codepad.org/flIhRlmO

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜