JOOMLA FICTION LAB
(11 votes)

How to create a database driven Joomla module

Wednesday, 03 August 2011 12:46
In this tutorial we will create a fresh new module for Joomla 1.7 that is driven by a MySQL database.
The process is very fast and easy and if you follow the guidelines below you will get the hang of it very quickly.

The features:

In this module we will use the table jos_users to retrieve the id, name and username of the registered users.

In the module parameters we will be able to control the user data we want to display.

Step 1

The file structure


Create the file structure that you see below, using your favorite file editor.
Tip: I am using a great free text / code editor called HTML-Kit. You can download it for free here.
mod_userdata.xml
mod_userdata.php
helper.php
index.html
tmpl/default.php
tmpl/index.html
This is the most basic file structure for a database driven module.

Step 2

The files


Now let's populate our files.
1. Open the file mod_userdata.xml and insert this code
<?xml version="1.0" encoding="UTF-8"?>
<extension type="module" version="1.7" client="site" method="upgrade">
 <name>User Data Module</name>
 <author>Minitek.gr</author>
 <creationDate>03/08/2011</creationDate>
 <copyright>Copyright (C) 2011. All rights reserved.</copyright>
 <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
 <authorEmail>your_email</authorEmail>
 <authorUrl>www.minitek.gr</authorUrl>
 <version>1.7.1</version>
 <description>Users Data Module</description>
 <languages>
 </languages>
 <files>
  <filename module="mod_userdata">mod_userdata.php</filename>
  <filename>mod_userdata.xml</filename>
  <filename>helper.php</filename>
  <filename>index.html</filename>
  <folder>tmpl</folder>
 </files>
 <config>
  <fields name="params">
   <fieldset name="basic">
    <field name="moduleclass_sfx" type="text" default="" label="Module Class Suffix" description="Suffix for individual css styling" />
    <field name="limit" type="text" default="10" label="Limit Displayed Users" description="Limit Displayed Users" />
    <field name="user_id" type="radio" default="1" label="Display user ID" description="Display user ID">
     <option value="0">JNO</option>
     <option value="1">JYES</option>
    </field>
    <field name="user_name" type="radio" default="1" label="Display Name" description="Display Name">
     <option value="0">JNO</option>
     <option value="1">JYES</option>
    </field>
    <field name="user_username" type="radio" default="1" label="Display Username" description="Display Username">
     <option value="0">JNO</option>
     <option value="1">JYES</option>
    </field>
   </fieldset>
  </fields>
 </config>
</extension>
This is the typical structure of a module xml file for Joomla 1.7.

2. Open the file mod_userdata.php and insert this code
<?php
/**
 * @package	Joomla.Site
 * @subpackage	mod_userdata
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license	GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die;

// Include the syndicate functions only once
require_once dirname(__FILE__).'/helper.php';

// Get the user data
$list	= modUserDataHelper::getData($params);

// Get the layout
require JModuleHelper::getLayoutPath('mod_userdata', $params->get('layout', 'default'));
3. Now let's move on to the file helper.php. This is the file that retrieves the data from the MySQL table.
Open the file helper.php and insert this code
<?php
/**
 * @package		Joomla.Site
 * @subpackage	mod_userdata
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die;

class modUserDataHelper
{
  function getData( &$params )
  {
	 
   // Database query		
   $list = array();			
   $query = " SELECT id, name, username "				
   ." FROM #__users "
   ." WHERE block=0 "				
   ." ORDER BY id DESC "
   ." LIMIT " . $params->get( 'limit' );				
   $db =& JFactory::getDBO();
   $db->setQuery( $query );		
   $rows = $db->loadObjectList();
					
   // Get list items
   if ($rows!=null)
   {
    $i=0;
    foreach ($rows as $row) 
    {				
     $list["users"][$i]["id"]=$row->id;
     $list["users"][$i]["name"]=$row->name;
     $list["users"][$i]["username"]=$row->username;
     $i++;		
    }
    return $list;
   }
					
  }
}
In this file, we firstly request the id, name and username from the table jos_users where the users are not blocked (block=0). Then we order the results by id and finally we limit the results to the limit that is defined in the module backend parameters. Let's move on to our template file.

4. Our template file is responsible for displaying the results.
Open the file tmpl/default.php and insert this code
<?php
/**
 * @package		Joomla.Site
 * @subpackage	mod_userdata
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die; ?>

<div class="moduletable<?php echo $params->get( 'moduleclass_sfx' ) ?>">

 <ul>
  <?php for ($i=0;$i< sizeof($list["users"]); $i++) { ?>	
	
	 <li>
	  <?php if ($params->get( 'user_id' )) { ?>
		 <span><?php echo $list["users"][$i]["id"];?></span>
		<?php } ?>
		<?php if ($params->get( 'user_name' )) { ?>
		 <span><?php echo $list["users"][$i]["name"];?></span>
		<?php } ?>
		<?php if ($params->get( 'user_username' )) { ?>
		 <span><?php echo $list["users"][$i]["username"];?></span>
		<?php } ?>
	 </li>
	
	<?php } ?>
 </ul>

</div>
5. Open the file index.html as well as the file tmpl/index.html and insert this code
<html><body bgcolor="#FFFFFF"></body></html>
This will prevent unauthorized directory browsing.
Now we are ready to pack our files and install the new module.

Step 3

Let's pack the files and install


Zip all the files we created in an archive called mod_userdata.zip. Now go to Joomla Administration -> Extension Manager and install the package. When the installation is complete, go to Module Manager, open the module, enable it and configure the parameters.
Go to frontend and check out what you have created! Have fun!




Add comment


Security code
Refresh