Find us on facebook

Jun 16, 2015

Creating a table on module install

Step 1 : mod_students.php

<?php
/**
 * Students Name Module Entry Point
 *
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @link http://docs.joomla.org/J2.5:Creating_a_simple_module/Developing_a_Basic_Module
 * @license        GNU/GPL, see LICENSE.php
 * mod_students is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

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

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

$studentName = modStudentsHelper::getStudentNames($params);
require(JModuleHelper::getLayoutPath('mod_students'));
?>

Step 2 : helper.php

<?php
/**
 * Helper class for Students module
 *
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @link docs.joomla.org/J2.5:Creating_a_simple_module/Developing_a_Basic_Module
 * @license        GNU/GPL, see LICENSE.php
 * mod_students is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */
class ModStudentsHelper
{
    /**
     * Retrieves the hello message
     *
     * @param array $params An object containing the module parameters
     * @access public
     */  
    public static function getStudentNames( $params )
    {
        // Obtain a database connection
$db = JFactory::getDbo();
// Retrieve the shout
$query = $db->getQuery(true)
->select($db->quoteName('fname'))
->from($db->quoteName('#__students'))
->where('lang = ' . $db->Quote('en-GB'));
// Prepare the query
$db->setQuery($query);
// Load the row.
$result = $db->loadResult();
// Return the Hello
return $result;
    }
}
?>

Step 3 : mod_students.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" client="site" method="upgrade">
    <name>Student Names</name>
    <author>Ishara</author>
    <version>1.0.0</version>
    <description>Display Student Names module.</description>
    <files>
        <filename>mod_students.xml</filename>
        <filename module="mod_students">mod_students.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
<filename>sql/install.mysql.utf8.sql</filename>
<filename>sql/uninstall.mysql.utf8.sql</filename>
    </files>
<install>
<sql>
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
</sql>
</install>
<uninstall>
<sql>
<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
</sql>
</uninstall>

<config>
    </config>
</extension>

Step 4: sql/install.mysql.utf8.sql

CREATE TABLE IF NOT EXISTS `#__students` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`fname` varchar(25) NOT NULL,
`lname` varchar(25) NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

INSERT INTO `#__students` (`fname`, `lname`) VALUES ('Derick', 'Cristo');
INSERT INTO `#__students` (`fname`, `lname`) VALUES ('Ruchi', 'Doe');
INSERT INTO `#__students` (`fname`, `lname`) VALUES ('Brunila', 'Cristine');

Step 5: sql/uninstall.mysql.utf8.sql

DROP TABLE IF EXISTS `#__students`

Step 6 : tmpl/default.php

<?php
// No direct access
defined('_JEXEC') or die; ?>
<?php echo $studentName; ?>

No comments:

Post a Comment