Are there other methods for outputting an HTML table that are faster than my WHILE loop?
Potentially yes. PHP is a templating language. Use it as such, don't fight it! Native templating is likely to be faster than laboriously sticking strings together manually — and IMO more readable.
<table>
<?php foreach($results as $result) { ?>
<tr>
<td><?php echo htmlspecialchars($result[0]); ?></td>
<td><?php echo htmlspecialchars($result[1]); ?></td>
</tr>
<?php } ?>
</table>
(Note the use of htmlspecialchars
. If you don't use this function every time you insert plain text into HTML, your code has HTML-injection flaws, leading to potential cross-site-scripting vulnerabilities. You can define a function with a short name like h
to do echo htmlspecialchars
to avoid some typing, but you must HTML-encode your strings.)
However: whilst this is prettier and more secure, it is unlikely to be significantly faster in practice. Your client-side rendering slowness is almost certainly going to be caused more by:
network transmission speeds for a lot of table data. You can improve this by deploying with zlib.output_compression
, mod_deflate
or other compressing filter for your web server, if you have not already.
rendering speeds for large tables. You can improve this by setting the CSS style table-layout: fixed
on your <table>
element and adding <col>
elements with explicit width
styles for the columns you want to have a fixed width.