In most components, you'll have at least one view where you display the contents of a single record. Let's take a look at a standard way of doing this in Joomla.
First, let's create tables folder. This tables folder needs to go into the backend of the component. Even though it's going into the backend of the component, the frontend of the component is going to know to look in the backend for these classes. This is just the standard that Joomla has set for these table classes.
So let's go to the backend of the component, in administrator>components >com_noidacity and we are going to create place.php file inside tables folder and add the following code
So inside of place.php is a class called NoidaCityTablePlace and that follows the naming convention of the name of the component, table and then the name of the table that we want to work with.
And so here, we are just overriding the constructor which by default takes in a database object(&$db) and we are right away calling the parent constructor and we are passing in the name of the table(#__noidacity_places) we want to work with along with the key that we want to use which is place_id, and then we are passing in again the database object($db).
What this class allows us to do is it allows us to work with all of the create, read, update, delete functionality that we would ever want to perform on the noidacity_places table in the database. That way we don't have to write, select and update and insert statements, we can just do it all through here.
So let's go back to the frontend to actually use this table class.
Go to components>com_noidacity and go to the models folder because we are going to add a new model. Go to the models folder and create place.php and add the following code.
We have declared another model class here that we are going to use. This model class has a function called getItem and getItem is pulling in the id from the getURL request.
So here we have getInt the id from the URL and storing it in place_id and notice that we are using JRequest to get this value and we are using the getInt function and that's making sure that we're getting an integer and not a string or any other value that is not an integer
Then we are getting a row object. So we are getting a row object here through JTable::getInstance by passing in 'place' and 'NoidaCityTable' and Joomla assembles all this into NoidaCityTablePlace and returns the row object.
Then once we have the row object, we can call the load function passing in the place_id and that will load the id that matches in the database and finally we return the row object with the data preloaded.
So we are finally going to add a new view.
Create the view.html.php file inside the view of our com_noidacity component, and add the following lines of code into view.html.php file
You will notice we have a display function and it is getting the item through the getItem function in the model, but it's calling it indirectly, and then it's assigning the value to this item.
And then finally, create default.php layout file inside your tmpl folder inside the place view and add the following line of code
You will notice that there are two things being pulled from the item. We have the place_name and the place_description and both of these are just going to be output here in the layout.
So let's see all this in action. We are going to load the place view and load the record with id=1.
So let's go to index.php, we are going to pull up the noidacity option for the noidacity component, we are going to do view=place & id=1. And you will notice, it pulls in the header of Pragati Maidan, and then it pulls in the description for the Pragati Maidan place.
So all we had to do was define a JTable class, have the model, load the JTable class, return it, and then pass it along to the view and then the view is able to output all of the HTML here. So whatever you want to display just a single record in Joomla, just use a JTable class along with your model and then assign it to the view.