我正在使用php&amp; Charts.js根据html <select>
生成数据图表我的问题是,即使从html <select>
中没有选择任何内容,页面底部也会显示空白图表而不是显示echo
块中的else {}
语句。
如果从html <select>
<body>
<form method="POST">
<p>Select Sales Name:
<select name="Name">
<option value="">Select A Name...</option>
<option value="Joe">Joe</option>
<option value="Jack">Jack</option>
<option value="Jerry">Jerry</option>
<option value="Jimmy">Jimmy</option></select></p>
<input type="submit" name="submit" value="Show Me">
</form>
</body>
<?php
if (isset($_POST['submit'])) {
$whatsalesprsn = $_POST['Name'];
if ($whatsalesprsn == "Joe") { $region = "East"; }
if ($whatsalesprsn == "Jack") { $region = "West"; }
if ($whatsalesprsn == "Jerry") { $region = "North"; }
if ($whatsalesprsn == "Jimmy") { $region = "South"; }
if (isset($region)) {
$query = "Select * from salesdata where salesregion = '$region'";
$db->setQuery($query);
$rows = $db->loadColumn();
$output = array();
for ($i = 0; $i <= 3; $i++) {
$column = $db->loadColumn($i);
array_push($output, $column);
}
$data = json_encode($output[0]);
} else { echo "PLease select a sales person to display data for."; }
}
?>
<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
</head>
<body>
<div id="container" style="width: 75%;">
<canvas id="canvas"></canvas>
</div><script>
//JavaScript to generate chart
</script>
</body>
</html>
答案 0 :(得分:0)
某处你必须致电new Chart(...)
。如果你这样做而没有传递有效数据,那么Chart.js将创建一个空图表(因为这就是你告诉它要做的)。
如果您没有有效数据(即在<select>
元素中没有选择任何内容),请不要实例化Chart.js。类似的东西:
if (data.length) {
new Chart(...);
}
答案 1 :(得分:0)
您需要使用empty()
功能添加std::vector<float> test_features = {0.377745556,0.009904444,0.063231111,0.009904444,0.003734444,0.002914444,0.008633333,0.000471111,0.009642222,0.05406,0.050163333,7e-05,0.006528889,0.000314444,0.00649,0.043956667,0.016816667,0.001644444,0.016906667,0.00204,0.027342222,0.13864};
int n_features = test_features.size();
x= tf::Tensor(tf::DT_FLOAT, tf::TensorShape({1,n_features}));
auto x_mapped = x.tensor<float, 2>();
for (int i = 0; i< n_features; i++)
{
x_mapped(0, i) = test_features[i];
}
。如果if statement
使用empty()
函数
if statement
变量是否为空,请{
$whatsalesprsn