简体   繁体   English

从数据库获取jQuery滑块值

[英]Get jQuery slider values from DB

I have a database with these tables 我有一个包含这些表的数据库

company
==========
id | name |
==========
 1 | C1   |
 2 | C2   |
 3 | C3   |

 position
 =================
id | level | name |
==================
 1 |  1    | SE   |
 2 |  2    | SE1  | 
 3 |  3    | SE2  |
 4 |  1    | SA   |
 5 |  2    | SA1  |
 3 |  3    | SA2  |

 company_position
 ==========
 cid | pid |
 ==========
  1  |  1  |
  1  |  2  |
  1  |  3  |
  2  |  1  |
  2  |  2  |
  2  |  3  |
  3  |  1  |
  3  |  2  |
  3  |  3  |

In the UI, I have a <select> for displaying Company names. 在UI中,我有一个<select>用于显示公司名称。 There is a corresponsding slider that should display Position range. 有一个相应的滑块应显示位置范围。 onChange on the <select> , I would like the slider the receive new values for range (min, max, name of Position instead of value). <select>上的onChange,我希望滑块接收范围的新值(最小值,最大值,位置名称而不是值)。 How do I go about solving this? 我该如何解决这个问题? jQuery getJSON? jQuery getJSON? I am not looking for code, rather an approach! 我不是在寻找代码,而是一种方法!

UPDATE I realized that my problem is that I am unable to imagine the structure of JSON callback data. 更新我意识到我的问题是我无法想象JSON回调数据的结构。

I would go the AJAX way, just have your onChange call a jQuery POST function that passes the <select> value (so you can filter properly), and anything else you need. 我会采用AJAX方式,只是让您的onChange调用传递<select>值的jQuery POST函数(以便您可以正确过滤),以及您需要的其他任何东西。 And then on callback, you'll recieve the desired values properly encoded (json_encode is an option) for reading or just in plain text. 然后在回调中,您将收到正确编码的所需值(json_encode是一个选项),用于阅读或纯文本。 But remember the data from the callback function is just one variable, if you need more (eg minium and maxium values), encode the desired data so you can access it later 但是请记住,回调函数中的data只是一个变量,如果您需要更多(例如,最小值和最大值),请对所需数据进行编码,以便以后可以访问

Hope this helps you! 希望这对你有所帮助!

Let's say you do a $.post to your server, with the selected company. 假设您使用所选公司向服务器执行$.post Let's say the user selected company 1. Then, on the server, you want to a query like: 假设用户选择了公司1。然后,在服务器上,您想要查询如下内容:

SELECT position.level, position.name FROM position
JOIN company_position ON position.id = company_position.pid
JOIN company ON company.id = company_position.cid
WHERE company.id = ?

Now, you should have a result set with (1, SE), (2, SE1), (3, SE2) given your data above. 现在,鉴于上面的数据,您应该将结果集设置为(1, SE), (2, SE1), (3, SE2) I would send this back as a JSON object (I don't know what server environment you have, so building this structure is up to you): 我会把它作为JSON对象发回(我不知道你有什么服务器环境,所以构建这个结构取决于你):

{
  "min": 1,
  "max": 3,
  "positions": {
    "1": "SE",
    "2": "SE1",
    "3": "SE2"
  }
}

Alternately, if there are few positions, you could let the client calculate max and min, just sending JSON: 或者,如果位置很少,您可以让客户端计算最大值和最小值,只需发送JSON:

{
  "1": "SE",
  "2": "SE1",
  "3": "SE2"
}

And calculating max, min with javascript: 并使用javascript计算最大,最小:

var max = Math.max.apply(null, Object.keys(response))
  , min = Math.min.apply(null, Object.keys(response));

In your javascript, given the object 在您的JavaScript中,给定对象

var result = {
  "min": 1,
  "max": 3,
  "positions": {
    "1": "SE",
    "2": "SE1",
    "3": "SE2"
  }
};

You can query that object like this: positions["1"] == "SE" . 您可以像这样查询该对象: positions["1"] == "SE" So, if you want to show the labels for min and max, you would find the values for min and max like so: 因此,如果要显示最小值和最大值的标签,您会找到min和max的值,如下所示:

var min = result.min, max = result.max

And you find their labels like this: 您会发现他们的标签如下:

var minLabel = result.positions[result.min]
  , maxLabel = result.positions[result.max];

I posted an answer a couple of minutes ago. 几分钟前我发了一个答案 Maybe this will help, maybe not. 也许这会有所帮助,也许不会。

Why don't you use something like this: 你为什么不用这样的东西:

jQuery jQuery的

$(".menu_item").mouseover(function(){   
    var id = $(".selected").attr("id");
    var qst = "?id="+id;
    var html = '';
    $.getJSON('ajax/get_sub_cats.php'+qst, function(data){
        var len = data.length;
        for (var i = 0; i< len; i++) {
            html += '<a id="subCatLink'+data[i].Sub_Cat_ID+'" href="products.php?id='+data[i].Sub_Cat_ID+'">'+data[i].Sub_Cat_Name+'</a>';
        }
        $(".sub_menu[id="+id+"]").html(html);
    });
});

PHP PHP

 require('../_req/base.php');
    $return = array();

    $id = $_REQUEST['id'];
    $sql = "select * from sub_cats where Main_Cat_ID = '$id'";
    $result = mysql_query($sql);

    while($ln = mysql_fetch_array($result)){
        $return[] = $ln;
    }

    echo json_encode($return);

As shown in the sample (PHP, but you can do some other like serialization on asp.net) I use json_encode. 如示例所示(PHP,但您可以在asp.net上执行其他类似序列化操作),我使用json_encode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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