So I'm using x-editable for bootstrap, which is awsome for me.
所以我使用x-editable进行引导,这对我来说很棒。
can anyone help me how to set option values from mysql database into x-editable select options
谁能帮助我如何将mysql数据库中的选项值设置为x-editable选项
this is my php code
这是我的PHP代码
$result1 = ("SELECT * FROM users where type='agent'") or mysql_error();
$users=mysql_query($result1);
while($rowa = mysql_fetch_array($users)){
?>
<option name="agent" value = "<?php echo $rowa['username']; ?>" >
<?php echo $rowa['username']; ?>
</option>
<?php
}
?>
2 个解决方案
#1
0
So what you're going to want to do is actually create a JSON string which you will put into an anchor
element. Let's see what that looks like.
所以你想要做的实际上是创建一个JSON字符串,你将它放入一个锚元素。让我们看看它是什么样的。
First, you need to push the usernames into an array object it, then json encode it. That'll look like this:
首先,您需要将用户名推送到数组对象中,然后json对其进行编码。这看起来像这样:
$usernames = array();
while(.....){
$usernames[] = $rowa['username'];
}
$usernames = json_encode($usernames);
<a href="#"
id="username" //your reference
data-type="select" //so the x-editable library makes a select list
data-name="username"
data-pk="1"
data-value="5"
data-original-title="Select Username"
data-source="<?php echo $usernames?"> //this is your JSON array of usernames.
">
Username
</a>
And this will produce a select list of usernames.
这将生成一个用户名的选择列表。
Here's your functioning jsFiddle
这是你的功能jsFiddle
#2
0
I also stumbled upon the same problems as Johannes Kiehl. However, after a little investigation I found the following and finally fixed it - somehow. It may not be the most elegant approach, hence ideas for improvement are very welcome.
我也偶然发现了和Johannes Kiehl一样的问题。然而,经过一番调查后,我发现以下内容并最终解决了 - 不知何故。它可能不是最优雅的方法,因此非常欢迎改进的想法。
Here's what I wanted to achieve: In a select field, results from a MySQL-Query shall be displayed. Upon success on the select, I want to refresh a div with the new content, also loaded via an external PHP-file.
这是我想要实现的目标:在选择字段中,应显示MySQL-Query的结果。在select上成功后,我想用新内容刷新div,也通过外部PHP文件加载。
Problem in the solution mentioned above: The JSON-Array given back by PHP (in my case) looks like this:
上面提到的解决方案中的问题:PHP给出的JSON-Array(在我的例子中)看起来像这样:
[{"value":"1","text":"TEXT1"},{"value":"2","text":"TEXT2"}]
The array used in the fiddle looks like:
小提琴中使用的数组如下:
{'1': 'something', '2': 'another thing'}
So I assumed that the double quotes may be the problem. I did then in PHP:
所以我认为双引号可能是问题所在。我在PHP中做了:
$myarray = str_replace("\"", "'", $myarray);
And easy as one-two-three it worked. Here's the whole thing complete:
它很容易就像一两三个一样有效。这是完整的事情:
Javascript to define the editable select element:
用于定义可编辑选择元素的Javascript:
$('#myselect-id').editable( {
success: function(response, newValue) {
$( "#mydiv-id" ).load("loading-external-results.php");
}
});
PHP
PHP
<?php
$myarray = array();
$query = "SELECT id, field1 FROM mytable";
$result = mysqli_query($dbconnect, $query);
while ($row = mysqli_fetch_array($result))
{
$myarray[] = array('value' => $row['id'], 'text' => $row['field1']);
}
$myarray = json_encode($myarray);
$myarray = str_replace("\"", "'", $myarray);
?>
HTML to generate the select-element:
用于生成select元素的HTML:
<a href="#" id="myselect-id"
data-type="select"
data-name="fieldname-in-table-to-update"
data-pk="id-of-the-affected-mysql-row"
data-value="5"
data-url="post.php"
data-source="'.$myarray.'">Text for Select</a>
<div id="mydiv-id">
Some Content, replaced on success of select element.
</div>
#1
0
So what you're going to want to do is actually create a JSON string which you will put into an anchor
element. Let's see what that looks like.
所以你想要做的实际上是创建一个JSON字符串,你将它放入一个锚元素。让我们看看它是什么样的。
First, you need to push the usernames into an array object it, then json encode it. That'll look like this:
首先,您需要将用户名推送到数组对象中,然后json对其进行编码。这看起来像这样:
$usernames = array();
while(.....){
$usernames[] = $rowa['username'];
}
$usernames = json_encode($usernames);
<a href="#"
id="username" //your reference
data-type="select" //so the x-editable library makes a select list
data-name="username"
data-pk="1"
data-value="5"
data-original-title="Select Username"
data-source="<?php echo $usernames?"> //this is your JSON array of usernames.
">
Username
</a>
And this will produce a select list of usernames.
这将生成一个用户名的选择列表。
Here's your functioning jsFiddle
这是你的功能jsFiddle
#2
0
I also stumbled upon the same problems as Johannes Kiehl. However, after a little investigation I found the following and finally fixed it - somehow. It may not be the most elegant approach, hence ideas for improvement are very welcome.
我也偶然发现了和Johannes Kiehl一样的问题。然而,经过一番调查后,我发现以下内容并最终解决了 - 不知何故。它可能不是最优雅的方法,因此非常欢迎改进的想法。
Here's what I wanted to achieve: In a select field, results from a MySQL-Query shall be displayed. Upon success on the select, I want to refresh a div with the new content, also loaded via an external PHP-file.
这是我想要实现的目标:在选择字段中,应显示MySQL-Query的结果。在select上成功后,我想用新内容刷新div,也通过外部PHP文件加载。
Problem in the solution mentioned above: The JSON-Array given back by PHP (in my case) looks like this:
上面提到的解决方案中的问题:PHP给出的JSON-Array(在我的例子中)看起来像这样:
[{"value":"1","text":"TEXT1"},{"value":"2","text":"TEXT2"}]
The array used in the fiddle looks like:
小提琴中使用的数组如下:
{'1': 'something', '2': 'another thing'}
So I assumed that the double quotes may be the problem. I did then in PHP:
所以我认为双引号可能是问题所在。我在PHP中做了:
$myarray = str_replace("\"", "'", $myarray);
And easy as one-two-three it worked. Here's the whole thing complete:
它很容易就像一两三个一样有效。这是完整的事情:
Javascript to define the editable select element:
用于定义可编辑选择元素的Javascript:
$('#myselect-id').editable( {
success: function(response, newValue) {
$( "#mydiv-id" ).load("loading-external-results.php");
}
});
PHP
PHP
<?php
$myarray = array();
$query = "SELECT id, field1 FROM mytable";
$result = mysqli_query($dbconnect, $query);
while ($row = mysqli_fetch_array($result))
{
$myarray[] = array('value' => $row['id'], 'text' => $row['field1']);
}
$myarray = json_encode($myarray);
$myarray = str_replace("\"", "'", $myarray);
?>
HTML to generate the select-element:
用于生成select元素的HTML:
<a href="#" id="myselect-id"
data-type="select"
data-name="fieldname-in-table-to-update"
data-pk="id-of-the-affected-mysql-row"
data-value="5"
data-url="post.php"
data-source="'.$myarray.'">Text for Select</a>
<div id="mydiv-id">
Some Content, replaced on success of select element.
</div>