Now, we try to write code for inserting comment. For this task, we add case "comment" at hello.php. Follow this steps:
- Open "hello.php" within joomla/administrator. Insert following bold code:
switch( $task ){
case 'view':
viewHello($option);
break;
case 'comment':
addComment($option);
break;
default:
showPublishedHello($option);
break;
}
- Insert following function:
function addComment($option)
{
global $mainframe;
$row =& JTable::getInstance( 'comment' , 'Table' );
if (!$row->bind(JRequest::get( 'post' )))
{
JError::raiseError(500, $row->getError() );
}
$row->comment_date = date ( 'Y-m-d H:i:s' );
$user =& JFactory::getUser();
if($user->id)
{
$row->user_id = $user->id;
}
if(!$row->store())
{
JError::raiseError(500, $row->getError() );
}
$link = JRoute::_( 'index.php?option='.$option.'&id='.$row->id . '&task=view' );
$mainframe->redirect( $link, 'Comment Added' );
}