Create a Custom Grid in PHP With Datatable

Hi Guys,

In PHP, you can create a custom grid using Datatable.
If you want to create a custom grid using Datatable, you can use the following code snippet:

// Add HTML Code

<table class="table" id="tableTestId" cellpadding="5" cellspacing="5" width="100%">
<thead>
<tr>
<th align="center">ID</th>
<th align="center">Column1</th>
<th align="center">Column2</th>
</tr>
</thead>
<tbody>
<?php foreach ($phpResults as $result) { ?>
<tr>
<td align="center"><?php echo $result["id"]; ?></td>
<td style='text-align:center'><?php echo $result['column1'] ?></td>
<td style='text-align:center'><?php echo $result['column2']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

/******Add jQuery Code for Data tables**********/
Add below scripts to you code to work with Datatables:

https://code.jquery.com/jquery-3.3.1.js
https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js

After adding the above links, use the following:

$(document).ready(function() {
$('#tableTestId').DataTable();
});

That’s it you are done!

 

Leave a Reply