I am trying to get each result with a key. I have a input field in which i am passing the keywords. Here In this Controller i am checking the each keyword for the city, if it Contain a cityname then i will do my stuff. But How can i differentiate both of them with a different key.
Here $data array has city and suggestion key which i want to access this in the ajax.
Like this-
$data['result]['city'] = ...
$data['result]['suggestion'] =
Controller
function autoshow() {
$data = array('result' => array(),'url' => array(), 'exp' => array());
$keyword = $this->input->get('search');
$search = $this->Mdl_home->get_auto_complete($keyword);
$explode = explode(' ', $keyword);
//Here i am checking if keyword has city name or
foreach ($explode as $cityname) {
$data['result']['city'] = $cityname;
}
foreach($search as $row) {
$usrch[] = $row->service_name;
$data['result']['suggesition'] = $usrch;
//Here i am using url to access this in the ajax
}
if (!isset($data)) {
$data = "";
}
$this->output->set_content_type('application/json');
echo json_encode($data);
}
Here i am trying to access with the define key name in controller
$.each(data.result, function (i, obj) {
autoData += "<li><a id='"
+ obj['suggesition']
+ "' href='javascript:void(0);'>"
+ obj['suggesition']
+(obj['city']) ? obj['city'] : " "
+ "</a></li>";
});
autoData += "</ul>";
$("#s_auto").html(autoData);
What i tried: This is giving me undefined. I know there must be some problem in $.each loop. How to access the key $obj['key'] in this way .. I do not know How to do this ..Please see this Code
If i remove result from $.each(data.result, function (i, obj) from this and simply i can access key obj['key'] it works but i have multiple type of data in different types. So to access those keys..
If i am doing this is in wrong please tell me. or any type of suggestions Thank you..
This is the result which is showing in the console. After searching "ho" in the keyword
{"result":{"city":"","suggesition":"7 Star Hotels"},"suggesition":[]}
$dataresults at end. since you looping ondata.resultresult should contain array of objects like{ "result":[{ city:"city1", suggestion:"suggestin" },{ city:"city2", suggestion:"suggestin2" }]}explodefrom i doing something else with this