Search
Other Sourcerer questions
Forum

Performance of php echo of HTML table

Robin Hair's Avatar Robin Hair
When I embed php code directly in Sourcerer with HTML Table 'echo' statements like this:
echo '<td style="text-align: left;">Some long text</td>';
it is very slow to complete the display of an HTML Table with 500 rows, and 10 columns of text. As per the sample, I echo each Table cell with single quoted statements.

However, when I copy the very same php code into a .php file on the server and call it from Sourcerer Javascript via ajax, it displays the results almost immediately. In this case I'm displaying the resulting HTML Table with a single Javascript jQuery command as part of the ajax code:
jQuery('#htmlOutput').html(data);

The timings are:
10 seconds for the embedded php code in Sourcerer
1 second for the server based php code with an ajax call in Sourcerer

Please can you comment as to why there is such a performance hit using php 'echo' statements to build an HTML Table directly within Sourcerer.

Thanks
Robin
Peter van Westen's Avatar Peter van Westen ADMIN
Are you outputting every <td> via its own {source} block?

What does your article/content look like?
Please post a rating at the Joomla! Extensions Directory
Robin Hair's Avatar Robin Hair
I have just one {source} for the article, and the php code is within a single <?php..?>. This is the dummy code I tested with in a loop displaying 500 rows. This exact code was then copied to a server based .php for comparison. My original code has an SQL Select to get the data for the HTML table, but I removed that so as not to cloud the result.

Here is the php code I used for the test:
<?php

echo '<div class="content">';
echo '<table border="1" align="left" class = "fixed">';
echo '<tbody>';
for ($i = 0; $i < 500; $i++) {
    	echo '<tr><td style="text-align: center;" class="table-lotno">1840</td>';
    	echo '<td class="table-lotdescription">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede</td>';
    	echo '<td style="text-align: right;">100</td>';
    	echo '<td style="text-align: right;">200</td>';
    	echo '<td style="text-align: right;">300</td>';
    	$ahref = '<a href="https://xxxx.xxx/images/2025-02/1840a.jpg" target="_blank">';
    	$imgsrc = '<img src="https://xxxx.xxx/images/2025-02//thumb1840a.jpg" ';
    	$imgsrc .= ' alt="Click for larger image" title="Thumb lot 1840" border="0" />'; 
    	echo '<td>' . $ahref . $imgsrc . '</td>';
    	echo '<td style="text-align: left;">24/2/2025 10:30:00</td>';
    	echo '<td>none</td>';
    	echo '<td>none</td>'; 
    	echo '<td class="table-purchase">none</td>'; 
    	echo '</tr>';
}
echo '</tbody></table>';
echo '</div>';

?>
Peter van Westen's Avatar Peter van Westen ADMIN
It is not easy to pinpoint what would be the reason for a difference in execution time, as the 2 methods you are using are completely different. My initial thoughts are these:

I don't think it is (just) the php process of Sourcerer here.

When doing this inline, the enormous amount of html it generates will be kept in memory and will get (re)placed into the content. This is then passed on to all the other Joomla content and system plugins that do whatever with the content. And also it goes through all the other Joomla core processes, before finally being injected into your template and final html output.

When using javascript to load the content, that php file is completely stand-alone. Neither Joomla nor all the plugins you have running will touch the html output.

So all in all: It is pretty impossible for me to tell you what parts of the complete rendering process are slowing the execution down.
Solution to most of these things: caching.
Please post a rating at the Joomla! Extensions Directory
Robin Hair's Avatar Robin Hair
Thanks for the detailed response. I certainly hadn't realised inline php echos of HTML content invoked lots of other potential extra processing by Joomla and plugins etc. Sourcerer makes it all to easy to code php inline, and this is often a handy proving ground towards quick webpage development. However, knowing Javascript displays HTML content without extra processing, means an Ajax call to php code on the server is the best way to implement a large display of an HTML table.

I'm still building up my Javascript/php coding skills, and have another quick question about inline php and javascript...

I recently discovered you could echo php values into Javascript variables like this:

var myFlag = "<?php echo $myFlag; ?>";

Does this only work if your php variable is declared within inline php in the same sourcerer source code? I assume this wouldn't work if the php variable was declared in Ajax linked php code based on the server?
Peter van Westen's Avatar Peter van Westen ADMIN
means an Ajax call to php code on the server is the best way to implement a large display of an HTML table.
Correct, if you don't want any Joomla plugins to further affect it.
If you want to use plugin tags (like placing modules and such) or use an Email Protector, etc... then you will need to run the content through Joomla.
Does this only work if your php variable is declared within inline php in the same sourcerer source code? I assume this wouldn't work if the php variable was declared in Ajax linked php code based on the server?
The variables need to be available in that code block (or any Sourcerer php code block above it).
PHP code called via ajax is executed AFTER the main page and any php code in there is not accessible to php code outside it.
var myFlag = "<?php echo $myFlag; ?>";
Make sure you escape the content of the php variables if the can contain quotes.
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription