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
<?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>';
?>
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.
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 ModulesTo 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) Note: You have to be a registered member to leave a comment. Free registration here. |
||
| < Prev | Next > |
|---|
Joomla 1.0 Articles
- How to create a module with DirectPHP plugin?
- How to include modules in Content item?
- How to include modules in Section/Content Description
- How to include PHP commands in Section/Content Description
- How to make plugins process Section/Category descriptions
- Include Code Listing plugin - More Tips and Techniques
- How to have DirectPHP plugin process RSS feed?
- How to have the plugins processed when genrating PDF documents of content items?
- How to have DirectPHP commands processed in Joomla search?
Joomla 1.5 Articles
- How to create a module with DirectPHP plugin?
- How to include modules in Content item?
- How to include modules in Section/Content Description
- How to include PHP commands in Section/Content Description
- How to make plugins process Section/Category descriptions
- Include Code Listing plugin - More Tips and Techniques
- How to have DirectPHP plugin process RSS feed?
- How to have the plugins processed when genrating PDF documents of content items?
- How to have DirectPHP commands processed in Joomla search?
Joomla Plugins
Joomla Components
Joomla Gadgets
- Google Image Search
- Google News Search
- Wikipedia Search
- Google Book Search
- Google Video Search
- Google Currency Converter - AJAX version
- Yahoo Currency Converter - AJAX version
- Yahoo Stock Quote (AJAX module version)
- Yahoo Stock Quote (AJAX component/ module version)
- googleSearch_cse component
- googleSearch_cse module
- googleMaps plugin















4.5 (from 12 users)
April 08, 2008 6:46pm
can not find directphp.
causes the whole tutorial to fall apart.
April 08, 2008 6:56pm
Please click the "DirectPHP plugin" link in the very first paragraph.
Regards,
/kksou
August 23, 2008 10:30am
hi,
i want to connect Mysql with the php code written Article page. how it can possile plz..help me
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
August 29, 2008 12:30am
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.
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
August 29, 2008 2:29am
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
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
September 03, 2008 5:14am
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.
January 21, 2009 6:58am
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.
January 29, 2009 7:05am
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 '';
}
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 "