如何在smarty模板中打印子阵列?

时间:2014-03-20 15:09:27

标签: php arrays foreach smarty associative-array

以下是PHP中的数组标题$ test_data:

Array
(
    [1] => Array
        (
            [test_pack_id] => 84880f321fc957f9b28b702d5a43f57b
            [test_pack_name] => CPT General Economics
            [test_pack_desc] => This Package contains <b>16 chapterwise testlets</b> of 15 minutes each, <b>8 chapterwise tests </b>of 1 hour each and <b>3 full syllabus tests</b> of 1 hour covering syllabus of CPT General Economics.<br>
<b>Total Tests in this Package : 27</b>
            [test_pack_type_id] => 7
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 6
            [test_pack_validity_days] => 0
            [test_pack_plan] => paid
            [test_pack_price] => 359.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_code] => 
            [test_pack_sold] => 0
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_created_date] => 1338298119
            [test_pack_updated_date] => 1343564534
            [test_pack_purchase_date] => 0
            [test_details] => Array
                (
                    [0] => Array
                        (
                            [test_id] => 580
                            [test_name] => CPT Testlet : Demand & Supply 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [1] => Array
                        (
                            [test_id] => 581
                            [test_name] => CPT Test : Micro Economics
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [2] => Array
                        (
                            [test_id] => 583
                            [test_name] => CPT Testlet : Micro Economics 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [3] => Array
                        (
                            [test_id] => 582
                            [test_name] => CPT Testlet : Micro Economics 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [4] => Array
                        (
                            [test_id] => 579
                            [test_name] => CPT Testlet : Demand & Supply 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [5] => Array
                        (
                            [test_id] => 578
                            [test_name] => CPT Test : Demand & Supply
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [6] => Array
                        (
                            [test_id] => 577
                            [test_name] => CPT Testlet : Production & Cost 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [7] => Array
                        (
                            [test_id] => 576
                            [test_name] => CPT Testlet : Production & Cost 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [8] => Array
                        (
                            [test_id] => 575
                            [test_name] => CPT Test : Production & Cost
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [9] => Array
                        (
                            [test_id] => 574
                            [test_name] => CPT Testlet : Price Determination 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [10] => Array
                        (
                            [test_id] => 573
                            [test_name] => CPT Testlet : Price Determination 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [11] => Array
                        (
                            [test_id] => 572
                            [test_name] => CPT Test : Price Determination
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [12] => Array
                        (
                            [test_id] => 571
                            [test_name] => CPT Testlet : Indian Economy - A Profile 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [13] => Array
                        (
                            [test_id] => 570
                            [test_name] => CPT Testlet : Indian Economy - A Profile 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [14] => Array
                        (
                            [test_id] => 569
                            [test_name] => CPT Test : Indian Economy - A Profile
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [15] => Array
                        (
                            [test_id] => 568
                            [test_name] => CPT Testlet : Aspects of Indian Economy 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                )

        )

)

Now by using following statement I'm assigning this array to smarty template:

    $smarty->assign('test_data', $test_data);

现在我想在我的smarty模板中打印所有测试的值,即我要为数组$test_data['test_details']应用循环。我应用了它,但它在smarty中运行不正常。给出一些模糊的结果。我不明白我在哪里弄错了。在这方面你能帮我吗?我的代码如下:

    <select name="test_id" id="test_id">
                                                   <option value="all">All</option>


                              {foreach from=$test_data item=test key=key}
                          {foreach from=$test.test_details item=test_detail key=key}
                          <option value="{$test_detail.test_id}" {if $test_id==$test_detail.test_id} selected="selected"{/if}>{$test_detail.test_name}</option>
                          {/foreach}
                        {/foreach}   
                        </select>

2 个答案:

答案 0 :(得分:0)

首先:如果您需要在循环中使用key变量,请为其指定唯一名称。您同时命名为key。此外,您甚至不使用key,因此您可以完全跳过它,只留下item个属性。

第二:您指的是$test_id,我在您提供的代码段中没有看到它。如果它没有在其他地方定义,它就不会起作用。

答案 1 :(得分:0)

请参阅以下代码,以便在smarty模板中访问子阵列

  <select name="test_id" id="test_id">
    <option value="all">All</option>
    {foreach from=$test_data key=key item=test}
        {foreach from=$test.test_details item=test_detail key=keylater}
        <option value="{$test_detail.test_id}">{$test_detail.test_name}</option>
        {/foreach}
    {/foreach}
  </select>