1

I have

<form action='' method='post'>
  City name <br /><br />
  <input type='text' name='search' value='' class='auto'>
</form>

and

<script type="text/javascript">
  $(function() {

    //autocomplete
    $(".auto").autocomplete({
        source: "search.php",
        minLength: 1
    });

  });
</script>

This is autocomplete. And now I receive fields from MySQL base in search.php file:

if(isset($_GET['term']))
    {
        $city = array();
        $airport = array();
        $citysearchsql = mysqli_query(db(),"SELECT * FROM citycode WHERE cityname LIKE '%".$_GET['term']."%' ");
        while($citysearchresult = mysqli_fetch_array($citysearchsql))
        {



            $airnamesql = mysqli_query(db(),"SELECT * FROM airports WHERE citycode='".$citysearchresult['citycode']."' ");
            while($airnameresult = mysqli_fetch_array($airnamesql))
            {
                $airport[] = $airnameresult['airportname'];
            }
            $city[] = $citysearchresult['cityname'];
        }




        echo json_encode($city);

    }

Base has two tables citycode and airports. When user writes city name, need to show city name and under city name show airport name, like here

http://joxi.ru/krD8oY1u0Nb0kr

I cant do it with json_encode.

any ideas, anybody can help me

3 Answers 3

2

Get all required elements in array then you can use your own custom data formats in frontend and displays by simply overriding the default focus and select actions.

$("#autocomplete").autocomplete()
.data( "autocomplete" )._renderItem = function( ul, item ) {
    return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( item.label )
        .appendTo( ul );
};

Check this link for demo example

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

Comments

0

Optimized to be just one query:

if (isset($_GET['term'])) {
        $city = array();
        $citysearchsql = mysqli_query(db(),"SELECT a.airportname, c.cityname FROM citycode c INNER JOIN airports a ON c.citycode = a.citycode WHERE c.cityname LIKE '%".$_GET['term']."%' ORDER BY c.citycode ASC");
        while($citysearchresult = mysqli_fetch_array($citysearchsql))
        {
            if (!in_array($citysearchresult['cityname'], $city)) {
                $city[] = $citysearchresult['cityname'];
            }
            $city[] = $citysearchresult['airportname'];   
        }
        echo json_encode($city);
}

1 Comment

Thank you for solution are working, but I need to stylize it somehow, like here: joxi.ru/krD8oY1u0Nb0kr I am already tried to use html tags, like &nbsp, but it doesnt work.
0

Yes you can... just merge tables

echo json_encode(array_merge($city, $airport));

Comments

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.