v June 2009 ~ WebsiteSupport

June 23, 2009

June 17, 2009

PHP code in Joomla content

Joomla is great for building all sorts of websites, but Websitesupport find that the real power comes from being able to mix and match PHP/MySQL code with the rest of the Joomla code.

There are numerous ways to do this:

  1. If you want to do it the right way then you can always write a custom module or component. A module is not that difficult, but a component is a bit of a different story. I am not going to go into how to write a component here, since the focus of this entry is rather on doing a quick and dirty!

    I've seen this Extension on the Joomla site:
    It is a Developer Toolkit for Joomla that, among other things, "creates boiler plate components" - will check it out one of these days and write up some feedback.

  2. You can use the Static Files component. I used this quite extensively on some of my sites, and it works quite well.

    You can download it from http://www.joomlaxt.com/ and the author has now written up a quick guide that should help you get going with it. You can find it on joomlaxt.com.

    Update : 26 Jan 2008: I do not use this at all any more, but rather KL_PHP, as described below.

  3. But here is the clincher! The BEST way to do 'quick-and-dirty' PHP programming inside Joomla is to use this mambot: KL_PHP. This mambot is not publically available any more but you can download here KL_PHP Mambot.
    Also, you cannot use this bot directly in modules, for that you require the module: mod_kl_evalphp.zip
    Now, I do realise that there are other options available by now, as also commented on below, however, this has worked very well for me up to now so I am not really interested at this stage to try something else!
  4. Here are my tips to get the most out of it:
    • Firstly, don't even try and use it if you have your WYSIWYG editor on. The WYSIWYG editor will destroy some of your code and you can never be sure that the code that you entered so painstakingly will still be there after you've saved it. So be sure to turn your WYSIWYG OFF when you use it in any page.
    • Then make sure that the Mambot is actually published!

You can then create a 'Static Page'. Make the name and title both 'some page' and link it to a menu item called 'Some Page'. I normally use the Othermenu or Usermenu to publish these types of pages for testing purposes. Your 'Some Page' page with your kl_php mambot will then look like this:

{ kl_php}

$inv_num = $_POST['invoice_id'];

and so on ...ending with

{ /kl_php }

Here are a couple of power tips:

  • You can use multiple { kl_php } ... { /kl_php } combinations through your page, but if you want to refer to $inv_num, as in the example above, later on in your page inside a different { kl_php } ... {/kl_php} block, you have to declare it as global first in the other blocks else it it won't have the value you originally assigned to it, it will be seen as a new variable.
  • Apart from that, you can also include other PHP files inside your { kl_php }... { /kl_php} block. This is very powerful since if you don't want to be bothered with writing your PHP code inside your Joomla content, you can write it as external PHP files and just include the PHP files. For maintenance purposes this might be better since if anything happens to your Joomla installation your PHP code will be gone too!
  • PLEASE NOTE: FOR SECURITY PURPOSES ALWAYS INCLUDE A CHECK AT THE TOP OF YOUR FILE TO MAKE SURE THAT THE INCLUDED FILE IS CALLED FROM THE JOOMLA ENVIRONMENT. For example, make sure that you include the following line at the top of your included PHP code:

    defined( '_VALID_MOS' ) or die( 'Restricted access' );
  • You can print output directly to the front end by using 'echo' and 'print' - at least in all the cases that I've used it. In some of the older forum entries it is specified that you should use 'Return' to output any values to the front end, however, I have not found that to be the case any more.
  • In my most recent usage of this mambot, I have even found that it is possible to sessions. It is therefore possible to code quite complex applications directly into your Joomla content by passing parameters through forms as well as sessions.

This is just a quick write-up of the basic functionality of this great extension - any more questions, just write a comment or drop me an email and I will see if I can elaborate.

Why use Joomla for web design?


The fact,Joomla is one of the easiest ways to quickly design and create your own website. You can easily create a wonderful looking website with loads of functionality. Wordpress is another way to create and manage your own website - but more on Wordpress somewhere else.

Joomla started life as an Open source Content Management System called Mambo. In August 2005 a split took place between the core developers of Mambo and the owners of the Mambo name. The developers decided to continue the development of this software under the name Joomla and most of the community that previously supported Mambo have now thrown in their weight behind the new product.

You can install Joomla from the Fantastico Panel inside the cpanel that comes with all the hosting companies. You need to have PHP and a MySQL database available, but these are also included with ALL the hosting companies, so it really is easy to get it up and running on your site.

If you would like to see Joomla in action, you can check out this demo site:

http://demo.joomla.org

In this demo you will see the front end of Joomla ( in other words, an example of a website that you can build with Joomla).

You can also log into the administrator section, where you would go to add new content (in other words, text, pictures, file downloads etc) to your site. Select the administrator option and use the username and password provided on the site to play around with the back end of the site.

But what if you don't like the look and feel of this site?

No problem! Joomla has literally hundreds of free templates available - one of my favourite sites is this one - Mambo Hut, your source for free Mambo/Joomla templates!

Joomla comes with a lot of built-in functionality but there are also thousands of free components and modules that you can add on to expand the functionality.

The other nice thing about Joomla is that there are lots of forums that offer free support and guidance. The problem normally with freeware is that support is provided not through any licensing fees that you pay, but through a community that all contribute and therefore gain themselves in the long term. Joomla has a very active community, and it is therefore very easy to get support for any possible problem.

The Joomla forum and all the extensions to Joomla in the form of components and modules can be found on http://www.joomla.org

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes

HTML Hit Counter