Multiple queries in Mysql

73 views Asked by At

I made 2 search modules for my joomla website which retrieves results from a component and appears them bellow the search input field as autocomplete.

The first module retrieves results from categories names and the second from listings names.

This is the php code i am using for categories:

<?php
header('Content-Type: application/json');
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));
require_once ( JPATH_BASE     .DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR.'defines.php' );
require_once ( JPATH_BASE     .DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR.'framework.php' );
$mainframe =& JFactory::getApplication('site',array('session'=>false));
$q = strtolower($_GET["term"]);
if (!$q) return;
$db =& JFactory::getDBO();
$query = 'SELECT cat_name FROM #__tt_cats WHERE cat_published = 1 AND cat_name LIKE ' .         $db->quote('%'.$q.'%');
$db->setQuery($query);
$result = $db->loadObjectList();
$items = array();
foreach ($result AS $row) {
$items[] = $row->cat_name;
}
$json = json_encode($items);
print_r($json);

and this is the php code for the listings names:

<?php
header('Content-Type: application/json');
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));
require_once ( JPATH_BASE .DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR.'defines.php' );
require_once ( JPATH_BASE .DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR.'framework.php' );
$mainframe =& JFactory::getApplication('site',array('session'=>false));
$q = strtolower($_GET["term"]);
if (!$q) return;
$db =& JFactory::getDBO();
$query = 'SELECT link_name FROM #__tt_links WHERE link_name LIKE ' . $db- >quote('%'.$q.'%');
$db->setQuery($query);
$result = $db->loadObjectList();
$items = array();
foreach ($result AS $row) {
$items[] = $row->link_name;
}
$json = json_encode($items);
print_r($json);

How can merge those files so i can use only one search module both for categories and listing names so when a user perform a search it will appear results from categories and listing names in the autocomplete drop down menu at the same time ??

Thanks

0

There are 0 answers