简体   繁体   English

从 pdo 查询创建多行 json 并将其从 php 传递到 jquery function

[英]Create multi-row json from a pdo query and pass it from php to a jquery function

I am creating an Inline Edit data by using X-editable with PHP Dropdown Select Box.我正在使用 X-editable 和 PHP Dropdown Select Box 创建一个内联编辑数据。 I am facing an issue in that I am not able to create a dynamic select option in jquery from database data.我面临的问题是我无法从数据库数据在 jquery 中创建动态 select 选项。

Actually, I want to echo the source format of text in PHP so I can add in jquery. The snippet below is in the correct format, but I don't know how to echo that text dynamically using data from PHP.实际上,我想回显 PHP 中文本的source格式,所以我可以添加 jquery。下面的代码片段格式正确,但我不知道如何使用 PHP 中的数据动态回显该文本。

-- PHP -- 
$stmt = $connect->prepare("SELECT * FROM `profile`");
$stmt->execute();  

-- JQUERY --
$('#employee_data').editable({
  container: 'body',
  selector: 'td.gender',
  title: 'Gender',      dataType: 'json',
  source: [{value: "Male", text: "Male"}, {value: "Female", text: "Female"}]
 });

Please help me to create this json structure [{value: "Male", text: "Male"}, {value: "Female", text: "Female"}] from a server-side query and pass it into the editable() function's argument in jquery.请帮助我从服务器端查询创建这个 json 结构[{value: "Male", text: "Male"}, {value: "Female", text: "Female"}]并将其传递到editable() jquery 中的函数参数。

I tried this, but it didn't work:我试过这个,但没有用:

$result = $stmt->fetchAll();     

foreach($result as $data => $value) {         
  $data = array('value' => $value["category_id"], 'text' => $value["category"]);     
}     

$category_list = json_encode($data);

There is a simple logic error here - you are overwriting the value of $data every time your loop runs, so you'll only ever end up encoding the value of whatever the last entry in your data was.这里有一个简单的逻辑错误 - 每次循环运行时都会覆盖$data的值,因此您最终只会对数据中最后一个条目的值进行编码。

Instead you need to declare an array, and then add items to that array each time you loop:相反,您需要声明一个数组,然后在每次循环时向该数组添加项目:

$result = $stmt->fetchAll();     
$list = array();

foreach($result as $data => $value) {         
  $list[] = array('value' => $value["category_id"], 'text' => $value["category"]);     
}     

$category_list = json_encode($list);

And then in the JavaScript, simply write:然后在 JavaScript 中,简单地写:

source: <?php echo $category_list; ?>

There are several points to inform you of...有几点要通知你...

  1. If you are not using any placeholders in your sql, then using a prepared statement is needless overhead.如果您没有在 sql 中使用任何占位符,那么使用准备好的语句是不必要的开销。 Just use query() .只需使用query()

  2. Do not ask for more data from your SELECT clause than you actually intend to use.不要从 SELECT 子句中索取比您实际打算使用的更多的数据。 I don't know how many columns are in your table, but you only need to be fetching category_id and category according to your posted script.我不知道你的表中有多少列,但你只需要根据你发布的脚本获取category_idcategory

  3. By aliasing your column names in your SELECT clause, you can avoid the extra step of iterating the result set's rows just to adjust the keys.通过在 SELECT 子句中为列名添加别名,您可以避免为了调整键而迭代结果集行的额外步骤。

  4. There seems to be a disconnect in your post between category_id & category versus gender & gender .您的帖子中category_id & categorygender & gender之间似乎存在脱节。 I don't know exactly what data is being used to create these <option> s, but I would like to tell you that there is never any benefit to repeating identical text in the option's text and the same option's value attribute.我不知道究竟是什么数据被用来创建这些<option> s,但我想告诉你,在选项的文本和相同的选项的value属性中重复相同的文本永远不会有任何好处。 In other words, <option value="Male">Male</option> should only ever be written as <option>Male</option> ;换句话说, <option value="Male">Male</option>应该只写成<option>Male</option> the former is simply redundant markup bloat.前者只是多余的标记膨胀。 I will assume that category_id and category are not identical values, so there should be no concern for redundancy.我假设category_idcategory不是相同的值,因此不必担心冗余。

  5. fetchAll() is most appropriately used when you are not iterating the data in the same "layer".当您不在同一“层”中迭代数据时,最适合使用fetchAll() Because the server-side (php) data is being passed to the client-side (js), there is no need to manually iterate the result set to create an array;因为服务器端(php)的数据正在传递给客户端(js),所以不需要手动迭代结果集来创建数组; just let fetchAll() do all of the work.fetchAll()完成所有工作。

Code: (assuming you are querying in the same file that you are printing to screen)代码:(假设您在打印到屏幕的同一个文件中查询)

source = <?php echo json_encode($pdo->query("SELECT `category_id` AS `value`, `category` AS `text` FROM `profile`")->fetchAll()); ?>

There is a bounty of good advice/techniques @ https://phpdelusions.net/pdo_examples/select有很多好的建议/技术@ https://phpdelusions.net/pdo_examples/select

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

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