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:

class MySelectObject{
var $id;
var $name;

Then to use it do the following:

$db = &JFactory::getDBO();
$query = "SELECT `id`,`name` FROM

$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 =

/*Make MySelectObject the default selected*/