Question

I've an array titled $all_products in PHP as follows:

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
                )

        )

)

The above array is assigned to a smarty template. Now I've to print the above array in a smarty template. Following is the code I tried to print the array in smarty template but it's displaying only two field's data properly viz. code and name. Can you please help me in correcting the error I'm making in printing the rest of the array data in smarty template?

<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>

From above code the following two statements are displaying the concerned data properly, other lines are displaying nothing:

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

So please help me in displaying the other data properly.

Was it helpful?

Solution

Try the following code:

<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>

You are parsing the array in wrong way. I've corrected it now. It should work for you. Cheers!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top