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.
1.
mod_userdata.xml
2.
mod_userdata.php
3.
helper.php
4.
index.html
5.
tmpl/default.php
6.
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
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
extension
type
=
"module"
version
=
"1.7"
client
=
"site"
method
=
"upgrade"
>
03.
<
name
>User Data Module</
name
>
04.
<
author
>Minitek.gr</
author
>
05.
<
creationDate
>03/08/2011</
creationDate
>
06.
<
copyright
>Copyright (C) 2011. All rights reserved.</
copyright
>
08.
<
authorEmail
>your_email</
authorEmail
>
09.
<
authorUrl
>www.minitek.gr</
authorUrl
>
10.
<
version
>1.7.1</
version
>
11.
<
description
>Users Data Module</
description
>
12.
<
languages
>
13.
</
languages
>
14.
<
files
>
15.
<
filename
module
=
"mod_userdata"
>mod_userdata.php</
filename
>
16.
<
filename
>mod_userdata.xml</
filename
>
17.
<
filename
>helper.php</
filename
>
18.
<
filename
>index.html</
filename
>
19.
<
folder
>tmpl</
folder
>
20.
</
files
>
21.
<
config
>
22.
<
fields
name
=
"params"
>
23.
<
fieldset
name
=
"basic"
>
24.
<
field
name
=
"moduleclass_sfx"
type
=
"text"
default
=
""
label
=
"Module Class Suffix"
description
=
"Suffix for individual css styling"
/>
25.
<
field
name
=
"limit"
type
=
"text"
default
=
"10"
label
=
"Limit Displayed Users"
description
=
"Limit Displayed Users"
/>
26.
<
field
name
=
"user_id"
type
=
"radio"
default
=
"1"
label
=
"Display user ID"
description
=
"Display user ID"
>
27.
<
option
value
=
"0"
>JNO</
option
>
28.
<
option
value
=
"1"
>JYES</
option
>
29.
</
field
>
30.
<
field
name
=
"user_name"
type
=
"radio"
default
=
"1"
label
=
"Display Name"
description
=
"Display Name"
>
31.
<
option
value
=
"0"
>JNO</
option
>
32.
<
option
value
=
"1"
>JYES</
option
>
33.
</
field
>
34.
<
field
name
=
"user_username"
type
=
"radio"
default
=
"1"
label
=
"Display Username"
description
=
"Display Username"
>
35.
<
option
value
=
"0"
>JNO</
option
>
36.
<
option
value
=
"1"
>JYES</
option
>
37.
</
field
>
38.
</
fieldset
>
39.
</
fields
>
40.
</
config
>
41.
</
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
01.
<?php
02.
03.
04.
05.
06.
07.
08.
09.
10.
defined(
'_JEXEC'
)
or
die
;
11.
12.
13.
require_once
dirname(
__FILE__
).
'/helper.php'
;
14.
15.
16.
$list
= modUserDataHelper::getData(
$params
);
17.
18.
19.
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
01.
<?php
02.
03.
04.
05.
06.
07.
08.
09.
10.
defined(
'_JEXEC'
)
or
die
;
11.
12.
class
modUserDataHelper
13.
{
14.
function
getData( &
$params
)
15.
{
16.
17.
18.
$list
=
array
();
19.
$query
=
" SELECT id, name, username "
20.
.
" FROM #__users "
21.
.
" WHERE block=0 "
22.
.
" ORDER BY id DESC "
23.
.
" LIMIT "
.
$params
->get(
'limit'
);
24.
$db
=& JFactory::getDBO();
25.
$db
->setQuery(
$query
);
26.
$rows
=
$db
->loadObjectList();
27.
28.
29.
if
(
$rows
!=null)
30.
{
31.
$i
=0;
32.
foreach
(
$rows
as
$row
)
33.
{
34.
$list
[
"users"
][
$i
][
"id"
]=
$row
->id;
35.
$list
[
"users"
][
$i
][
"name"
]=
$row
->name;
36.
$list
[
"users"
][
$i
][
"username"
]=
$row
->username;
37.
$i
++;
38.
}
39.
return
$list
;
40.
}
41.
42.
}
43.
}
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
01.
<?php
02.
03.
04.
05.
06.
07.
08.
09.
10.
defined(
'_JEXEC'
)
or
die
; ?>
11.
12.
<div
class
=
"moduletable<?php echo $params->get( 'moduleclass_sfx' ) ?>"
>
13.
14.
<ul>
15.
<?php
for
(
$i
=0;
$i
< sizeof(
$list
[
"users"
]);
$i
++) { ?>
16.
17.
<li>
18.
<?php
if
(
$params
->get(
'user_id'
)) { ?>
19.
<span><?php
echo
$list
[
"users"
][
$i
][
"id"
];?></span>
20.
<?php } ?>
21.
<?php
if
(
$params
->get(
'user_name'
)) { ?>
22.
<span><?php
echo
$list
[
"users"
][
$i
][
"name"
];?></span>
23.
<?php } ?>
24.
<?php
if
(
$params
->get(
'user_username'
)) { ?>
25.
<span><?php
echo
$list
[
"users"
][
$i
][
"username"
];?></span>
26.
<?php } ?>
27.
</li>
28.
29.
<?php } ?>
30.
</ul>
31.
32.
</div>
5. Open the file
index.html as well as the file
tmpl/index.html and insert this code
1.
<
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!
No comments:
Post a Comment