Search code examples
phphtmlcodeigniterhtml-tabledompdf

Array representation in a specific way of table format in codeIgniter view file?


I have an array in the below format. This array had around 100 elements in same format. I want to represent the whole array in a specific format of table using DOMPDF library using codeIgniter.

result = Array(
              [0] => Array (
                     ['brand'] => x,
                     ['product'] =>p1,
                     ['language']=>English,
                    );
              [1] => Array (
                     ['brand'] => x,
                     ['product'] =>p2,
                     ['language']=>English,
                    );
              );

I like to represent them in the below format using <table> tags in html.

  Brand          x             y
  Product        p1            p2
  Language       English       English

I am looking for ideas. I have those kind of array elements not 2 more than 100. I want to loop them.

So after request below i am going to post my code.

<?php foreach($result as $res)

 {
  <table>
    <tr>
        <td>Brand</td>
        <td><?php echo $res['brand'] ?></td>
    </tr>
    <tr>
        <td>Product</td>
        <td><?php echo $res['product'] ?></td>
    </tr>
    <tr>
        <td>Language/td>
        <td><?php echo $res['language'] ?></td>
    </tr>
  </table>
}

This will give you output for one product like this. Brand x
Product p1
Language English

How I can do loop for other products to get output of how I want above?

Note: not only 3 fields in each array element. I have more than 30 fields. May be you think I am going this way only because of print able area in the PDF page.


Solution

  • @Vengat, following coding are working Good. Try This One...

    <?php
    $result = Array(
                  0 => Array (
                         'brand' => 'x',
                         'product' =>'p1',
                         'language'=>'English',
                        ),
                  1 => Array (
                         'brand'=> 'y',
                         'product' =>'p2',
                         'language'=>'English',
                        )
                  );
    
    
        echo '<table>';
        echo '<tr><td>Brand</td>';
        foreach($result as $res)
        {
            echo "<td>".$res['brand']."</td>";
        }
        echo '</tr>';
        echo '<tr><td>Product</td>';
        foreach($result as $res)
        {
            echo "<td>".$res['product']."</td>";
        }
        echo '</tr>';
        echo '<tr><td>Language</td>';
        foreach($result as $res)
        {
            echo "<td>".$res['language']."</td>";
        }
        echo '</tr>';   
        echo '</table>';
    
    ?> 
    

    Output is:

    Brand   x   y
    Product p1  p2
    Language    English English
    

    Use css for alignment.