duanji1056 2012-07-11 00:48
浏览 47

用于自动完成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);
?>
  • 写回答

1条回答 默认 最新

  • duanmei1850 2012-07-11 04:42
    关注
    <?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"]
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料