I'm trying to write data from a query to an HTML table using PHP and MS SQL. I can successfully write the data of text, integer and float fields to the table. However, when I try to format date columns, the whole process seems to break.
<?php
echo "<table id='stakeholderTable' class='display nowrap'>
<thead>
<tr>";
foreach ($array as $colmn) {
echo '<th><strong>' . $colmn . '</strong></th>';
}
echo "</tr>
</thead>";
echo '<tbody>';
$sel_result = sqlsrv_query($con,$query);
while($sel_row = sqlsrv_fetch_array($sel_result)) {
echo '<tr>';
foreach ($array as $colmn) {
$colmn2 = str_replace("'", "", $colmn);
$joe = str_replace("tbl_actor.", "", $colmn2);
$joe = str_replace("tbl_commitment.", "", $joe);
if($joe == 'com_date' || $joe == 'com_due') {
echo '<td>' . $sel_row[$joe]->format('Y-m-d') . '</td>';
}else{
echo '<td>' . $sel_row[$joe] . '</td>';
}
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
?>
The code above code works as long as the value is not a date (com_date or com_due). If the value is a date, the first row of the table will print in the correct format, but no subsequent rows will print and the page's Javascript stops working.
The problem appears to be with the line:
echo '<td>' . $sel_row[$joe]->format('Y-m-d') . '</td>';
but I can't figure out what is wrong with it.
Any help would be greatly appreciated
Cheers,
$sel_row[$joe]is just array element type ofstringorinteger. but you are calling->format('Y-m-d')method. Those values are not objects - they don't have any methods to call