phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


Joomla - Module: Using Helper (Part 5)

Tag: joomla, cms   Category:
post: 21 Nov 2007 read: 1,738


Sometime, we want to display random items. May be, we want to display new item on the top or other ideas. It will be more efficient if we made a helper class. It will centralize the data function. In this tutorial, we will try to build helper class to replace code in last tutorial.

Create a file named "helper.php" within joomla/modules/mod_hello. Enter following code:


<?php
defined('_JEXEC') or die('Restricted access');

class modHelloHelper 
{
  	function getHello($params)
  	{
    		$items = $params->get('items', 1);
    		$db		=& JFactory::getDBO();
    		$result	= null;

    		$query = 'SELECT id, message'
        			.' FROM #__hello'
        			.' WHERE published = 1'
        			;
    		$db->setQuery($query, 0, $items);
    		$rows = $db->loadObjectList();

    		if ($db->getErrorNum()) {
      			JError::raiseWarning( 500, $db->stderr(true) );
    		}
	
    		return $rows;
  	}

  	function renderHello(&$hello, &$params)
  	{
    		$link = JRoute::_('index.php?option=com_hello&id='.$hello->id.'&task=view');
    		return $link;		
  	}

 }

?>

Now, it is time to change your "mod_hello.php" within joomla/modules/mod_hello.php. Change with following code:


<?php
defined('_JEXEC') or die('Restricted access');
require(dirname(__FILE__).DS.'helper.php');

$rows = modHelloHelper::getHello($params);
foreach($rows as $row)
{
   	echo 'ID: <A href="'. modHelloHelper::renderHello($row, $params)
.'">'.$row->id.'</A> </br>';
  	echo $row->message .'</br>';
}
?>

Can you catch the idea?



Series this article:
Joomla - Module: Register Database (Part 1)
Joomla - Module: Showing Module at Front End (Part 2)
Joomla - Module: Show Items from database (Part 3)
Joomla - Module: Showing Few Text After Link (Part 4)
Joomla - Module: Using Helper (Part 5)
Joomla - Module: Using Template for Layout Flexibility (Part 6)
Joomla - Module: Trying Alternative Layout (Part 7)
Joomla - Component: Showing Number of Comment at Last Post (Part 8)

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting