Echo html table from PHP array

时间:2016-10-19 13:32:38

标签: php html multidimensional-array

I'm learning php and I'm stuck with one task.. I have an array looking like this:

$data[1]["First"]   = "Iva";
$data[1]["Last"]    = "Ivić";
$data[1]["Date"]    = "2016-09-29";
$data[1]["Paid"]    = "Yes";

$data[2]["First"]   = "Petar";
$data[2]["Last"]    = "Perić";
$data[2]["Date"]    = "2016-02-08";
$data[2]["Paid"]    = "No";

$data[3]["First"]   = "Tomo";
$data[3]["Last"]    = "Tomić";
$data[3]["Date"]    = "2015-08-22";
$data[3]["Paid"]    = "Yes";

I need to echo HTML table from this Array. On the value ['Paid'], html table should output select field and showing current value as selected. Also, first column of the table should assign #ID number.

So far, I've managed to create the table, but I can't figure out how to count and echo # and getting the select field to work. Second value doesn't work for me.

<table border="1" align="center" width="80%">
<tr bgcolor="yellow">
    <th>ID #</th>
    <th>First</th>
    <th>Last</th>
    <th>Date</th>
    <th>Paid</th>
</tr>
<?php 

foreach ( $data as $info )
{
// date transform
$date = explode('-', $info['Date']);
//var_dump($date); 

echo '<tr align="center" bgcolor="#f6f6f6">';
echo ' <td>' '</td>';// How to count #IDs starting from 1?     
echo '<td>'. $info['First'] .'</td>';
echo '<td>'. $info['Last'] .'</td>';
echo '<td>'. $date[2].'.'.$date[1].'.'.$date[0].'.</td>';
//echo '<td>'. $info['Paid'] .'</td>';

echo '<td>';
echo '<select name="Paid">';
echo '<option value="0" selected>'. $info['Paid'] .'</option>';
echo '<option value="1">'. $info['Paid'] .'</option>';//This is where I'm stuck
echo '</select>';
echo '</td>';
echo '</tr>';
}
?>
</table>
?>

Thanks.

3 个答案:

答案 0 :(得分:2)

您可以使用大量循环来实现此目的,最简单的情况是foreach()循环。

<!-- conditional loops -->
<?php foreach($data as $single): ?>

    <!-- shorthand PHP statement to echo the result -->
    <td> <?= $single['First']; ?> </td>

<?php endforeach; ?>

注意 - 数组索引从0开始,而不是1.因此第一个值为$data[0]['First']

如果要为其中的每个数组创建表,可以使用嵌套循环,如下所示:

<?php for($i = 0; $i <= count($data); $i++):
    foreach($data[$i] as $key => $val): ?>
        <td>
            <!-- ternary expression -->
            <tr> <?= ($key == "First") ? $val : ""; ?> </tr>
        </td>
    <?php endforeach;
endfor;

如果您更喜欢不同的方法,可以使用while()循环来实现相同的想法:

<?php $i = 0;
while($i != count($data)): ?>
    <tr> <?= $data[$i++]['First']; ?> </tr>
<?php endwhile;

注意未来 - 使用bool数据类型而不是&#39; yes&#39;,&#39; no&#39;字符串。例如:

$paid = true;
echo ($paid) ? 'selected' : '';

而不是像:

echo ($paid == 'No') ? '' : 'selected';

答案 1 :(得分:1)

通过对代码进行微调,您只需检查付费值,如果符合您的选项,请添加所选属性。

您可以将$ data键用作ID。

<?php

$data[1]["First"]   = "Iva";
$data[1]["Last"]    = "Ivić";
$data[1]["Date"]    = "2016-09-29";
$data[1]["Paid"]    = "Yes";

$data[2]["First"]   = "Petar";
$data[2]["Last"]    = "Perić";
$data[2]["Date"]    = "2016-02-08";
$data[2]["Paid"]    = "No";

$data[3]["First"]   = "Tomo";
$data[3]["Last"]    = "Tomić";
$data[3]["Date"]    = "2015-08-22";
$data[3]["Paid"]    = "Yes";

?>
<table>
    <thead>
    <tr>
        <th>ID #</th>
        <th>First</th>
        <th>Last</th>
        <th>Date</th>
        <th>Paid</th>
    </tr>
</thead>
<tbody>
<?php 
foreach ( $data as $key => $info )
{
    $date = explode('-', $info['Date']);
    echo '<tr>';
        echo '<td>' . $key . '</td>';
        echo '<td>'. $info['First'] .'</td>';
        echo '<td>'. $info['Last'] .'</td>';
        echo '<td>'. $date[2].'.'.$date[1].'.'.$date[0].'.</td>';
        echo '<td>';
            echo '<select name="Paid">';
            $selected = $info['Paid'] == 'Yes' ? 'selected' : '';
            echo "<option value=0 $selected >Yes</option>";
            $selected = $info['Paid'] == 'No' ? 'selected' : '';
            echo "<option value=1 $selected >No</option>";
            echo '</select>';
        echo '</td>';
    echo '</tr>';
}
?>
</table>

我可能分别使用Yes和No作为选项的值或1和0,或者只使用复选框。但每个都是他们自己的。

答案 2 :(得分:0)

The first entry in an array is the zeroth entry. So if you declare the array, do it like this:

$data = [
   [ // 0
      "First" => "Iva",
      "Last" => "Ivic",
      "Date" => "2016-09-26",
      "Paid" => "Yes",
   ], 
   [ // 1
      // etc.
   ],
];

Next, if you want the indexes in the foreach loop, you should do this:

foreach($data as $index => $info){
    // $index will now be 0 in the first iteration, 1 in the second etc.
    echo "<td>#" . ($index + 1) . "</td>";
}