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.
<?php echo ( $info[ 'Paid' ] == 0 ? 'selected' : '' ); >on the second option replace the 0 for a 1. This is only really useful for this example as it is not easily expandable.echo '<option value="0" <?php echo ( $info[ 'Paid' ] == 0 ? 'selected' : '' ); >>'. $info['Paid'] .'</option>'; echo '<option value="1" <?php echo ( $info[ 'Paid' ] == 1 ? 'selected' : '' ); >>'. $info['Paid'] .'</option>';