我创建了一个 PostgreSQL 数据库(带有pg-admin),其中包含多个 10个表,其中填充了不同区域的Arsenic测试。我需要根据html表单的用户输入生成html表。
此外,我在我的html表单下面放了一些复选框,其中包含要选择的区域名称,通过检查它们,您可以限制选择哪个区域的数据以及不选择哪个区域。
每个复选框代表我的数据库中的区(PostgreSQL中的单个表或模式)的名称;因此,只要选中复选框(用户可以选择一个复选框或多个复选框),就会显示所选的任何内容。
到目前为止,我设法编写了一个连接数据库的php脚本,在没有复选框时显示错误消息。
问题:我知道如何选择个别区域(即这里的区域是个人桌子)但不能为用户提供多种选择。
信息:所有表都具有相同的列名,只有数据因地区名称的更改而有所不同。
我做了什么: - 我已经访问过Stakoverflow中的许多解决方案但是所有建议我选择动态相同表的字段而不是表 我去过: -
http://stackoverflow.com/questions/25527975/how-to-use-checkboxes-to-retrieve-specific-data-in-a-database
这是一个简单的html表单,仅供两个区检查。
<html>
<head>
<title>Jasper Report</title>
</head>
<body>
<h1 align="center">ARSCENIC TEST REPORT</h1>
<b>Districts: </b> <br>
<form method="post" action="app.php">
<input type="checkbox" name="reg1" value="Banke" checked /> Banke<br>
<input type="checkbox" name="reg2" value="Bara" /> Bara <br><br>
<input type="submit" value="Generate"/>
</form>
</body>
</html>
用于选择单个分区表的PHP代码是
<?php
require_once __DIR__ . "/vendor/autoload.php";
use Jaspersoft\Client\Client;
$c = new Client(
"localhost",
"8080",
"jasperadmin",
"jasperadmin",
"/jasperserver"
);
//$dist = $_GET['dist'];
//echo $dist;
$db = pg_connect('host=localhost port=5433 dbname=BankeDB user=postgres password=admin');
$userclass = array('0-5','6-10','11-15', '>15','Total');
$btotal = array();
$query = "select
sum(case when ".'"district_n"'." ='Banke' AND ".'"vdc_name"'."='Belahari' AND ".'"NO_OF_USERS"'." <= '5' AND ".'"conc_arsc"'." <= '10' then 1 else 0 end),
sum(case when ".'"district_n"'."='Banke' AND ".'"vdc_name"'."='Belahari' AND ".'"NO_OF_USERS"'." >= '6' AND ".'"NO_OF_USERS"'." <= '10' AND ".'"conc_arsc"'." <= '10' then 1 else 0 end),
sum(case when ".'"district_n"'."='Banke' AND ".'"vdc_name"'."='Belahari' AND ".'"NO_OF_USERS"'." >= '11' AND ".'"NO_OF_USERS"'." <= '15' AND ".'"conc_arsc"'." <= '10' then 1 else 0 end),
sum(case when ".'"district_n"'."='Banke' AND ".'"vdc_name"'."='Belahari' AND ".'"NO_OF_USERS"'." > '15' AND ".'"conc_arsc"'." <= '10' then 1 else 0 end),
sum(case when ".'"district_n"'."='Banke' AND ".'"vdc_name"'."='Belahari' AND (".'"NO_OF_USERS"'."<='5' or (".'"NO_OF_USERS"'." >='6' and ".'"NO_OF_USERS"'." <='10') or (".'"NO_OF_USERS"'." >='11' and ".'"NO_OF_USERS"'." <='15') or ".'"NO_OF_USERS"'." >'15') AND ".'"conc_arsc"'." <= '10' then 1 else 0 end)
from public.".'"Arsenic_Test_Banke"'."";
//echo $query;
$btresult = pg_query($db, $query);
while($btresults = pg_fetch_row($btresult)){
$count = count($btresults);
$y = 0;
while ($y < $count)
{
$c_row = current($btresults);
$btotal[] = $c_row;
next($btresults);
$y = $y + 1;
}
}
?>
/*
Other related queries here as above
*/
<table width="600" height="300" cellspacing="2" border="1" align="center">
<tr>
<th colspan=13>Tubewells by Arsenic Concentration Levels</th>
</tr>
<tr>
<th>User Class (No of Users)</th>
<th>No.(0-10)</th>
<th>%(0-10)</th>
<th>No.(11-50)</th>
<th>%(11-50)</th>
<th>No.(51-100)</th>
<th>%(51-100)</th>
<th>No.(101-300)</th>
<th>%(101-300)</th>
<th>No.(>300)</th>
<th>%(>300)</th>
<th>Total</th>
<th>%</th>
</tr>
<tr>
<?php
for($i=0; $i<5; $i++){
?>
<tr>
<td><?php echo $userclass[$i];?></td>
<td><?php echo $btotal[$i];?></td>
<td><?php echo $perb10[$i];?></td>
<td><?php echo $bettotal[$i];?></td>
<td><?php echo $pbet[$i];?></td>
<td><?php echo $b51_100total[$i];?></td>
<td><?php echo $pb51_100[$i];?></td>
<td><?php echo $bt101_300total[$i];?></td>
<td><?php echo $pb101_300[$i];?></td>
<td><?php echo $abov300total[$i];?></td>
<td><?php echo $pabov300[$i];?></td>
<td><?php echo $total[$i];?></td>
<td><?php echo $ptotal[$i];?></td>
</tr>
<?php
}
?>
</table>
<?
pg_close($db);
?>
现在问题是如何对表进行多项选择,以便用户可以一次选择多个分区来分析合并的数据。
谢谢大家。答案 0 :(得分:0)
您可以使用Union:
SELECT * FROM Arsenic_Test_Banke
UNION
SELECT * FROM Arsenic_Test_Bara
你可以在If:
中使用它val = SELECT 1 WHERE 1=2
if banke is checked
val = val + union
SELECT * FROM Arsenic_Test_Banke
if bara is checked
val = val + 'union
SELECT * FROM Arsenic_Test_Bara'