$this->Twitter->status('Merry Christmas!!!');
Sep 8

File Structure

There are basic files that are used in the standard pattern of module development:

  • mod_randomquotes.php - This file is the main entry point for the module. It will perform any necessary initialization routines, fetch the quotes in database and include the template which will display the module output.
  • mod_randomquotes.xml - This file contains information about the module. It defines the files that need to be installed by the Joomla! installer and specifies configuration parameters for the module.
  • tmpl/default.php - This is the module template. This file will take the data collected by mod_randomquotes.php and generate the HTML to be displayed on the page.
Creating mod_randomquotes.php
First restrict the direct access in your script.
defined( '_JEXEC' ) or die( 'Restricted access' );

then fetch the quotes from database

$db =& JFactory::getDBO();
$query = "SELECT quote FROM #__quotes ORDER BY RAND() LIMIT 1";
$db->setQuery( $query );
$showQuote = $db->loadResult();

note : do not use ORDER BY RAND() for selecting random records this is for tutorial only, use it at your own risk. 

and then render the default template

require( JModuleHelper::getLayoutPath( 'mod_randomquotes' ) );

Creating /tmpl/default.php
The default.php file is the template which displays the module output.

The code for the default.php file is as follows:

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
?>
<h5><?php echo $showQuote; ?></h5>

An important point to note is that the template file has the same scope as the mod_randomquotes.php file. What this means is that the variable $showQuote can be defined in the mod_randomquotes.php file and then used in the $showQuote file without any extra declarations or function calls.

Creating mod_randomquotes.xml

The mod_randomquotes.xml is used to specify which files the installer needs to copy and is used by the Module Manager to determine which parameters are used to configure the module. Other information about the module is also specified in this file.
The code for mod_helloworld.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<install type="module" version="1.5.0">
    <name>Random Quotes</name>
    <author>Ramsey Ramos</author>
    <creationDate>August 8th, 2008</creationDate>
    <copyright>Copyright (C) 2008 Ramsey Ramos. All rights reserved.</copyright>
    <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
    <authorEmail>ramsey.ramos@gmail.com</authorEmail>
    <authorUrl>http://ramseyramos.net</authorUrl>
    <version>0.0.1</version>
    <description>Random Quotes modules for Jayson Vega Website.</description>
    <files>
        <filename module="mod_randomquotes">mod_randomquotes.php</filename>
        <filename>index.html</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
    </files>
    <params>
    </params>
</install>

You will notice that there are two additional files that we have not yet mentioned: index.html and tmpl/index.html. These files are included so that these directories cannot be browsed. If a used attempts to point their browser to these folders, the index.html file will be displayed. These files can be left empty or can contain the simple line:

<html><body bgcolor=#FFFFFF"></body></html>

which will display an empty page.

And import this sql to your database :

CREATE TABLE IF NOT EXISTS `jos_quotes` (
  `id` int(11) NOT NULL auto_increment,
  `quote` tinytext collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=9 ;

INSERT INTO `jos_quotes` (`id`, `quote`) VALUES
(1, '"Di bale ng tamad nde naman pagod... - ramsey"'),
(3, '"Quote 2"'),
(7, '"Quote 3"'),
(8, '"Quote 4"');

You can download the file here ( mod_randomquotes.zip ).

Salamat nga pala kay master Jayson Vega! Siya kase ang may pakana ng lahat ng ito eh.

Cheers!

2 Responses to “how to create basic module in joomla 1.5”

  1. jsonv Says:

    grabe naiblog agad! aufs! meister!

  2. ramsey Says:

    breaktime eh. yosi mode muna.

Leave a Reply