How to create a module with DirectPHP plugin (for Joomla 1.5.x)
Written by kksou   
Friday, 28 March 2008

You can easily create Joomla modules using the DirectPHP plugin.

In this article, I will show you how to create a module similar to the Most Popular module (mod_mostread) that comes with the standard Joomla installation.

Our version of "Most Popular" module has one additional enhancement: it will list the total number of hits for each listing as shown below:

Note: the steps below are for Joomla 1.5.x. If you're using Joomla 1.0.x, please refer to How to create a module with DirectPHP plugin (for Joomla 1.0.x). The steps are easier for Joomla 1.0.x.

Here are the steps to create a Joomla module

  1. The main "problem" with Joomla 1.5.x is that the HTML module (was Custom module in 1.0.x) is no longer processed by the content plugins. So we cannot use the method as outlined in the article How to create a module with DirectPHP plugin (for Joomla 1.0.x).
  2. To resolve this, we need to install a module called "CustomContent" by Ian MacLennan first. This is a module that allows you to publish a content article in a module position. Please go ahead and install this module first.
  3. Next we create an article to hold the PHP codes for the module. From the Content Menu, select "Article Manager", and click New.
    Let's give it a title "Most Popular v2".
    For the Section and Category, you can select Uncategorized. For Published, make sure it's set to 'Yes'.
  4. Now copy and paste the following code into the content area:
  5. <?php
    global $mainframe;
    $db =& JFactory::getDBO();
    $query = "SELECT * FROM #__content ORDER BY hits DESC limit 0,10";
    $db->setQuery( $query, 0, $count );
    $rows = $db->loadObjectList();
    
    echo '<ul>';
    foreach($rows as $row) {
    echo "<li>$row->title ($row->hits views)</li>";
    }
    echo '</li>';
    ?>
    
  6. If you've copied and pasted the code from this web page, most likely you will see the format set to "preformatted". Highlight all the text and make sure you change it to "Paragraph":
  7. Save the article.
  8. Now from the Extensions menu, select Module Manager. Look for the module named "Custom Content" and click on it.
  9. On the left-hand side which says "Details", change the title to "Most Popular v2".
    Select a Position. If you're using the standard installation, you can leave it as 'Left'.
    Also, make sure 'Yes' is selected for Enabled.
  10. On the right-hand side which says "Parameters", click the "Select" button. You will be prompted with a screen to select an article. Choose the article named "Most Popular v2" which you have created in Step 3 above.
    For "Module Class Suffix", you may want to put "_menu" to give a frame to the module.
  11. Now click Save.
  12. For this example, we're using database query. So you need to allow DirectPHP to execute those commands. Go to "Extensions - Plugin Manager", click on "DirectPHP", and then on the right hand side where it says Enable PHP Command Block, change it from 'Yes' to 'No'.
  13. Note: Please make sure you're only doing this on your development machine. It's too dangerous to do this on a live machine, unless you're the only one managing your website.

  14. You're done creating the module! Try loading your Joomla home page. You should be able to see the new module "Most Popular v2" appearing on the left hand side of the page as shown below:

Of course the above code is extremely simplified. I just want to show you that creating a Joomla module using DirectPHP plugin is simple and straightforward.

If you want, you can refer to mod_mostread.php and put in a more complex database query that checks access control, whether it's published or not, and adds a link to the article, etc.

Creating More Modules

To create more modules, you do not need to install more copies of "CustomContent".

Following from the above example, from the Module Manager, check the module "Most Popular v2" , and then select "Copy" from the menu on the top-right as shown below (the third one from the left):

You will see another module called "Copy of Most Popular v2" appearing above or below "Most Popular v2". Click on that module and then select another article containing a different PHP code. You have now a new module!



User reviews   Average user ratings:    4.5   (from 12 users)
  1. mjh
    April 08, 2008 6:46pm
    Step #11 is a little unclear...

    can not find directphp.
    causes the whole tutorial to fall apart.

  2. kksou
    April 08, 2008 6:56pm

    Please click the "DirectPHP plugin" link in the very first paragraph.

    Regards,
    /kksou

  3. Bal Krishna Shrestha
    August 23, 2008 10:30am
    How to connect with mysql database

    hi,
    i want to connect Mysql with the php code written Article page. how it can possile plz..help me

  4. kksou
    August 25, 2008 3:04am

    The fact that you're using Joomla means that you're already conencted to mysql. In Joomla 1.0, it's in the variable $database. In Joomla 1.5, it's in the variable $db.

    You might want to refer to the mod_mostread module in either Joomla 1.0 or 1.5 and see how the variable $database/$db is being used to access the mysql db.

    When you understood how to use the variable $database/$db, you can then incorporate the code into the content item with DirectPHP.

    Regards,
    /kksou

  5. gadabout
    August 29, 2008 12:30am
    very cool...thanks

    This was exactly what I was looking for...I didn't even know about the 'custom' module and directPHP until this article. Question: where do I get the full mos_mostread.php code? I would like to take this module and make it actually useful and have it at my site...so need the articles to be linked, etc...I've looked online and can't seem to find the full php code for 'most read'...thanks.

  6. kksou
    August 29, 2008 12:47am

    It's right inside your own joomla installation. Goto your joomla root folder and look for the folder "modules". In there, you will see "mod_mostread". All the files are in there.

    However, unless you're already familiar with the Joomla 1.5 strucure, the codes can be difficult to read at first. The three core files are "mod_mostread.php", "helper.php" and "default.php". They make calls to a lot of the internal Joomla core modules. So it might be a bit confusing...

    Regards,
    /kksou

  7. gadabout
    August 29, 2008 2:29am
    that's just it...

    That is just it...I looked already in the modules folder at my c-panel, and the mostread module isn't listed there at all. I think this is because I migrated from 1.15 to 1.5.6, and in my old site I had long ago deleted it and used a different module for that, but that module isn't yet coded for 1.5, so now I need the core mostread module again for my new site...

    If you happen to know where online I can get the mostread php to insert into this tutorial (in place of the basic one above you've given us) I would BE so grateful.

    And if you don't know where that would be...thanks anyhow and keep up the great work on this site as I've already used several pages now to do some new things at my site.

    m

  8. kksou
    August 29, 2008 2:57am

    Just download a fresh copy of Joomla direct from:
    http://www.joomla.org/download.html

    Unzip the file. You will find the folder modules/mod_mostread in there.

    Regards,
    /kksou

  9. Gogo
    September 03, 2008 5:14am
    Problem with Most Popular

    I was following exactly the steps, but when i display the Custom Content /mod_customcontent/ on left sidebar the module shows php code as it is in article:


    but when i display article min front page everything is excellent.
    What can be wrong ?

    Thank you.

  10. zeldap
    January 21, 2009 6:58am
    How to link to article ?

    Hi,
    this snippet of code works great.
    My question is :
    how to link to the article for which we already have the number of hits ?
    Any suggestion would be welcomed.

  11. zeldap
    January 29, 2009 7:05am
    Solved

    If anyone is interested, I solved the question I asked above:

    $db =& JFactory::getDBO();
    $query = "SELECT * FROM #__content ORDER BY hits DESC limit 0,10";
    $db->setQuery( $query, 0, $count );
    $rows = $db->loadObjectList();


    foreach( $rows as &$row ) {
    // to check the properties of the row.. uncomment the next line
    // print_r($row);

    $link = JHTML::_('link', 'index.php?option=com_content&view=article&id=' . $row->id . '〈=en', sprintf("%s (%d)",
    $row->title, $row->hits));
    echo $link;
    echo '';

    }

  12. MMX
    August 14, 2009 1:08pm

    I still get plain code..

    I am not sure but when I paste the piece of code of the example.. automatically the "

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2009. kksou.com. All Rights Reserved