1

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

8
  • How is it not working? Your question is not yet clear. Does it generate an error on screen? Error in the logs? Do you get a blank screen? Does your computer catch on fire? Are you able to confirm that your user input variables are populated as expected? Have you dumped your rendered query to see what Joomla is generating? We need some more clues. Commented Aug 27, 2019 at 7:29
  • If you cannot get the correct result set within phpMyAdmin, please share a dbfiddle link, so that volunteers can work with your real data structure. Commented Aug 27, 2019 at 7:32
  • All input are correct and every is working good except the call of view It's just show message : error
    – wbdevlop
    Commented Aug 27, 2019 at 12:19
  • ...and the error is? Have you turned on error reporting? Commented Aug 27, 2019 at 12:22
  • $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. Commented Aug 27, 2019 at 12:24

1 Answer 1

0

I can't see everything about your component and the following is completely untested, but here are some types of things I would expect to see...

In the model:

public function closestStores($coorlat, $coorlng) {
    $db = JFactory::getDBO();
    $query = $db->getQuery(true)
        ->select($db->qn("x_subtitle", "title"))  // select more than one column if necessary
        ->from($db->qn("places")
        ->orderBy("(3956 * 2 * ASIN(SQRT(
                      POWER(SIN((" . ((float)$coorlat) . " - latitude) *  PI() /180 / 2), 2) + 
                      COS(" . (float)$coorlat . " * PI() / 180) * 
                      COS(latitude * PI() / 180) * 
                      POWER(SIN((" . ((float)$coorlng) . " - longitude) * PI() / 180 / 2), 2)
                  )))"
                 )
        ->setLimit(3);
    return $db->setQuery($query)->loadObjectList(); 
}

In the controller:

$jinput = JFactory::getApplication()->input;
// $name   = $jinput->get('text', null, 'word');
$coorlat = $jinput->get('coorlat', null, 'float');
$coorlng = $jinput->get('coorlng', null, 'float');

$view = $this->getView('searchplace', 'html');
$view->closestStores = $this->getModel('storemanager')->closestStores($coorlat, $coorlng);
$view->display(); // show the view

In the view:

foreach ($this->closestStores as $store) {
    echo "<li>{$store->title}</li>";
}

Valentin's advice about assignRef(): Joomla 3 - What to use instead of assignRef?

(...again, no guarantees about my snippets, I welcome anyone to message or edit to fix what isn't right)

p.s. These posts might be helpful:

2
  • For researchers and volunteers, this question was cross-posted at Stack Overflow and there are mild differences in the posts. Commented Aug 27, 2019 at 14:11
  • still no luck I changed everything and still nothing work
    – wbdevlop
    Commented Aug 27, 2019 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.