1

PHP ファイルで名前が付けられた次の多次元配列を取得し、$cs_map_dataそれを smarty テンプレートに割り当てました。

Array
(
    [2] => Array
        (
            [class_id] => 2
            [class_name] => II
            [class_checked] => 0
            [class_subjects] => Array
                (
                    [0] => Array
                        (
                            [cs_map_id] => 81
                            [subject_name] => 11 Engllish
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                )

        )

    [3] => Array
        (
            [class_id] => 3
            [class_name] => III
            [class_checked] => 0
            [class_subjects] => Array
                (
                    [0] => Array
                        (
                            [cs_map_id] => 155
                            [subject_name] => Hidi
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                    [1] => Array
                        (
                            [cs_map_id] => 156
                            [subject_name] => 11 Maths
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                    [2] => Array
                        (
                            [cs_map_id] => 157
                            [subject_name] => 11 Science
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                    [3] => Array
                        (
                            [cs_map_id] => 158
                            [subject_name] => 11 Engllish
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                )

        )

    [4] => Array
        (
            [class_id] => 4
            [class_name] => IV
            [class_checked] => 0
            [class_subjects] => Array
                (
                    [0] => Array
                        (
                            [cs_map_id] => 86
                            [subject_name] => Physics
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                )

        )

    [6] => Array
        (
            [class_id] => 6
            [class_name] => VI
            [class_checked] => 0
            [class_subjects] => Array
                (
                    [0] => Array
                        (
                            [cs_map_id] => 144
                            [subject_name] => Mathematics
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                    [1] => Array
                        (
                            [cs_map_id] => 145
                            [subject_name] => dfadadadsagfasrsarasrarBiology
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                )

        )

    [7] => Array
        (
            [class_id] => 7
            [class_name] => VII
            [class_checked] => 0
            [class_subjects] => Array
                (
                    [0] => Array
                        (
                            [cs_map_id] => 129
                            [subject_name] => Physics
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                    [1] => Array
                        (
                            [cs_map_id] => 130
                            [subject_name] => Chemistry11
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                    [2] => Array
                        (
                            [cs_map_id] => 131
                            [subject_name] => 11 Science
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                )

        )

    [8] => Array
        (
            [class_id] => 8
            [class_name] => VIII
            [class_checked] => 0
            [class_subjects] => Array
                (
                    [0] => Array
                        (
                            [cs_map_id] => 67
                            [subject_name] => Hidi
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                    [1] => Array
                        (
                            [cs_map_id] => 68
                            [subject_name] => 11 Engllish
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                )

        )

    [9] => Array
        (
            [class_id] => 9
            [class_name] => IX
            [class_checked] => 0
            [class_subjects] => Array
                (
                    [0] => Array
                        (
                            [cs_map_id] => 87
                            [subject_name] => Mathematics
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                    [1] => Array
                        (
                            [cs_map_id] => 88
                            [subject_name] => Hidi
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                    [2] => Array
                        (
                            [cs_map_id] => 89
                            [subject_name] => 11 Science
                            [subject_checked] => 0
                            [teacher_cs_id] => 
                        )

                )

        )

)

キー [cs_map_id] と smarty[subject_name]テンプレートのすべての要素の値にアクセスしたいと思います。[cs_map_id]foreach を使用してみましたが、 keyとから値を取得できませんでした[subject_name][cs_map_id]key の値と[subject_name]foreach ループで smartyにアクセスする方法を教えてください。smarty テンプレートで私が書いたコードは次のとおりです。

<table>                         
              <tr>
                 {assign var='i' value=0}
                 {if $cs_map_data}
                 {foreach from=$cs_map_data item="map_data"}
                 {if $i%4 == 0}</tr><tr>{/if}
                 <td align="left" valign="top" width="150">

                  <input type="checkbox" name="cs_map_id[]" id="{$map_data.cs_map_id}" value="{$map_data.cs_map_id}"  onChange="get_test_by_category('{$map_data.cs_map_id}'); return false;" />
                 {$map_data.subject_name}
                 </td>
                 {assign var='i' value=$i+1}
                 {/foreach}
                 {else}
                        <td>
                            <i>No Subject is assigned to the class.</i>
                    </td>
                 {/if}
                </tr>
               </table>

smarty テンプレートの forech で両方のキーの値を使用する方法について、誰か助けてもらえますか? 前もって感謝します。

4

1 に答える 1

3

これは Smarty 2 と 3 で動作します。{foreach} はバージョン間で大幅に変更されましたが、{section} は同じように動作します。

{section name=map loop=$cs_map_data}
    <tr>
         <td>{$cs_map_data[map].class_name}</td> //this is the format to access your data from the array
    {section name=subject loop=$cs_map_data[map].class_subjects}
          <td>
                {$cs_map_data[map].class_subjects[subject].subject_name} //this is how to access the entries of your 'inner' array
          </td>
    {/section} //end your nested section
    </tr>
{/section} //end your outer section

これは、配列をループするように機能する必要があります...

セクションの smarty マニュアル: http://www.smarty.net/docsv2/en/language.function.section.tpl

幸運を!

于 2013-05-07T14:53:13.417 に答える