【问题标题】:Retrieve data from PostgreSQL database and display in tables - Display certain data according to checkboxes checked从 PostgreSQL 数据库中检索数据并在表格中显示 - 根据选中的复选框显示某些数据
【发布时间】:2017-02-07 07:31:03
【问题描述】:

我创建了一个 PostgreSQL 数据库(使用 pg-admin),其中包含 10 多个表,其中包含不同地区的砷测试。我需要根据来自 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);

    ?>

现在的问题是如何对表进行多项选择,以便用户可以一次选择多个区域来分析合并数据。

谢谢大家。

【问题讨论】:

    标签: php jquery html postgresql checkbox


    【解决方案1】:

    你可以使用联合:

    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'
    

    【讨论】:

    • 而不是“SELECT 1 WHERE 1=2”,您需要“选择 1 作为您的列,1 作为您的列 2 .... WHERE 1=2”
    • 感谢您的回答,但我不需要 SQL 查询,因为我已经有查询来选择两个区域之间的数据,但我被困在如何在 PHP 中动态实现此查询,以便用户应该能够在同一张表中选择一个或多个区域组合之间的数据!如果您还有任何想法,我将不胜感激。谢谢!
    猜你喜欢
    • 2014-05-20
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    相关资源
    最近更新 更多