javascript - js脚本仅显示来自mysql数据库的真实 bool 值

标签 javascript php jquery mysql json

我有一个包含以下列的表格:

Name, 
Quantity, 
Price, 
Option1 (bool), 
Option2 (bool)

我正在使用 php、js 和 jquery 在一个简单的网页上显示。尽管该脚本有效,但我正在尝试找到一种仅显示非 bool 值(名称、数量和价格)和真 bool 值的方法。这是工作的 js 脚本:

$(document).ready( function() {
 done();
});

function done() {
      setTimeout( function() { 
      updates(); 
      done();
      }, 200);
}

function updates() {
     $.getJSON("fetch.php", function(data) {
         $("ul").empty();
     $.each(data.result, function(){
         $("ul").append("<li>Name: "+this['name']+"</li><li>Quantity: " + this['quantity']+"</li><li>Price: "+this['price']+"</li><li>Option1: "+this['option1']+"</li><li>Option2: "+this['option2']+"</li><br />");
     });  
});

由于我几乎没有 js 经验,任何帮助将不胜感激!

这是我的 fetch.php 脚本供您引用:

<?php

include_once('db.php');

$sql = "SELECT * FROM people";
$res = mysql_query($sql);
$result = array();

while( $row = mysql_fetch_array($res) )

    array_push($result, array(
                        'name' => $row[0]));
                        'quantity' => $row[1],
                        'price'  => $row[2],
                        'option1'  => $row[3],
                        'option2' => $row[4]));


echo json_encode(array("result" => $result));

?>

谢谢!

最佳答案

试试这个

function updates() {
 $.getJSON("fetch.php", function(data) {
     $("ul").empty();
 $.each(data.result, function(){

     $("ul").append("<li>Name: "+this['name']+"</li><li>Quantity: " + this['quantity']+"</li><li>Price: "+this['price']+"</li>";

if (this['option1']) {
    $("ul").append("<li>Option1: "+this['option1']+"</li>");
 }
if (this['option2']) {
     $("ul").append("<li>Option2: "+this['option2']+"</li><br />");
  }     
     });  
   });

关于javascript - js脚本仅显示来自mysql数据库的真实 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38963686/

相关文章:

php - INSERT Row If Does Not Exist mysql with POST 插入行

php - 选择 1 个 pr S/N,其中客户 ID = x 和最新的一个

php - "Subquery returns more than 1 row"没有子查询 MySQL?

jquery - MVC 3 AJAX 和 [ValidateAntiForgeryToken]

javascript - meteor CollectionFS : Make sure the image is uploaded before displaying

javascript - 使用 jQuery 动态替换 <tr> 及其包含的元素

jquery colorbox 图像边框

jquery - 你能加粗 jquery ui datepicker 字段吗?

javascript - CSS JQuery 在容器内旋转图像

javascript - ng-include 不包括 Angular 中的页面