Showing information in Joomla Module

Modules are the quickest way to add PHP code to your Joomla site. In this example we'll pull some data from our component and display it in a module.

Module Editing in backend of joomla

So go to the root of your Joomla installation, then go to modules, then go to mod_noidacity and you'll see mod_noidacity.php( If you don't have this module in place in your joomla installation please read my previous joomla tutorial series). So what you want to do is just replace that file code with the following code

  1. <?php
  2. defined( '_JEXEC' ) or die;
  4. $db = JFactory::getDBO();
  6. $query = $db->getQuery(true);
  8. $query->select('place_name')
  9. ->from('#__noidacity_places');
  11. $db->setQuery($query);
  13. $rows = $db->loadObjectList();
  15. ?>
  16. <ul>
  17. <?php foreach($rows as $row): ?>
  18. <li><?php echo $row->place_name; ?></li>
  19. <?php endforeach; ?>
  20. </ul>

and then if we go back to the front end and hit Refresh, you'll notice that now we've the titles of several places that you can go when you're in Noida City.

Module pulling information from database

  1. <?php
  2. defined( '_JEXEC' ) or die;

Now let's take a look at the file that we just replaced. So at the top of the file we still make sure that we are within Joomla We don't want people to go directly to mod_noidacity.php.

  1. $db = JFactory::getDBO();

Next, we are calling the JFactory::getDBO function. What this function does is it gets an object that represents the database that Joomla is currently connected to.

  1. $query = $db->getQuery(true);

After we have our database object we can call to getQuery method on it. In this case we are passing the parameter true into the function. What this does is it returns a fresh database query object. If we omit true from this call what it will do is it will return the last database query that was executed in the database. We've really don't want that. We want our fresh ones that we can build our own query to get some records out of the database.

  1. $query->select('place_name')
  2. ->from('#__noidacity_places');

Then next we are building our SQL statement. Rather than just typing out an SQL statement as at long string what we are doing is we are calling different functions on the query object. So that way we can build our query very incrementally. In this case it's very straightforward. We are just selecting the place_name from the places table, but in more complicated cases this functionality could be very useful for building your query incrementally rather than just outright.

  1. $db->setQuery($query);

We are setting the query object into the database object and what that does is it just takes the query and places it into the database object and gets it ready to be executed.

  1. $rows = $db->loadObjectList();

Then finally we are calling the LoadObjectList function on the database object. That takes the query, runs the query, and then loads all of the data from that query into an array and that array contains objects that each represent a row in the database.

  1. <ul>
  2. <?php foreach($rows as $row): ?>
  3. <li><?php echo $row->place_name; ?></li>
  4. <?php endforeach; ?>
  5. </ul>

Once we have our rows, we can cycle over them using the foreach statement. This foreach statement is simply building an unordered list and we are just outputting the places name for each place that's in the database.

So this is a very simple module and it's a way that you can just get some information out of the database and display it on the front end. When you want start displaying information in Joomla without a lot of work, modules are good choice. They are simple and can pull any data stored in Joomla