PHP Foreach array to display the table

I have an array $day

and want to display this array as a table (I am 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 am using in the 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 I get 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

      

Please show me the correct foreach to get the desired result. Thanks you

+3


source to share


6 answers


Try the following:

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

+3


source


You don't necessarily close <tr>

.

All you need is the number of children on a separate line.



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

      

0


source


if you are using 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>";

      

0


source


Works great here

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

0


source


My answer is inspired by the "Two-Step View" pattern, in which I create boolean data for a view to avoid unnecessary information in the view (for example, I'm not a fan of having array indices in views if I can help):

<?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 regardless of the view.

We can create a simple widget that converts boolean data into an html table like this:

 <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 snippet can be placed inside the "widget" and at the beginning of the table rendering.

Note This simply means a prototype and is not intended to be a complete solution.

0


source


$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 a field array and add field data to the array, try this ...

0


source







All Articles