While single and list views are helpful, you'll often also want to display records that is from different tables in the same view. Let's mix some view data right now.
So we want to open the models folder and go into the place.php file in there, and add the following function called getMalls (the code for this file is as following).
So now we have a getMalls function that's in the place model
We are still getting the place_id just like in getItem, but this time we are building a custom query to get all the malls that are assigned to that place.
So we are getting a fresh database object from the model, and then we are getting a fresh query from that database object.
Then we are building up our query to get the place name, id, location and teaser from the malls table, for all the malls that are assigned to this place(table).
We are then setting that query in the database, and then we are loading the result set using loadObjectList. This is going to return an array of objects so we can cycle over them in the view.
So now that we have this function getMalls in the model, let's go to the view and update it so that we have the data ready to present.Go to views, and go to place.
Go to view.html.php and open it, and add the following code.
You will notice that we have malls as a protected property as well as getMalls.
So that's going to pull in the malls and assign it to our view object so that it is ready to be displayed.
So finally, now that we have the data from the model, let's open up the layout that's in default.php which is in tmpl folder for our view for place and we are going to replace the layout code that we currently have with the following code.
So currently, we are just displaying the place_name along with the place_description.
So now we have a header that's identifying the malls apart from the description and the title of the place.
And we are cycling over each mall and providing a length to each.
So let's go and load one of the places. Let's go to index.php and then view=place and then id=1. This loaded the place title, as well as the place description, and then underneath, we have loaded every mall that is assigned to this place. So if we click on the link for one of these malls, it takes us right to that mall. The expansion of the place view now makes it possible for users to navigate from an place, over to a mall on that place.
The flexibility of Jmodel, allows us to work with multiple data sets in the same model, and then displaying all of those data sets in the view.