3

I have an array $day and want to display this array in table form (I'm working with CI)

Array ( 
[START_EXECUTION] => 
    Array ( 
        [0] => 27-OCT-14 
        [1] => 28-OCT-14 
        [2] => 29-OCT-14 
          ) 
[NUM_OF_POPULATION] => 
    Array ( 
        [0] => 6171 
        [1] => 6990 
        [2] => 6882 
          ) 
[SUM_AMOUNT] => 
        Array ( 
        [0] => 361154716.01 
        [1] => 409210099.77 
        [2] => 407191552.71 
          ) 
)

Here is my code that I use in view :

<?php
if(count($day)>0){
    print_r($day);
    foreach($day as $index => $dt1_element){    
?>        
    <table class='table'>
        <tr>
        <td><?= $index ?></td>
        </tr>        
<?php
    foreach($dt1_element as $row){        
?>
        </tr>
        <td><?= $row ?></td>
<?php
    }
?>
        </tr>
<?php
    }
?>
    </table>
<?php
    }
?

But what I get is like this :

START_EXECUTION
27-OCT-14
28-OCT-14
29-OCT-14

NUM_OF_POPULATION
6171
6990
6882

SUM_AMOUNT
361154716.01
409210099.77
407191552.71

The result should be :

START_EXECUTION   NUM_OF_POPULATION   SUM_AMOUNT
27-OCT-14         6171                361154716.01
28-OCT-14         6990                409210099.77
29-OCT-14         6882                407191552.71

Kindly show me the correct foreach to get the desired result. Thank you

6 Answers 6

3

Try this:

echo '<table>';

$cols = array_keys($day);
echo '<tr>';
foreach ($cols as $col) echo '<th>' . $col . '</th>';
echo '</tr>';

foreach ($day[$cols[0]] as $i => $null) {
    echo '<tr>';
    foreach ($cols as $col) echo '<td>' . $day[$col][$i] . '</td>';
    echo '</tr>';
}

echo '</table>';

enter image description here

demo

Sign up to request clarification or add additional context in comments.

Comments

0

You are unnecessarily closing <tr>.

All you need is the child amounts on a separate row.

Corrected code:

<?php
if(count($day)>0){
    print_r($day);
    foreach($day as $index => $dt1_element){    
?>        
  <table class='table'>
        <tr>
        <td><?php echo $index;?></td>
        </tr>        
<?php
$tds = array();
    foreach($dt1_element as $row){        
                $tds[] = '<td>'.$row.'</td>';
?>
<?php
    }
    echo "<tr>". impldoe(' ', $tds) ."</tr>";
    ?>

    <?php
   }
?>
    </table>
<?php
    }
?>

1 Comment

Thank you for the answer but I still get same result
0

if you use PHP>=5.5 than

echo "<table>";

$cols = array_keys($day);
echo "<tr><th>";
echo implode('</th><th>', $cols);
echo "</th></tr>";

for ($i = 0, $num = count($cols); $i < $num; ++$i)
{
    echo "<tr><td>";
    echo implode('</td><td>', array_column($day, $i));
    echo "</td></tr>";
}

echo "</table>";

Comments

0

Here works fine

print "<table><tr><th>START_EXECUTION |</th><th>NUM_OF_POPULATION |</th><th>SUM_AMOUNT |</th></tr>";
//$index=0;
foreach($vals['START_EXECUTION'] as $index=>$values){

    echo "<tr><td>$values</td><td>".$vals['NUM_OF_POPULATION'][$index]."</td><td>".$vals['SUM_AMOUNT'][$index]."</td></tr>";
    //$index++;
} print '</table>';

for demo here

Comments

0

My response is inspired by the "Two-Step View" pattern where I build the logical data for the view to avoid any unnecessary information being in the view (for example, I'm not a fan of having array indices in views if I can help it):

<?php
    $arr = array(
        'START_EXECUTION'=>array(
            '27-OCT-14',
            '28-OCT-14',
            '29-OCT-14',
        ),
        'NUM_OF_POPULATION'=>array(
            6171,
            6990,
            6882,
        ),
        'SUM_AMOUNT'=>array(
            361154716.01,
            409210099.77, 
            407191552.71,
        ),
    );

    $headers = array_keys($arr);

    $body = array_map(function($a, $b, $c) { return array($a, $b, $c); },
        $arr['START_EXECUTION'],
        $arr['NUM_OF_POPULATION'],
        $arr['SUM_AMOUNT']
    );

    $table = array(
        'headers'=>$headers,
        'body'=>$body,
    );
?>

$table will contain the logical structure of the table in a view independent way.

We can create a simple widget that will convert the logical data into a html table like so:

 <table class="table">
    <thead>
    <tr>
        <?php foreach($table['headers'] as $header): ?>
            <th><?php echo $header; ?></th>
        <?php endforeach; ?>
     </tr>
     </thead>
     <tbody>
     <?php foreach ($table['body'] as $row): ?>
     <tr>
     <?php foreach ($row as $col): ?>
          <td><?php echo $col; ?></td>
     <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

As long as you maintain the same logical structure, the html code snippet can be placed inside a "widget" and that's the beginning of a table renderer.

Note This is just meant as a prototype and is not intended to be a comprehensive solution.

Comments

0
$data = array($FIELDS);//field value like name,email,password etc.
foreach($data as $row)
{
    echo "<tr>";
    foreach($row as $column){
    echo "<th>".$column."</th>";
}
echo "</tr>";
}

you can create array of field and add field data in array try this ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.