0

I get the variables city and province from autocoplete google map v3

Check if the query with search by city is full, otherwise filter povincie if empty filter by distance.

Is there an alternative to this that forces me to duplicate joins, where for each query?

Example:

<?php
$city = "Milano";
$povince = "MI";

public function getItmes(){

    $province = JRequest::getVar('p','MI');
    $city     = JRequest::getVar('c','Milano');
    $lat      = JRequest::getVar('lat');
    $lng      = JRequest::getVar('lng');

    // check by city
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select('t.*');
    $query->from('#__table_list as t');
    // Join table
        $query->select('j.name');
        $query->join('LEFT', '#__table_join AS j ON t.id = j.id');

        // more join....
    $query->where('t.city="'.$city.'"');
    $db->setQuery($query);
    $results = $db->loadObjectList();

    if($results) { 
        return $results;
    } else { 

    // check by province
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('*.t');
        $query->from('#__table_list as t');
        // Join table
        $query->select('j.name');
        $query->join('LEFT', '#__table_join AS j ON t.id = j.id');

        // more join....

        $query->where('t.province="'.$province.'"');
        $db->setQuery($query);
        $results = $db->loadObjectList();

        if($results) { 
            return $results;
        } else { 
            // check by distance
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $query->select('*.t, ( 3959 * acos( cos( radians('.$lat.') ) * cos( radians( t.lat ) ) * cos( radians( t.lng ) - radians('.$lng.') ) + sin( radians('.$lat.') ) * sin(radians(t.lat)) ) ) AS distance');
            $query->from('#__table_list as t');
            // Join table
            $query->select('j.name');
            $query->join('LEFT', '#__table_join AS j ON t.id = j.id');

            // more join....

            $query->where('t.province="'.$province.'"');
            $db->setQuery($query);
            $results = $db->loadObjectList();
            if($results) { 
                return $results;
            } else { 
                return "no results";
            }
        }
    }
?>

This method seems repetitive, What do you recommend?

Maxw1975
  • 11
  • 4
  • Don't use `JRequest` with Joomla 3.x as it's deprecated. Please refer to the documentation: http://docs.joomla.org/Retrieving_request_data_using_JInput ;) – Lodder Mar 03 '15 at 16:19
  • ok thanks for the query you have an alternative solution? – Maxw1975 Mar 03 '15 at 16:38
  • Your question is not clear... So do you want to perform join operation but can you elaborate on the condition about fetching the cities and provinces? – AniV Mar 03 '15 at 22:11

0 Answers0