Tuesday, December 16, 2008

Joomla! 1.5 How to create a Select tag through the Joomla Framework

Today I needed to display a list of towns from a database table. Nowhere on the Joomla! documentation could I find an example on how to achieve that, so I had to go figure it out once again.

I have a database table that contains all my towns. First I had to create a query to retrieve the towns and put them in an object list.

The code for that follows.
<?php
$db =& JFactory::getDBO();

$query = 'SELECT `id`,`name` FROM #__town';
$db->setQuery( $query );
$townlist = $db->loadObjectList();
?>

Ok now that I have a list of towns I simply have to use JHTMLSelect.genericlist() to output the HTML code for my select.

The code to do that follows:

<?php
echo JHTML::_('Select.genericlist',$townlist,"town","","id","name",1);
?>


In the code I am simply creating a select from the town list with the options values set as id and the texts set as name (as per the query) . The 1 at the end indicates the selected option. 
If I for example get the selected option from a post/get request, the code will look as follows.

<?php
$selectedId = JRequest::getVar( 'id',0);
echo JHTML::_('Select.genericlist',$townlist,"town","","id","name",$selectedId);
?>

Thursday, December 4, 2008

Joomla! 1.5 How to cloak an email through code.

This one I figured out for myself and I am sure you will be able to do the sime... right. :)

Say for example you have a custom field in the database that stores an email and you want to display that email on a page somewhere. You don't really want to allow every spam or virus harvest bot out there to come and grab the email. That is why Joomla! created the cloak function. It just makes sense to protect ppl's emails.

To cloak the email simply enter the following code.


<?php
$email = 'some.email@some.domain';
$emcloaked = JHTML::_('Email.cloak',$email);
echo $emcloaked;
?>


And that is it.

Wednesday, October 22, 2008

Joomla! 1.5 Getting the modal dialog to work

Today I quickly want to show you how to get the Joomla! 1.5 Modal Dialog (SqueezeBox - Expandable Lightbox) to work in any of your components or modules. It's actually quite easy to do since the Joomla! team did such a great job at making mootools part of the solution. JHTML to the rescue! :)

In this example I'm going to make a normal anchor open up a Modal dialog. Are you ready?

Within your php code tags enter the following:

<?php
JHTML::_('behavior.modal', 'a.modal-button');
?>

Add an anchor and the following to the anchor:


<a href="http://www.google.com/" class="modal-button" rel="{handler: 'iframe', size: {x: 725, y: 520}}">MyMooModal</a>


And that is all there is to it. You can specify ANY link and it will open in the Modal Dialog

Friday, September 5, 2008

goAgri.com

Today I added a facebook group for a farming website I founded. Head on over to http://www.new.facebook.com/group.php?gid=24487333117  should you wish to join my group.

I've got quite a few ideas which I have gathered over the years. Join me as I start to implement these ideas on the world wide web. Be part of something not seen before in the agriculture sector, world wide!

For the goAgri.com website I have partnered with NeoEdge, the leading ad network for casual games. See http://www.neoedge.com

Friday, July 25, 2008

How to get the active component in Joomla! 1.5.x

For some time now I have been struggling to figure out how to get the active component in Joomla! 1.5.x without going through the GET/POST Requests. This problem was specifically for the frontpage.

What happens on the frontpage is that it is not possible to echo JRequest::getVar('option') and have the code tell you that you are on the frontpage. Instead the text that will be displayed will simply display com_content, which is the component for articles.

After searching throught the Joomla! Document Wiki I stumbled upon the solution; totally by chance I might add.

How to determine if the user is viewing the front page

The above document shows this piece of code:
$menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault())
{

echo 'This is the front page';
}
But what does the code actually do?

JSite::getMenu() returns a JMenu class called $menu that is inherited from the JObject class.

$menu->getActive() and $menu->getDefault() returns objects of the active and default menu items (by id).

If the active object is the same as the default object then naturally it must be the front page.

For other components the simplest option will still be to use the JRequest object to get the component name.

e.g. JRequest::getVar('option')
Note: I have not tested the above code with Search Engine Friendly URLs yet since they may, or may not, make a difference.