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.
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
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.
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.
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.
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.
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.
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.
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.
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