简体   繁体   中英

MYSQL to JSON for Autocomplete jQueryUI

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM