仅当 PHP 变量具有值时才生成图表
Only Generate Chart If PHP Variable Has Value
我正在使用 php 和 Charts.js 生成基于 html <select>
的数据图表我的问题是即使没有从 html <select>
页面底部显示空白图表,而不是在我的 else {}
块中显示 echo
语句。
如果从 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>
您一定是在某个地方打电话 new Chart(...)
。如果您在没有传递有效数据的情况下这样做,那么 Chart.js 将创建一个空图表(因为这是您告诉它要做的)。
如果您没有获得有效数据(即 <select>
元素中未选择任何内容),请不要实例化 Chart.js。类似于:
if (data.length) {
new Chart(...);
}
您需要添加 if statement
和 empty()
function. SO if if statement
will check $whatsalesprsn
variable if is empty or not by using empty()
功能
if(!empty($whatsalesprsn)){
$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]);
}
我正在使用 php 和 Charts.js 生成基于 html <select>
的数据图表我的问题是即使没有从 html <select>
页面底部显示空白图表,而不是在我的 else {}
块中显示 echo
语句。
如果从 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>
您一定是在某个地方打电话 new Chart(...)
。如果您在没有传递有效数据的情况下这样做,那么 Chart.js 将创建一个空图表(因为这是您告诉它要做的)。
如果您没有获得有效数据(即 <select>
元素中未选择任何内容),请不要实例化 Chart.js。类似于:
if (data.length) {
new Chart(...);
}
您需要添加 if statement
和 empty()
function. SO if if statement
will check $whatsalesprsn
variable if is empty or not by using empty()
功能
if(!empty($whatsalesprsn)){
$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]);
}