Creating really dynamic pages in Joomla!

Joomla! is a very popular CMS (Content Management System) which allows you to build websites and powerful online applications. In fact, the lines that you read right now belong to a Joomla! website.

Although the CMS is dynamic, I have found that, oddly, the articles (i.e. the content) is rather static. Of course you can include videos, or a gallery of images in the article, but what about the actual text? How can you change it dynamically?

A method that I suggest requires a free game-changing extension: DirectPHP. This extension allows you to embed PHP code right within the content. This opens up a whole new set of possibilities. Of course, you must be more careful now and frequent backups is a must (Akeeba is highly recommended).

After installing and configuring DirectPHP we are ready to go. Suppose that we have a product list with the corresponding prices in a table somewhere in our database. In our scenario, the table ("products") contains two columns, "code" and "price". The task is to search for a specific product code (say "011-602051") and display the corresponding price with a trailing euro sign. Here's the code, which is included (just as in this article) as simple text (in the editor):

The price is <?php 
$db = JFactory::getDBO();
$query = "
SELECT price
FROM ".$db->nameQuote('#__products')."
WHERE code = '011-602051';
";
$db->setQuery($query);
$result = $db->loadResult();
print($result);
?> €

That's it! The text will be actually executed by the web server and the result (in the browser) will be something like The price is 12.34 €. You don't need to worry about the type or the prefix of the database, since the code uses Joomla! wrapper functions. When the database table changes, the Joomla! article will also change automatically

But hey! How can I actually see the code in this very article and the web server doesn't execute it? The reason is simple: I have not installed DirectPHP in this site.