Building html tables from query data ... faster?

Thanks to my limited experience / knowledge, I use the following structure to generate HTML tables on the fly from MySQL queries:

$c = 0;
$t = count($results);

$table = '<table>';

while ($c < $t) {
   $table .= "<tr><td>$results[0]</td><td>$results[1]</td> (etc etc) </tr>";
   ++$c;
}

$table .= '</table>';

it works, obviously. But for tables with more than 300 rows, a delay in pageload is noticeable, and the script builds the table. Currently, the list of maximum results is only about 1,100 lines, and wait a short time, but obviously wait.

Are there other HTML table output methods that are faster than my WHILE loop? (PHP only please ...)

+3
source share
6 answers

Are there other methods for outputting an HTML table that are faster than my WHILE loop?

. PHP - . , ! , , , - IMO .

<table>
    <?php foreach($results as $result) { ?>
        <tr>
            <td><?php echo htmlspecialchars($result[0]); ?></td>
            <td><?php echo htmlspecialchars($result[1]); ?></td>
        </tr>
    <?php } ?>
</table>

( htmlspecialchars. , HTML, HTML-, . , h, echo htmlspecialchars, , HTML-.)

: , . :

  • . , zlib.output_compression, mod_deflate -, .

  • . , CSS table-layout: fixed <table> <col> width , .

+1

-, , HTML, PHP . -, , .

script PHP :

  • ob_start(); ob_get_clean(); - , html :

    ob_start();
    // your code here
    echo ob_get_clean();
    
  • :

    $str = array();
    $str[] = 'add the strings';
    echo implode($str);
    

BR.

+4

, , - . , , .

echo, .

- , , for ( for each) , while. , , , , .

+1

, PHP. HTML - .

HTML. , .. , ,

+1

XML . .

, . , , xml. xml . , . . , .

If you feel that the above logic is still slow and you have even more data, do the following: When the page loads the first load of 100 data tables. Then run the ajax request and get the next 100 nodes from the xml file and display it. Once this is finished, run an ajax request to get the next 100 nodes from the xml file and show it on the page, etc.

0
source

Source: https://habr.com/ru/post/1740107/


All Articles