Joseph L. LeBlanc
homeportfoliojoomlabiocontactblog
articles
guides
tutorials
all
forum
Subscribe in NewsGator Online

Learning Joomla! for the first time? Buy the Joomla! 1.5 Essential training CD-ROM or watch it on Lynda.com.

 

Welcome to Joseph LeBlanc's Joomla resources. First time visitors will find the Daily Message Component Tutorial to be helpful. (Daily Message for Joomla 1.0 is also available, as well as many other tutorials based around it). In addition to the tutorials, there are articles covering conceptual topics, such as the oft-asked question "What is the difference between a component and a module?" To receive alerts for new tutorials and articles, choose a newsfeed at the right appropriate for your newsreader.

PDF Print E-mail
dailymessage.php
This file displays what site visitors will see when the Daily Message component is loaded. Although this code is simple, it is all that is needed to generate HTML for any Joomla installation. It is the only file that is copied to the components/com_dailymessage directory. Let's step through the component:

<?

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

 

This line secures your component by making sure that Joomla and only Joomla is calling this file.

 

global $database;

 

The $database object is declared elsewhere in Joomla. 'global' makes it available here.

 

// get configuration information

$database->setQuery("SELECT * FROM mos_joe_dailymessage_conf LIMIT 1");

 

$rows = $database->loadObjectList();

$row = $rows[0];

 

Running a MySQL query in Joomla is a two step process. First, you use the setQuery() function to enter the SQL statement. Then you use a function such as loadObjectList() to run and retrieve the results. The line $row = $rows[0] stores the variables from the first object in the list in the array $row. In this case, we are retrieving the configuration information from the database, which should only be one record.

 

Also, in MySQL queries through Joomla, '#_' is replaced with the appropriate table name prefix. This makes it possible for applications aside from Joomla capable of sharing the same database (currently 'mos'). This functionality can be helpful if you are running on a budget host that only allows one database.

 

$bold = $row->bold;

$italic = $row->italic;

$underline = $row->underline;

$showdate = $row->showdate;

 

After loading the result into an array, we are able to retrieve the individual values from the query result into variables we can use later.

 

$starttags = "";
$endtags = "";

 

if($bold == 1)

{

$starttags .= "<b>";

$endtags = "</b>" . $endtags;

}

 

if($italic == 1)

{

$starttags .= "<i>";

$endtags = "</i>" . $endtags;

}

 

if($underline == 1)

{

$starttags .= "<u>";

$endtags = "</u>" . $endtags;

}

 

These statements build ending and beginning tag strings based on the settings from the database. These strings will be used below to format the message.

 

//get data and output accordingly

 

$database->setQuery("SELECT * FROM mos_joe_dailymessage WHERE published = '1'");

 

$rows = $database->loadObjectList();

 

Using the same $database object as above, we set the query to now pull all published items from the table containing messages, then pull all the objects into $row.

 

?><table><?

 

Now we start outputting HTML code. The ?> allows us to escape from PHP temporarily to output what will be rendered as HTML until we reach a <? token. This produces the same results as echo().

 

foreach($rows as $row)

{

if($showdate == 1)

echo "<tr><td>" . mosFormatDate($row->date) . "</td>";

else

echo "<tr>";

 

echo "<td>" . $starttags . $row->message . $endtags . "</td></tr>";

}

 

This loop cycles through each object in $rows and loads the contents into $row. Then we are able to pull the information out and build strings that will render properly formatted HTML.

 

?></table>

 

Outputs the closing table tag.