MySQL数据库中的范围滑块和值

时间:2016-01-30 09:21:31

标签: php jquery mysql

我已经采用了一个范围滑块小部件并根据我的需要进行了自定义:

$(function () { $inputFrom = $(".js-input-from"),
$inputTo = $(".js-input-to"),

    $("#kalorie").ionRangeSlider({
        hide_min_max: false,
        keyboard: true,
        max_postfix: "+",
        max: 4000,
        min: 500,
        from: 2,
         to: 5,
        type: 'double',
        postfix: "kcal",
        grid: true,
        values: [
    "500", "1000", "1250",
    "1500", "1750",
    "2000", "2250", "2500", "2750", "3000", "3500", "4000" ],
        onStart: updateInputs,
onChange: updateInputs,
onFinish: updateInputs
    });

});

我现在要做的是将其中的 values [] 替换为我从MySQL数据库中提取的内容(如10个不同的值排序ASC)

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}  

$sql2 = "SELECT DISTINCT * FROM TABLE";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
    // output data of each row
    while($row2 = $result2->fetch_assoc()) { 

如何将输出保存到变量中,以便我可以在滑块代码中链接它?

1 个答案:

答案 0 :(得分:0)

逐个从数据库中获取记录并将所有记录存储在一个数组中:

while($row2 = $result2->fetch_assoc()) { 
     $all_values[] = $row2['column_name']; // store all records in one array
}

$final_values = implode(",",$all_values);

然后输入$ final_valuse variable in your javascript代码

 $("#kalorie").ionRangeSlider({
        hide_min_max: false,
        keyboard: true,
        max_postfix: "+",
        max: 4000,
        min: 500,
        from: 2,
         to: 5,
        type: 'double',
        postfix: "kcal",
        grid: true,
        values: [<?php echo $final_values; ?>], // echo your PHP variable here
        onStart: updateInputs,
onChange: updateInputs,
onFinish: updateInputs
    });