2016-09-29 81 views
0

我已经创建了一个的PostgreSQL数据库(有PG管理员)含有多于10表填充有不同区的砷测试。我需要根据HTML表单的用户输入生成html表格。检索从PostgreSQL数据库和显示数据在表 - 显示根据复选框某些数据检查

此外,我已经在我的HTML表单中放入了一些复选框,其中包含要选择的区域名称,并通过检查它们可以限制要选择的区域以及哪个区域的数据。

每个复选框代表区(这是单独的表或模式中的PostgreSQL)我的数据库的名称;所以随着复选框的选择(用户可以选择一个复选框或多个复选框),显示任何被选中的项目。

到目前为止,我设法写了一个连接到数据库的php脚本,当我的复选框没有时显示错误消息。

问题:我知道如何选择个别区域(即区域是单个表格),但不能给用户多种选择。

信息:所有表具有相同的列名称,只有数据因区域名称的更改而不同。

我做了什么: - 我在Stakoverflow参观了许多解决方案,但所有的建议我选择同一个表的字段dynamicaly但不表 我曾参观过: -

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

您可以使用联盟:

SELECT * FROM Arsenic_Test_Banke 
UNION 
SELECT * FROM Arsenic_Test_Bara 

而且你可以用它在一个如果:

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' 
+0

,而不是 “SELECT 1 WHERE 1 = 2”,你需要“选择1为yourcolumn, 1 as yourcolumn2 .... WHERE 1 = 2“ –

+0

感谢您的回答,但我不需要SQL查询,因为我已经有查询选择两区之间的数据,但我坚持如何在PHP中动态实现此查询,以便用户应该能够在同一个表中的一个或多个区的组合之间选择数据! 如果你还有任何想法,那么我将不胜感激。谢谢! –

相关问题