我有一个包含以下列的表格:
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/