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.

Monday, July 14, 2008

keepr

Today I would like to introduce keepr, a php cms/framework I am working on in my free time.

It all started in May/June 2007. I was without a job and needed something to help me deliver projects faster without forcing me to change the way I like to code. It impliments the way I see Model-View-Controller for the web environment using PHP XTemplate

I launched a GNU GPL version 3 at the following url http://code.google.com/p/keepr/ . There is no downloads yet by the full source is available at Source/Browse under trunk.

Thursday, March 13, 2008

SQLite and SOCI

Now that I know that I can do SQL queries agains the SQLite database, my first objective is to create a C++ dataobject that will allow my yet to be created framework to interact with the database. But first I need to be able to actually access SQLite through code.

My search led me to SOCI which is a C++ Database Access Library. I quote from their website:

The idea is to provide C++ programmers a way to access SQL databases in the most natural and intuitive way.
From what I can see SOCI looks to be the best thing for what I want to achieve.

http://soci.sourceforge.net

Thursday, February 7, 2008

SQLite - Updating Tables

What if I need to make changes to the data in my table? As an example, I forgot to insert Armourette's birthday and Johan is not active yet. This mean I will have to run an update query on the table.

SQLite query:

SQLite version 3.5.4
Enter ".help" for instructions
sqlite> .schema person
CREATE TABLE person
(
'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
'FIRSTNAMES' TEXT,
'SURNAME' TEXT,
'NAME' TEXT,
'BIRTHDATE' DEFAULT CURRENT_DATE,
'TITLE' TEXT,
'ACTIVE' TEXT(1) DEFAULT 'N'
);
sqlite> select * from person;
1|Johannes|Strydom|Johan|1977-02-22|Mr|N
2|Armourette|Strydom|Armourette|2008-01-17|Mrs|Y
sqlite> UPDATE person SET ACTIVE='Y' WHERE ID=1;
sqlite> UPDATE person SET BIRTHDATE='1983-07-05' WHERE ID=2;
sqlite> select * from person;
1|Johannes|Strydom|Johan|1977-02-22|Mr|Y
2|Armourette|Strydom|Armourette|1983-07-05|Mrs|Y
sqlite>
Using the primary key ID I am able to quickly update the correct data.

Wednesday, January 16, 2008

SQLite - Creating Tables

What is a database without tables?

My next chalenge in SQLite is to create tables. Using the SQLite create table documentation I am able to create tables without any hassle.

Say for example I want to store data about a person in the database and to do that I want to create a table with fields for firstnames, surname, preferred name, birthdate,title and some sort of primary reference that should be indexed and that should auto increment whenever a new row is added to the table. I also want to indicate in this table whether the person is active.

So I start up SQLite and attached my database;
SQLite version 3.5.4
Enter ".help" for instructions
sqlite> ATTACH DATABASE 'myDB.fl' AS myDB;
sqlite> .databases
Ok so I now have everything in place to start creating the table.
sqlite> CREATE TABLE myDB.person
...> (
...> 'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
...> 'FIRSTNAMES' TEXT,
...> 'SURNAME' TEXT,
...> 'NAME' TEXT,
...> 'BIRTHDATE' DEFAULT CURRENT_DATE,
...> 'TITLE' TEXT,
...> 'ACTIVE' TEXT(1) DEFAULT 'N'
...> );
Fine so far. Now to insert data!
sqlite> INSERT INTO myDB.person('FIRSTNAMES','SURNAME','NAME','BIRTHDATE','TITLE
') VALUES ('Johannes','Strydom','Johan','1977-02-22','Mr');
No problemo. To see whether everything works I do a SELECT
sqlite> SELECT * FROM myDB.person;
1|Johannes|Strydom|Johan|1977-02-22|Mr|N
And another insert to test the default date.
sqlite> INSERT INTO myDB.person('FIRSTNAMES','SURNAME','NAME','TITLE'
,'ACTIVE') VALUES ('Armourette','Strydom','Armourette','Mrs','Y');
sqlite> SELECT * FROM myDB.person;
1|Johannes|Strydom|Johan|1977-02-22|Mr|N
2|Armourette|Strydom|Armourette|2008-01-17|Mrs|Y
sqlite>
Wasn't that easy? Now If I can only get SQLite to show me the table schema. For some reason it does not want to do that.
sqlite> .schema myDB.person
sqlite>
So what worked:
  • Create Table (passed)
  • Insert (passed)
  • Auto Increment (passed)
  • Default values (passed)
  • .schema (failed)
There must be a configuration or command somewhere for schema which I must have missed.

I solved the problem by exiting SQLite and opening my database as a parameter to the SQLite command line executable.

C:\sqlite-3_5_4\sqlite3.exe myDB.fl
SQLite version 3.5.4
Enter ".help" for instructions
sqlite> .schema person
CREATE TABLE person
(
'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
'FIRSTNAMES' TEXT,
'SURNAME' TEXT,
'NAME' TEXT,
'BIRTHDATE' DEFAULT CURRENT_DATE,
'TITLE' TEXT,
'ACTIVE' TEXT(1) DEFAULT 'N'
);
sqlite>
So the master database is now my SQLite database! Isn't that cool? :)

With my database as the master I am also able to get the schema by running the following SELECT statement
sqlite> SELECT * FROM sqlite_master WHERE type IN ('table') AND tbl_name NOT LIKE 'INFORMATION_SCHEMA_%' ;
table|person|person|2|CREATE TABLE person
(
'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
'FIRSTNAMES' TEXT,
'SURNAME' TEXT,
'NAME' TEXT,
'BIRTHDATE' DEFAULT CURRENT_DATE,
'TITLE' TEXT,
'ACTIVE' TEXT(1) DEFAULT 'N'
)
table|sqlite_sequence|sqlite_sequence|3|CREATE TABLE sqlite_sequence(name,seq)
sqlite>
Conclusion?
  • .schema (!passed!)

Wednesday, January 9, 2008

SQLite - my journey starts

I do C++ programming in my free time and one of the challenges I am facing at the moment is databases. I have a project that require me to use a database for storing data.

Most of my development is taking place on my gaming PC with Windows XP Professional (Although I also work a bit on Fedora Linux)

My search led met to Sqlite

Sqlite is :
and has many more features...

I downloaded the Windows command-line version and started it up.
SQLite version 3.5.4
Enter ".help" for instructions
sqlite>
Figuring out how to create a database was very easy using the Sqlite language syntax as on the Sqlite website
sqlite> ATTACH DATABASE 'myDB.fl' AS myDB;
That command created a database for me and I was able to see where it is located by entering .databases at the Sqlite command prompt.

sqlite> .databases
seq name file
--- ---- ---------------------
0 main
2 myDB C:\sqlite-3_5_4\myDB.fl

So far I like Sqlite allot. It's easy to use and it just simply works.