1

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":[]}
4
  • can you show the example data of ajax response data console.log(data); Commented Jul 22, 2017 at 11:43
  • i just update my console data Commented Jul 22, 2017 at 11:55
  • i think something wrong with data preparation. you should see what exact $data results at end. since you looping on data.result result should contain array of objects like { "result":[{ city:"city1", suggestion:"suggestin" },{ city:"city2", suggestion:"suggestin2" }]} Commented Jul 22, 2017 at 12:01
  • I forgot to remove explode from i doing something else with this Commented Jul 22, 2017 at 12:05

1 Answer 1

1

You are overwriting the same $data['result']['suggesition'] in each iteration.

In order to make the javascript work as written you need something like:

foreach($search as $row) {
      $res = array(
             'suggesition'=> $row->service_name,
             'city' => // ?? I don't understand city logic
            );

      // add new item to `$data['result']` array
      $data['result'][] = $res;

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You.. and i know city logic should not be there. But i'm just asking if i explode the keywords and then if ii run a foreach to find out whether my keywords contains a city name or not.. Is this a right way..
Possibly, depending on what is sent. You would have to compare each one to something though to validate as a city. Also depends how your database queries are made

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.