让json内部自动完成

时间:2015-12-23 10:56:11

标签: javascript php jquery json autocomplete

当我获得包含项目的json数据时,自动完成功能怎么办?我可以说它正在成为一个列表,但它没有填充名称。

我的json数据示例:http://nettport.com/no/stram/search.php?term=e

enter image description here

<script>
    $(function() {
            $( "#inp_meal_a_0" ).autocomplete({
                source: 'search.php'
            });
            $('#inp_meal_a_0').on('autocompleteselect', function (e, ui) {
                var val = $('#inp_size_a_0').val();
                var energy = ui.item.energy;
                var proteins = ui.item.proteins;
                var carbohydrates = ui.item.carbohydrates;
                var fat = ui.item.fat;
                $("#inp_energy_a_0").val((energy*val)/100);
                $("#inp_proteins_a_0").val((proteins*val)/100);
                $("#inp_carbs_a_0").val((carbohydrates*val)/100);
                $("#inp_fat_a_0").val((fat*val)/100);
            });
    });

</script>

的search.php

<?php
header('Content-type: text/html; charset=windows-1252;');

// Make a MySQL Connection
$host = $_SERVER['HTTP_HOST'];
if($host == "127.1.1.0"){
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("n") or die(mysql_error());
}



// Quote smart
function quote_smart($value){
    // Stripslashes
    if (get_magic_quotes_gpc() && !is_null($value) ) {
               $value = stripslashes($value);
        }

        //Change decimal values from , to . if applicable
        if( is_numeric($value) && strpos($value,',') !== false ){
                $value = str_replace(',','.',$value);
        }
        if( is_null($value) ){
                $value = 'NULL';
        }
        // Quote if not integer or null
        elseif (!is_numeric($value)) {
                $value = "'" . mysql_real_escape_string($value) . "'";
        }

        return $value;
}




if(isset($_GET['term']) && $_GET['term'] != ''){
    $term = $_GET['term'];
    $term = strip_tags(stripslashes($term));
    $term = trim($term);
    $term = strtolower($term);
    $term = $term . "%";
    $part_mysql = quote_smart($term);


    //get matched data from skills table
    $x = 0;
    $query = "SELECT cc_id, cc_food_name, cc_manufactor_name, cc_serving_size, cc_serving_mesurment, cc_energy, cc_proteins, cc_carbohydrates, cc_fat FROM stram_calorie_calculation WHERE cc_food_name LIKE $part_mysql";
    $r = mysql_query($query);
    while ($row = mysql_fetch_array($r, MYSQL_NUM)) {
        $get_cc_id          = $row[0];
        $get_cc_food_name       = $row[1];
        $get_cc_manufactor_name     = $row[2];
        $get_cc_serving_size        = $row[3];
        $get_cc_serving_mesurment   = $row[4];
        $get_cc_energy          = $row[5];
        $get_cc_proteins        = $row[6];
        $get_cc_carbohydrates       = $row[7];
        $get_cc_fat             = $row[8];

        if($get_cc_food_name == ""){
            $result = mysql_query("DELETE FROM stram_calorie_calculation WHERE cc_id='$get_cc_id'") or die(mysql_error()); 
        }

            $data[$x] = array('food_name' => $get_cc_food_name, 'energy' => $get_cc_energy, 'proteins' => $get_cc_proteins, 'carbohydrates' => $get_cc_carbohydrates, 'fat' => $get_cc_fat);

        //$data[$x] = $get_cc_food_name;
        $x++;
    }
    //return json data
    echo json_encode($data);
}
?>

0 个答案:

没有答案