With PHP i’m rendering an HTML table which look somewhat like this:
--------------------------------------------------
| title | description | actions |
-------------------------------------------------|
| title1 | desc1 | edit / delete |
| title2 | desc2 | edit / delete |
| title3 | desc3 | edit / delete |
| title4 | desc4 | edit / delete |
--------------------------------------------------
The code works like this (stripped down version! no thead, tbody etc. in this examle):
<table>
<?php foreach ( $tableData as $row ): ?>
<tr>
<!-- Render columns (title, description)
<?php foreach ( $columnData as $column ): ?>
<td>
<?= $row[$column['name']]; ?>
</td>
<?php endforeach; ?>
<!-- Render actions -->
<?php foreach ( $actions as $action ): ?>
<td>
<?= include($action); // include the proper action button! ?>
</td>
<? endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
This gives me the desired result. But I only have one problem with this. It gets kinda slow when I have 1000+ records. I already noticed that this is because i’m doing an include for every table row. When I remove the include then everything runs very fast.
The included file contains some PHP logic. So I can’t just do a file_get_contents. Well, I could, but then I have to use eval() to parse the contents. But i’d rather not use that function at all.
So now i’m wondering if it is possible to cache the included file somehow? So that PHP don’t actively have to include the actual file over and over again but gets it from its cache? Is something like this possible?
Or are there any better alternatives?
You can put all actions (functions) in one file and include that file once. Just in loop you will call different functions according to their name. Variable function example.