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:
{source}<?php
$query = "SELECT something from #__mydatabasetable WHERE this = 'that'";
$database->setQuery($query);
$result = $database->loadResult();
// Do something with $result
?>{/source}
Or better (more stable):
{source}<?php
$query = $db->getQuery(true)
->select($db->quoteName('something'))
->from('#__mydatabasetable')
->where($db->quoteName('this').' = '.$db->quote('that'));
$db->setQuery($query);
$result = $database->loadResult();
// Do something with $result
?>{/source}
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:
{source}<?php
$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}