phpeveryday.com

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


Joomla - Component: Showing Number of Comment at Last Post (Part 8)

Tag: joomla, cms   Category: PHP Framework
post: 22 Nov 2007 read: 2,281


In this tutorial, we try to make different scenario. Show last post and the number of comments in that post. The code must be flexible. We can be back to display list or last post without change any code anymore. If can do it, it is realy named flexible!

First, we modify "mod_hello.php" within joomla/modules/mod_hello. Update like following code:


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

$last = $params->get('last' , 1);

if($last)
{
   	$rows = modHelloHelper::lastHello($params);
}
else
{
   	$rows = modHelloHelper::getHello($params);
}
require(JModuleHelper::getLayoutPath('mod_hello','lastpost'));
?>

From the code, we need method lastHello within helper. This method for retrieve last post from database. Open helper.php, and add following code:


	function lastHello(&$params)
	{
  		$db =& JFactory::getDBO();
  		$query = "SELECT id, message FROM 
#__hello ORDER BY id DESC LIMIT 1 ";
  		$db->setQuery( $query );
  		$rows = $db->loadObjectList();
		
  		if ($db->getErrorNum()) {
    			JError::raiseWarning( 500, $db->stderr(true) );
  		}		
		
  		return $rows;
	}

For count number of comments, we need method named "countComment" at helper. Add following code to helper.php.


function countComment($item)
{
  		$db =& JFactory::getDBO();
  		$query = "SELECT count(1) as total
            FROM #__hello_comment WHERE hello_id=".$item;
  		$db->setQuery( $query );
  		$rows = $db->loadObjectList();
		 		
  		if ($db->getErrorNum()) {
    			JError::raiseWarning( 500, $db->stderr(true) );
  		}				
		
  		return $rows[0]->total;		
			
}

Just remembering, we call template name "lastpost" at mod_hello: require(JModuleHelper::getLayoutPath('mod_hello','lastpost')).

Now, we create the template. Create a file named "lastpost.php" at joomla/modules/mod_hello/tmpl.


<?php 
defined('_JEXEC') or die('Restricted access'); 
foreach($rows as $row)
{
  	modHelloHelper::renderHelloComment($row, $params);
 }
?>

This call, renderHelloComment() at helper. Add this function to helper class (at "helper.php"):


	function renderHelloComment(&$hello, &$params)
	{
  		$total = modHelloHelper::countComment($hello->id); 
  		$link  = JRoute::_('index.php?option=com_hello&id='.$hello->id.'&task=view');
  		require(JModuleHelper::getLayoutPath('mod_hello' , '_hello'));	
			
	}

Last, we need modify "_hello.php" for accommodate showing total of comment.


<?php 
defined('_JEXEC') or die('Restricted access'); 
 
echo 'ID: <A href="'. $link .'">'.$hello->id.'</A> </br>';
echo $hello->message;

echo "</br>";
 if((int)$total > 0){
  	echo "<i>".$total." comments</i>";
  	echo "</br>";	
 }
 ?>


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