I have a table with the following columns:
Name,
Quantity,
Price,
Option1 (bool),
Option2 (bool)
that I'm displaying on a simple webpage using php, js, and jquery. Although the script works, I'm trying to find a way to display the non-bool (Name, Quantity, and Price) and true-bool values only. This is the working js script:
$(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 />");
});
});
Since I have almost no js experience, any help will be highly appreciated!
Here's my fetch.php script for your reference:
<?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));
?>
Thanks!
Try this
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 />");
}
});
});