Monday, September 6, 2010

Joomla 1.5 Additional select tag values

This continues on my blog post on how to add a select tag through the Joomla! framework. The code that follows will show you how to add to the ObjectList for a Select tag. This is especially useful where you have to use a select tag in a search form.

I normally use a value of 0 to indicate a search of "All Values".

The first thing you do is create a class at the top of your page as follows:

<?php
class MySelectObject{
var $id;
var $name;
}
?>



Then to use it do the following:

<?php
$db = &JFactory::getDBO();
$query = "SELECT `id`,`name` FROM
#__town";

$db->setQuery($query);
$townlist = $db->loadObjectList();

/*Create an instance of MySelectObject*/
$mso = new MySelectObject();

/*Set values*/
$mso->id = 0;
$mso->name = 'Any Town';

/*Create an array for insertion*/
$tmpArr = array(0 => $mso);

/*Merge with array result from query. Add to the front of list*/
$selectList =
array_merge($tmpArr,$townlist);

/*Make MySelectObject the default selected*/
echo
JHTML::_('Select.genericlist',$selectList,'town',"","id","name",0);


?>

Wednesday, October 14, 2009

Joomla 1.5 - Creating RAW output for the front end

If you want your component to create RAW output on the front end, like xml data to be returned for an AJAX POST/GET, then this is what you have to do.

1. In your component's views/view/ folder, add a view.raw.php file. E.g. if your component has a view called smile your will add view.raw.php to your views/smile/ folder . i.e. com_mycomponent/views/smile/view.raw.php

2. Whenever you call your component via a url simply append format=raw to the url. E.g. index.php?option=com_mycomponent&format=raw

Notes: For the backend administrator side, you can just specify format=raw without having to add the view.raw.php file to your views folder.

Monday, August 10, 2009

Joomla 1.5 Raising Errors, Notices and Warnings through code

Sometimes it is neccessary to inform an user that incorrect information was entered, like with a login or form validation or sometimes the users needs to be notified what fields needs to be filled in when the form gets loaded.


Making use of Joomla!'s JError class it is possible to display this kind of messages theough the Joomla! framework. To use simply call the JError::raiseError, JError::raiseNotice or JError::raiseWarning methods.


<?php
$msg = 'Error Message';
JError::raiseError(0,$msg);

$msg = 'Notice Message';
JError::raiseNotice(0,$msg);

$msg = 'Warning Message';
JError::raiseWarning(0,$msg);
?>


[update - 11 Feb 2010]
For more go to the following url:
http://docs.joomla.org/Display_error_messages_and_notices

Monday, May 25, 2009

Joomla! 1.5 Fixing the Tabs for IE

The basic tabs style does not really display properly in Internet Explorer. See my tutorial on how to create tabs through the Joomla! framework.

In Internet Explorer the dd and dt tags does not stack properly while it display properly in Firefox, Chrome, Opera and Safari.

To fix this issue, an IE only style is created and included in the site template's  index.php file, or the existing ieonly.css file in the template/css directory can be used, if there is such a file.
E.g.
<!--[if IE]>
<link href="<?=$this->baseurl?>/templates/<?=$this->template?>/css/ieonly.css" rel="stylesheet" type="text/css" />
<![endif]-->

In this css file the following code is added.
div.current { overflow:auto; }
div.current dd  { float:left; }

And the display issue is fixed!

Credits: I would like to credit alexwai for figuring out a solution for this. See thie forum thread.

Thursday, May 21, 2009

Joomla! 1.5 Adding to the Breadcrumb/PathWay through the Joomla Framework

Joomla's JPathway class allows you to add a new item to the pathway to be displayed in the Breadcrumb.

The first thing to do is to access the global JApplication object, then get a reference to the JPathway object.

<?php 
global $mainframe;
$breadcrumbs = &$mainframe->getPathWay();
?>
Adding a breadcrumb is now as simple as calling the JPathway::addItem() method. The addItem method takes a name as the first parameter and a link as the second parameter.
<?php 
$breadcrumbs->addItem( 'My Crumb!', JRoute::_('index.php?option=com_mycrumb') );
?>

If your Item is the last item on the breadcrumb the link will be ignored.