Creating a simple module on Joomla 1.5. # (Using a database connection)

    The network has many examples of creating a Hello World module, but this is often not the main goal for a person who first wants to make his module. Usually its task is to output some data from the database that was not provided by the usual standard Joomla modules.
    To do this, create a directory and put there any index.html file (located in any folder of any Joomla module) to avoid direct access to the directory. If you don’t want to worry about installing it, but you have Joomla on the localhost, you can create your module’s folder immediately in the modules folder, after creating all the files, you can go directly to the admin panel, control the modules, click “create” and it’s will be displayed in the list and ready for inclusion ...

    For example, the module will be called mod_my. His task will be to display a list of users from the database and limit it to the number specified by himself from the admin panel to manage this module.

    The minimum set of files for the module to work
    properly




    : /modules/mod_my/index.html /modules/mod_my/mod_my.php /modules/mod_my/mod_my.xml /modules/mod_my/helper.php /modules/mod_my/tmpl/index.html /modules/mod_my/tmpl/default.php

    This module uses the MVC architecture (Model, View, Controller - Model, View, Controller).
    First of all, we separate the logic of the module (controller) into the helper.php file so that all data work is performed only there. Presentation (View) / template, (X) HTML we will put out in tmpl / default.php. It is a good programming style to separate logic from presentation.

    Now let's start creating files. Let's start with the mod_my.php file
    get ('usercount');
    // get the items to display from the helper
    $ items = ModMyHelper :: getItems ($ userCount);
    // include the template for display
    require (JModuleHelper :: getLayoutPath ('mod_my'));
    ?>
    


    The main stages of this file:
    • We include the helper.php file, which will be our workhorse when working with module logic and data.
    • After we receive the data, we simply load a template that uses our data and displays it.


    The following file, mod_my.xml , will contain a description of the module and some of its settings:
    My moduleNick2009-03-30CopyrightGPL 2.0info@info.comwww..com1.0.0Every mod_my.phpindex.htmlhelper.phptmpl / default.phptmpl / index.html


    Helper.php itself will contain the request code directly:
    setQuery ($ query);
            $ items = ($ items = $ db-> loadObjectList ())? $ items: array ();
            return $ items;
        } // end getItems
    } // end SimplestForumLatestPostsHelper
    ?>


    It simply obtains a list of users from the database and randomly fills the array with a subset of users, the dimension of which is specified through the module parameter and passed to the getItems method via the $ userCount parameter.

    And finally, so that all this is displayed in the simplest list, we create the tmpl / default.php file
    • name; ?>


    As a result, we get a fully working module, of the simplest content, but having a connection to the database and an example of its implementation! Then you can of course archive it in ZIP and it will be ready to install in any joomla 1.5

    Also popular now: