0

select2 プラグインを使用して、MySQL リモート データをタグとして使用したいと考えています。しかし、JSON の学習曲線は非常に弱いです。この JSON 形式の修正を手伝ってください。

$("#e7").select2({
    placeholder: "Search for a movie",
    minimumInputLength: 3,
    ajax: {
        url: "search.php",
        dataType: 'jsonp',
        quietMillis: 100,
        data: function (term, page) { // page is the one-based page number tracked by Select2
            return {
                q: term, //search term
                page_limit: 10, // page size
                page: page, // page number
                apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
            };
        },
        results: function (data, page) {
            var more = (page * 10) < data.total; // whether or not there are more results available

            // notice we return the value of more so Select2 knows if more results can be loaded
            return {
                results: data.movies,
                more: more
            };
        }
    },
    formatResult: movieFormatResult, // omitted for brevity, see the source of this page
    formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
    dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
    escapeMarkup: function (m) {
        return m;
    } // we do not want to escape markup since we are displaying html in results
});

そして、これがsearch.phpです

<?php
    $sql=mysqli_query($db3->connection,"SELECT * FROM tags");

    while($row=mysqli_fetch_array($sql)){
        $tags=$row['tags'];
        $id=$row['id'];

        $response=array();
        $response['tags']=$tags;
        $response['id']=$id;     
    }

    echo json_encode($response);
?>
4

1 に答える 1

0

while ループが $response 配列を埋めていません。$response=配列(); ループの各反復で $response をリセットしています

$sql=mysqli_query($db3->connection,"SELECT * FROM tags");

$response=array();
while($row=mysqli_fetch_array($sql)){
    $tags=$row['tags'];
    $id=$row['id'];

    $response[]=array('tags'=>$tags, 'id'=>$id);

}

echo json_encode($response);
于 2013-03-25T10:31:28.267 に答える