How to Mix View Data in Joomla

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.

Editing place file inside our model

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


                      
  1. public function getMalls()
  2. {
  3. $place_id = $JRequest::getInt('id');
  4.  
  5. $db = $this->getDbo();
  6. $query = $this->getQuery(true);
  7.  
  8. $query->select('mall_name, mall_id, mall_location, mall_teaser');
  9. $query->from('#__noidacity_malls');
  10. $query->where("place_id = '{$place_id}'");
  11.  
  12. $db->setQuery($query);
  13. $rows = $db->loadObjectList();
  14.  
  15. return $rows;
  16. }

                      

                      
  1. public function getMalls()
  2. {
  3.  
  4. ...
  5. ...
  6. }

                      

So now we have a getMalls function that's in the place model


                      
  1. $place_id = $JRequest::getInt('id');
  2.  

                      

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.


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

                      

So we are getting a fresh database object from the model, and then we are getting a fresh query from that database object.


                      
  1. $query->select('mall_name, mall_id, mall_location, mall_teaser');
  2. $query->from('#__noidacity_malls');
  3. $query->where('place_id' = '{$place_id}');

                      

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


                      
  1. $db->setQuery($query);
  2. $rows = $db->loadObjectList();
  3.  
  4. return $rows;

                      

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.

view html of places

Go to view.html.php and open it, and add the following code.


                      
  1. <?php
  2.  
  3. defined ( '_JEXEC' ) or die;
  4.  
  5. jimport('joomla.application.component.view');
  6.  
  7. class NoidaCityViewPlace extends JView
  8. {
  9. protected $item
  10. protected $malls;
  11.  
  12. public function display($tpl = null)
  13. {
  14. $this->item = $this->get('Item');
  15. $this->malls = $this->get('Malls');
  16.  
  17. parent::display($tpl);
  18. }
  19. }
  20.  

                      

                      
  1. protected $malls;

                      

You will notice that we have malls as a protected property as well as getMalls.


                      
  1. $this->malls = $this->get('Malls');

                      

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.


                      
  1. <?php defined( '_JEXEC' ) or die; ?>
  2. <h1 id="place_name"><?php echo $this->escape($this->item->place_name); ?></h1>
  3.  
  4. <div id="place">
  5. <div id="place_left">
  6. <p id="place_description">
  7. <?php echo $this->item->place_description; ?>
  8. </p>
  9.  
  10. <h2><?php echo JText::_('COM_NOIDACITY_MALLS'); ?></h2>
  11.  
  12. <ol>
  13. <?php foreach($this->malls as $m): ?>
  14. <?php $url = "index.php?option=com_noidacity&view=mall&id=".
  15. $m->mall_id; ?>
  16. <li>
  17. <a href="<?php echo JRoute::_($url); ?>"><?php echo $this->escape($m->mall_name) ?></a> -
  18. <?php echo $this->escape($m->mall_teaser); ?>
  19. </li>
  20. <?php endforeach; ?>
  21. </ol>
  22. </div>
  23. </div>

                      

                      
  1. <?php defined( '_JEXEC' ) or die; ?>
  2. <h1 id="place_name"><?php echo $this->escape($this->item->place_name); ?></h1>
  3.  
  4. <div id="place">
  5. <div id="place_left">
  6. <p id="place_description">
  7. <?php echo $this->item->place_description; ?>
  8. </p>

                      

So currently, we are just displaying the place_name along with the place_description.


                      
  1. <h2><?php echo JText::_('COM_NOIDACITY_MALLS'); ?></h2>

                      

So now we have a header that's identifying the malls apart from the description and the title of the place.


                      
  1. <ol>
  2. <?php foreach($this->malls as $m): ?>
  3. <?php $url = "index.php?option=com_noidacity&view=mall&id=".
  4. $m->mall_id; ?>
  5. <li>
  6. <a href="<?php echo JRoute::_($url); ?>"><?php echo $this->escape($m->mall_name) ?></a> -
  7. <?php echo $this->escape($m->mall_teaser); ?>
  8. </li>
  9. <?php endforeach; ?>
  10. </ol>

                      

And we are cycling over each mall and providing a length to each.

Pulling various malls from database

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.