I'm using Joomla 3 and I have a controller with the function abc that should call a view with some parameters title & address
My controller :components/com_storemanager/controller.php
<?php
/**
* @version CVS: 1.0.1
* @package Com_storemanager
* @author LYT
* @copyright 2LYT
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access
defined('_JEXEC') or die;
jimport('joomla.application.component.controller');
/**
* Class storemanagerController
*
* @since 1.6
*/
class storemanagerController extends JControllerLegacy
{
/**
* Method to display a view.
*
* @param boolean $cachable If true, the view output will be cached
* @param mixed $urlparams An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
*
* @return JController This object to support chaining.
*
* @since 1.5
*/
public function display($cachable = false, $urlparams = false)
{
$app = JFactory::getApplication();
$view = $app->input->getCmd('view', 'places');
$app->input->set('view', $view);
parent::display($cachable, $urlparams);
return $this;
}
function abc()
{
$jinput = JFactory::getApplication()->input;
$coorlat = $jinput->get('coorlat');
$coorlng = $jinput->get('coorlng');
$query = "SELECT x_subtitle, telephone, address, (3956 * 2 * ASIN(SQRT( POWER(SIN(( $coorlat - latitude) * pi()/180 / 2), 2)
+COS( $coorlat * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( $coorlng - longitude) * pi()/180 / 2), 2) )))
as distance from places order by distance LIMIT 3";
$db = JFactory::getDBO();
$db->setQuery($query);
$articles = $db->loadObjectList();
foreach($articles as $article)
{
$view = $this->getView('searchplace','html'); //get the view
$view->title = $article->x_subtitle; //set the data to the view
$view->display(); //show the view
}
exit;
}
My view : components/com_storemanager/views/searchresult/view.html.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* HTML View class for the HelloWorld Component
*
* @since 0.0.1
*/
class storemanagerViewSearchplaces extends JViewLegacy
{
//public $title;
public function display($tpl = null)
{
echo "<li>{$store->title}</li>";
}
}
?>
if I use var_dump($articles) I get
array(3) { [0]=> object(stdClass)#335 (4) { ["x_subtitle"]=> string(17) "Varennes" ["telephone"]=> string(12) "450-652-2133" ["address"]=> string(48) "1250, boul. Lionel Boulet, Varennes, QC, J3X X1P" ["distance"]=> string(18) "15.077548488524313" } [1]=> object(stdClass)#336 (4) { ["x_subtitle"]=> string(19) "Repentigny" ["telephone"]=> string(12) "450-657-0101" ["address"]=> string(45) "75, rue de Normandie, Repentigny, QC, J6A 7B3" ["distance"]=> string(18) "16.434693660810648" } [2]=> object(stdClass)#337 (4) { ["x_subtitle"]=> string(15) "Delson" ["telephone"]=> string(12) "450-845-9842" ["address"]=> string(35) "59C, Route 132 Delson, QC, J1B 1H1" ["distance"]=> string(18) "17.146666885006635" } }
But if I use view I get just text on console network says ERROR with 500 that all
$title
and$address
are intended to be 3 separate strings right? Right now, it looks like you are overwriting them in the loop. Does your view iterate the date? How are you displaying the 3 sets of results? I don't reckon$view->display()
should be inside the loop. I'd probably send the whole$db->loadObjectList()
array of objects back to the view and let the view iterate it. Strictly speaking, you shouldn't be executing any queries in the controller, that's what the model is for.