开发者

Generate array in PHP for JavaScript function

<script>

$(function() {

    var availableTags = [

        "ActionScript",

        "AppleScript",

        "Asp",

        "BASIC",

        "C",

        "C++",

        "Clojure",

        "COBOL",

        "ColdFusion",

        "Erlang",

        "Fortran",

        "Groovy"
    ];

    $( "#fname" ).autocomplete({

        source: availableTags

    });

});

The above function helps me to achieve auto complete using static values, what I want is to get these values from the database. I wrote a php function that connect to the db but I am not sure how to pass the return value of that functio开发者_开发问答n to the javascript page below is snippet of that.

function getAllDrugs(){
$med = "";
$select = mysql_query("SELECT dname FROM phar_store_config where quantity > 1");

while($row =  mysql_fetch_array($select)){

$med = $med.$row['dname'];

}
return $med;
}

Now i want the return value of the above php function to be passed to my js function.

Any help will be appreciated.


You may use the function json_encode() and modify your code like this:

function getAllDrugs(){
  $med = array();
  $select = mysql_query("SELECT dname FROM phar_store_config where quantity > 1");

  while($row =  mysql_fetch_array($select)){

    $med[] = $row['dname'];
  }
  return $med;
}

(this will turn the result into an array of names instead of a string). You can then write, in the tag:

$(function() {

    $( "#fname" ).autocomplete({

        source: <?php echo json_encode(getAllDrugs()); ?>

    });

});


Here is an exemple of a autocomplete working with JSON :

$( "#fname" ).autocomplete({
    source: "autoAcc.php",
    dataType:'json',
    minLength: 0,
    delay:0,
    select: function( event, ui ) {
        $( "#fname" ).val( ui.item.classe );
        return false;
    }

}).data( "autocomplete" )._renderItem = function( ul, item ) {
    if(!item.classe) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append('<a style="color:red;font-weight:bold;">No result !</a>' )
            .appendTo( ul );
    } else {
    return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append(    '<a>' + item.classe + '</a>' )
        .appendTo( ul );
    }
};

$( "#fname" ).click(function() {
    $( "#fname" ).autocomplete("search","");
});

Assuming classe is the column i want to show out in the "listbox".

And here is my PHP code to generate JSON (autoAcc.php) :

$requete = 'SELECT * FROM tbl_administrators';
$result = mysql_query($requete);
$rows = array();
while($r = mysql_fetch_assoc($result)) {
    $rows[] = $r;
}
echo json_encode($rows);

I hope this will help you, if you want any further information about this code just ask.


Assuming you're using jQuery.

If you store the values as a JSON array, you can do $.parseJSON or $.getJSON then parse the values to your JS function.


$(function() {
    var availableTags = [<?="'".implode("','",getAllDrugs())."'"?>];

    $( "#fname" ).autocomplete({
        source: availableTags
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜