I just developed a Joomla 1.6 component for a private project and came across some things which I believe are good to know. So I decided to note 5 things here:
Include JavaScript / CSS files
Adding a JavaScript or CSS file to the header section of the HTML is quite simple and elegant (well in my opinion):
// get document object $document =& JFactory::getDocument(); // add JavaScript file $document->addScript('components/com_mycomponent/views/script.js'); // add Stylesheet file $document->addStyleSheet('components/com_mycomponent/views/styles.css'); |
Database handling
Using the database in Joomla is also quite simple:
// get database object $db =& JFactory::getDBO(); // set a query, #_ will be replaced with the database prefix set in Joomla! $db->setQuery('SELECT * FROM #__table'); // setting a query will not execute it, you execute it with, returns false in error case: $db->query(); // getNumRows returns the number of rows of the result // loadAssoc loads the first row as an array if ($db->getNumRows() > 0) $row = $db->loadAssoc(); |
You can use setQuery also for INSERTs and UPDATEs too of course. You should also check the documentation.
User handling
// return the user object $user =& JFactory::getUser(); // print the user id echo $user->id; // print the user name echo $user->username; |
You may also want to check this page with some more details.
Multilanguage
Need a component in several languages? No problem. First you need to create a language file (called com_mycomponent.ini) in the correct subdirectory. For example if you want to have a en-GB translation you save the file to JOOMLA_ROOT/language/en-GB. If you want to have a german translation you save it to JOOMLA_ROOT/language/de-DE. The file you need should look like this:
; You can have comments with coma-semicolon ; then you have the labels COM_MYCOMPONENT_HELLO="Hello." COM_MYCOMPONENT_GOODBYE="Goodbye." ; and so on... |
If you need to enter some special chars like umlauts you need to use the html entity in that file!
Then in your code you can use the labels like this:
echo "You say: ".JText::_(COM_MYCOMPONENT_HELLO)."<br />"; echo "I say: ".JText::_(COM_MYCOMPONENT_GOODBYE); |
This will print the two labels. The language which is set in Joomla will be the default language of your component. You have not to think about this though. Joomla will automaticall pick the correct ini file.
GET and POST
Find detailed information in the API documentation and on this example page.
Basically you can acces POST/GET variables like this:
$string = JRequest::getString('myvar'); |
So thats all for today. I hope I can help some newcomer with this information.
Neueste Kommentare