用于自动完成jQueryUI的MYSQL到JSON

时间:2012-07-11 00:48:46

标签: php javascript jquery jquery-ui autocomplete

下面的代码展示了我正在做的事情。基本上,目标是从数据库中检索数据并将其转换为jQueryUI的JSON格式。使用下面的方法和一个不使用array_to_json函数的方法,我能够获得返回的JSON数据。例如。: [{“name”:“Kurt Schneider”},{“name”:“Sam Tsui”},{“name”:“Christina Grimmie”}]

但问题是它仍然无法使用自动完成功能。我用下面的代码替换了示例中提供的search.php,所以这显然是这个代码的一个问题。

我开始只使用这段代码:

<?php
    include 'connect.php';
    mysql_select_db("database", $con);
    $search = mysql_query("SELECT name FROM artist");
    $rows = array();
    while($row = mysql_fetch_assoc($search)) {
        $result[] = $row;
    }
print json_encode($result);
?>

与以下代码有效地获得了相同的结果(除了间距的一些差异): (直接从示例中复制了array_to_json函数。)

<?php
    include 'connect.php';
    mysql_select_db("database", $con);
    $search = mysql_query("SELECT name FROM artist");
    $rows = array();
    while($row = mysql_fetch_assoc($search)) {
        $result[] = $row;
    }

    function array_to_json( $array ){

    if( !is_array( $array ) ){
        return false;
    }

    $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
    if( $associative ){

        $construct = array();
        foreach( $array as $key => $value ){

            // We first copy each key/value pair into a staging array,
            // formatting each key and value properly as we go.

            // Format the key:
            if( is_numeric($key) ){
                $key = "key_$key";
            }
            $key = "\"".addslashes($key)."\"";

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "\"".addslashes($value)."\"";
            }

            // Add to staging array:
            $construct[] = "$key: $value";
        }

        // Then we collapse the staging array into the JSON form:
        $result = "{ " . implode( ", ", $construct ) . " }";

    } else { // If the array is a vector (not associative):

        $construct = array();
        foreach( $array as $value ){

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "'".addslashes($value)."'";
            }

            // Add to staging array:
            $construct[] = $value;
        }

        // Then we collapse the staging array into the JSON form:
        $result = "[ " . implode( ", ", $construct ) . " ]";
    }

    return $result;
}

    echo array_to_json($result);
?>

1 个答案:

答案 0 :(得分:0)

<?php
    include 'connect.php';
    mysql_select_db("database", $con);
    $search = mysql_query("SELECT name FROM artist");
    $rows = array();
    while($row = mysql_fetch_assoc($search)) {
        //don't add the array, just the name.
        $result[] = $row["name"];
    }
print json_encode($result);
?>

上面的代码应该返回JSON

["Kurt Schneider", "Sam Tsui", "Christina Grimmie"]