When placing PHP code, you have access to anything available in PHP itself. But also to anything Joomla makes available.

This means you can access and output any session data, user data, connect to the database, and a whole lot more.

If you know your PHP, you know that the sky is the limit!

Ready-to-use PHP Variables & Objects

Sourcerer checks your PHP code to see if you are referencing any commonly used Joomla objects / variables, and then creates them for you.

This means you don't have to create these variables yourself every time.

Currently Sourcerer creates these variables ready for use:

  • $app The Joomla application framework
  • $document or $doc The html document
  • $database or $db The database
  • $user The user object containing the details of the guest or current logged in user
  • $Itemid The menu id of the page
  • $article The article object (only available when using the code inside articles)

Connecting to the Database

Internal Database

If you want to do database calls to get data from it (or write data to it), you can use Joomla's built-in database object and functions.

Sourcerer already creates the $database (or $db) object for you when it is needed. So you do not have to initialize this yourself.

So for example, you can directly do:

$query = "SELECT something from #__mydatabasetable WHERE this = 'that'";

$result = $database->loadResult();

// Do something with $result

Or better (more stable):

$query = $db->getQuery(true)
->where($db->quoteName('this').' = '.$db->quote('that'));

$result = $database->loadResult();

// Do something with $result

For more information on how to use the Joomla Database object: docs.joomla.org/How_to_use_the_database_classes_in_your_script

External database

To connect to an external database, use:

  $option = array(
    'driver'   => 'mysql', // Database driver name
    'host'     => 'db.myhost.com', // Database host name
    'user'     => 'my_username', // User for database authentication
    'password' => 'my_password', // Password for database authentication
    'database' => 'my_database', // Database name
    'prefix'   => 'abc_', // Database prefix (may be empty)
  $db = JDatabaseDriver::getInstance( $option );

// Do something with the $db object ?>{/source}