Showing our data in our module

Now that we have inserted some data into the database, it's the best time to modify our module and enhance it to show this data in our module position.

At the moment, mod_tinynews is only showing the "This is working" message, which obviously is useful for nothing. Let's enhance it. The first file that we are going to modify is modules/mod_tinynews/helper.php, as follows:

<?php
            defined('_JEXEC') or die('Direct Access to this location is not allowed.'),
            class ModTinyNewsHelper{
            function getallnews(){ $database =& JFactory::getDBO(); $query = "SELECT * FROM ".$database->nameQuote('#__ tinynews').";"; $database->setQuery($query); $result = $database->loadObjectList(); return $result; }
            }
        

Looks familiar? It's the same method we used in our component frontend model. It will be useful for here too. We will call this method in our modules/mod_tinynews/mod_tinynews.php file:

<?php
            defined('_JEXEC') or die('Direct Access to this location is not allowed.'),
            JHTML::stylesheet('styles.css','modules/mod_tinynews/css/'),
            $document =& JFactory::getDocument();
            $document->addscript(JURI::root(true).'modules'.DS.'mod_tinynews'.DS.'js'.DS.'tinynews.js'),
            require_once(dirname(__FILE__).DS.'helper.php'),
            $news = ModTinynewsHelper::getallnews();
            require(JModuleHelper::getLayoutPath('mod_tinynews','default_tmpl'));
        

The only difference here is that we are calling the getallnews method from our helper. This way we will have the result in our $news variable that will be available in our modules/mod_tinynews/default_tmpl.php file:

<?php
            defined('_JEXEC') or die('Direct Access to this location is not allowed.'),
            ?>
            <div id="tinynews">
            <?php
            foreach($news as $new){
            echo "<p><b>".$new->title."</b></p><br/><br/>";
            echo "<p>".$new->text."</p><br/><br/>";
            }
            ?>
            </div>
        

This code is also quite similar to the one we used in our component, but instead of using $this->news for getting the value from a class property, we now have our variable directly available as $news. With all this code in place, our module will look similar to the following screenshot:

Showing our data in our module

Better than before, isn't it? But we will make it even better soon.

Tip

You can find the modified module in the second_mod_tinynews.tar.gz file in the code bundle, so you can take a look at it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.71