用于自动完成jQueryUI的MYSQL到JSON

My code below show what I'm doing. Basically the aim is to retrieve data from a database and convert it to a JSON format for jQueryUI. With the method below and a method that didn't use the array_to_json function I was able to get JSON data returned. Eg.: [ { "name": "Kurt Schneider" }, { "name": "Sam Tsui" }, { "name": "Christina Grimmie" } ]

But the issue is that it still doesn't function with the autocomplete. I replaced the below code with the search.php that was provided with the example, so it's obviously an issue with this code.

I began with only this code:

<?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);
?>

Which achieved affectively the same results as the below code (besides some differences in spacing): (The array_to_json function was copied direct from the example).

<?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);
?>
duanping6698
duanping6698 刚刚在这里发布了一个完整的解
大约 8 年之前 回复
doubairan4213
doubairan4213 我找到了一个解决方案,但不能再发布4个小时了。请参阅:stackoverflow.com/questions/4234455/...
大约 8 年之前 回复
dongtuoji5396
dongtuoji5396 我不确定它会如何转换为PHP...
大约 8 年之前 回复
duanchen6423
duanchen6423 就字符串数组而言,我们在谈论array(0)=“dfgfdsg”;等或你上面谈到的形式?
大约 8 年之前 回复
dr5648
dr5648 以下是第一个代码的示例结果:[{“name”:“KurtSchneider”},{“name”:“SamTsui”},{“name”:“ChristinaGrimmie”}]
大约 8 年之前 回复
dongzhuner6981
dongzhuner6981 让你的PHP返回一个简单的字符串数组:[“KurtSchneider”,“SamTsui”,“ChristinaGrimmie”]
大约 8 年之前 回复
doukaizha5417
doukaizha5417 你能展示第一个代码的样本结果吗?
大约 8 年之前 回复

1个回答

<?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);
?>

The code above should return the JSON

["Kurt Schneider", "Sam Tsui", "Christina Grimmie"]
douzhi1972
douzhi1972 完全解决了。 等到我可以发布答案。 谢谢:)
大约 8 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问