如何在以下场景中打印Smarty模板中的关联数组数据?

时间:2014-04-21 14:49:34

标签: php arrays smarty associative-array

我在PHP中使用标题为$all_products的数组,如下所示:

Array
(

    [0] => Array
        (
            [id] => 5
            [code] => Ch1212
            [name] => Chesse
            [manufacturer_id] => 33
            [product_type_id] => 35
            [packaging_type_id] => 4
            [created_at] => 1397816303
            [updated_at] => 1397827441
            [manufacturer] => Array
                (
                    [id] => 33
                    [company_name] => Eywa Solutions
                    [email] => shweta.jain@creatywa.com
                    [domain_id] => 14
                    [created_at] => 1397481239
                    [updated_at] => 1397672832
                )

            [product_type] => Array
                (
                    [id] => 35
                    [product_type] => Milk
                    [created_at] => 1397816259
                    [updated_at] => 1397816259
                )

            [packaging_type] => Array
                (
                    [id] => 4
                    [packaging_type] => Pack
                    [bottles_per_pack] => 6
                    [created_at] => 1397641872
                    [updated_at] => 1397672081
                )

        )

    [1] => Array
        (
            [id] => 8
            [code] => LAP201
            [name] => Laptop an
            [manufacturer_id] => 41
            [product_type_id] => 32
            [packaging_type_id] => 5
            [created_at] => 1398088150
            [updated_at] => 1398088150
            [manufacturer] => Array
                (
                    [id] => 41
                    [company_name] => Dell
                    [email] => dell.india@test.com
                    [domain_id] => 8
                    [created_at] => 1397641666
                    [updated_at] => 1397672612
                )

            [product_type] => Array
                (
                    [id] => 32
                    [product_type] => Rum
                    [created_at] => 1397656687
                    [updated_at] => 1397672183
                )

            [packaging_type] => Array
                (
                    [id] => 5
                    [packaging_type] => Case
                    [bottles_per_pack] => 12
                    [created_at] => 1397672031
                    [updated_at] => 1397672031
                )

        )

)

将上述数组分配给smarty模板。 现在我在一个聪明的模板中打印上面的数组。以下是我尝试在智能模板中打印数组的代码,但它只显示了两个字段的数据。 代码和名称。能否帮助我纠正我在智能模板中打印其余数组数据时所犯的错误?

<table class="table table-bordered table-hover table-striped">
                <thead>
                  <tr>
                    <th>Sr. No.</th>
                    <th>Manufacturer</th>
                    <th>Product Code</th>
                    <th>Product Name</th>
                    <th>Product Type</th>
                    <th>Packaging Type</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  {assign var='i' value=1}
                  {section name=product loop=$all_products}
                  <tr>
                    <td>{$i}</td>
                    <td>{$all_products[product][manufacturer].comapany_name}</td>
                    <td>{$all_products[product].code}</td>
                    <td>{$all_products[product].name}</td>
                    <td>{$all_products[product][product_type].product_type}</td>
                    <td>{$all_products[product][packaging_type].packaging_type}</td>
                    <td align="center"><a href="{$control_url}products.php?op=edit&id={$all_products[product].id}" ><i class="icon-pencil"></i></a>&nbsp;&nbsp;&nbsp;<a href="{$control_url}products.php?op=delete&id={$all_products[product].id}" onClick="return ConfirmDelete()"><i class="icon-trash"></i></a></td>
                  </tr>
                  {assign var='i' value=$i+1}
                  {/section} 
                </tbody>
              </table>

从上面的代码中,以下两个语句正确显示相关数据,其他行没有显示任何内容:

<td>{$all_products[product].code}</td>
<td>{$all_products[product].name}</td>

所以请帮我正确显示其他数据。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

<table class="table table-bordered table-hover table-striped">
                <thead>
                  <tr>
                    <th>Sr. No.</th>
                    <th>Manufacturer</th>
                    <th>Product Code</th>
                    <th>Product Name</th>
                    <th>Product Type</th>
                    <th>Packaging Type</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  {assign var='i' value=1}
                  {section name=product loop=$all_products}
                  <tr>
                    <td>{$i}</td>
                    <td>{$all_products[product].manufacturer.company_name}</td>
                    <td>{$all_products[product].code}</td>
                    <td>{$all_products[product].name}</td>
                    <td>{$all_products[product].product_type.product_type}</td>
                    <td>{$all_products[product].packaging_type.packaging_type}</td>
                    <td align="center"><a href="{$control_url}products.php?op=edit&id={$all_products[product].id}" ><i class="icon-pencil"></i></a>&nbsp;&nbsp;&nbsp;<a href="{$control_url}products.php?op=delete&id={$all_products[product].id}" onClick="return ConfirmDelete()"><i class="icon-trash"></i></a></td>
                  </tr>
                  {assign var='i' value=$i+1}
                  {/section} 
                </tbody>
              </table>

您正在以错误的方式解析数组。我现在纠正了。它应该适合你。干杯!!!