Is it possible to dynamically build a table using PHP?
I am using code such as the below to build tables etc. but as there are multiple I was wondering in this case is it possible for the table to be built dynamically based on the SQL query?
For example if I need to build another table then I can copy the below code and just edit the SQL rather than also editing the column headers etc. in the HTML.
Basically some method to simplify the below code so that it is more compact\tidier if being used multiple times on the same page.
Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('core/connection.php');
if($conn){
$stid = oci_parse($conn, "
SELECT *
FROM
(
SELECT c1, c2, c3, c4
FROM t1
ORDER BY c1
)
WHERE ROWNUM <= 10
");
oci_execute($stid);
echo "<table class='table table-hover '>
<thread>
<tr>
<th>c1</th>
<th>c2</th>
<th>c3</th>
<th>c4</th>
</tr>
</thread>
<tbody>";
while ($row = oci_fetch_array($stid, OCI_NUM)) {
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "</tr>";
unset($row);
}
echo "</tbody>
</table>";
oci_free_statement($stid);
oci_close($conn);
}
tablify
class or function? something liketablify($headings, $rows);
? It really depends on how complex you want to go...