我在循环中挣扎并为每个键返回特定格式

I'm struggling with loops and returning a specific format for each key

编辑为包括 sql、所需输出与当前和数据库信息示例

我不确定我是否很了解循环。

我想以特定格式显示查询结果以进行许可证检查。对于每个许可证号,我希望一些信息显示为 header 部分,然后遍历每个检查日期并在其下方显示它们的信息。我 运行 遇到的问题是,由于 属性 的年龄,一些地址有多个许可证。使用我当前的代码,我提取了我想要的数据并正确设置了格式,但是,它从不显示 "new" header 部分以及该地址的下一个许可证号。它确实继续通过最后一行结果循环检查其余的检查日期信息。

使用 table 中的列名,这基本上就是我希望 HTML 显示的方式。许可证信息和街道地址,遍历各种检查日期,如果 属性 有另一个许可证,重新开始并打印该信息,然后遍历这些检查日期。发生的事情是显示第一个许可证信息,但如果 属性 有多个许可证号,则不会显示该许可证信息,但循环中的检查日期信息是:

    //What I want it to look like
    //This info displayed once per ['APKEYFULL']
    123 MAIN Permit: F987654
    Inspection Period: 36 Months
    System Type: Leach
    //This information displayed for each inspection date
    Inspection Date:  01-02-2012
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

    Inspection Date:  01-02-2010
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

    Inspection Date:  01-02-2008
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

    123 MAIN Permit: F456789  //Same address, new permit number
    Inspection Period: 36 Months
    System Type: Leach
    //This information displayed for each inspection date
    Inspection Date:  01-02-2018
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

    Inspection Date:  01-02-2016
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

    Inspection Date:  01-02-2014
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.


    //What it actually looks like

    123 MAIN Permit: F987654
    Inspection Period: 36 Months
    System Type: Leach
    //This information displayed for each inspection date
    Inspection Date:  01-02-2012
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

    Inspection Date:  01-02-2010
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

    Inspection Date:  01-02-2008
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

    Inspection Date:  01-02-2018  //Notice no new permit number but the inspection date values display appropriately
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

    Inspection Date:  01-02-2016
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

    Inspection Date:  01-02-2014
    Inspection Status: Passed
    Code Violation Status:  Complied
    H1234 AOM Designation
    Keep up the good work.

我试过 foreach 但它根据查询返回的总行数循环遍历所有结果,这不是我需要的。我还完成了嵌套 while 循环。这会正确显示我需要的所有内容——它只是再次根据查询中的总行数显示所有内容。我的代码可能效率不高,所以我很抱歉。

每个许可证编号都有一个与之关联的密钥。我想我可以按照(代码和英语的混合)的思路想出一些东西:

    if ($key != $current_permit){
        start at the beginning with the next $key;
    }

但我似乎也想不通。

最后,我的实际代码和示例数据库 table:

+-----------+----------+------------+----------+-----------+---------+----------+----------+-------------+--------+-----------------+-------------+----------+----------------+
| APKEYFULL | STNOFULL | STNAMEFULL | APNOFULL |  HPERPRD  | HSEPTYP | COMPDTTM | STATTYPE | DESCRIPT478 | FAILED |    DESCRIPT     | COMMENTS255 |   LOC    | COMMENTSFAILED |
+-----------+----------+------------+----------+-----------+---------+----------+----------+-------------+--------+-----------------+-------------+----------+----------------+
|     44444 |      123 | MAIN       | F987654  | 36 months | LEACH   | 1/2/2008 | Passed   | Complied    | H1234  | AOM Designation | Keep up     | the good | work.          |
|     44444 |      123 | MAIN       | F987654  | 36 months | LEACH   | 1/2/2010 | Passed   | Complied    | H1234  | AOM Designation | Keep up     | the good | work.          |
|     44444 |      123 | MAIN       | F987654  | 36 months | LEACH   | 1/2/2012 | Passed   | Complied    | H1234  | AOM Designation | Keep up     | the good | work.          |
|     55555 |      123 | MAIN       | F456789  | 36 months | LEACH   | 1/2/2014 | Passed   | Complied    | H1234  | AOM Designation | Keep up     | the good | work.          |
|     55555 |      123 | MAIN       | F456789  | 36 months | LEACH   | 1/2/2016 | Passed   | Complied    | H1234  | AOM Designation | Keep up     | the good | work.          |
|     55555 |      123 | MAIN       | F456789  | 36 months | LEACH   | 1/2/2018 | Passed   | Complied    | H1234  | AOM Designation | Keep up     | the good | work.          |
+-----------+----------+------------+----------+-----------+---------+----------+----------+-------------+--------+-----------------+-------------+----------+----------------+



    $serverName = "localhost";
    $connectionInfo = array( "Database"=>"mydb", "UID"=>"myuser", "PWD"=>"pass", "ReturnDatesAsStrings" => true);
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    if($conn === false) {
        echo "Could not connect.\n";
        die( print_r( sqlsrv_errors(), true));
    }

    $searchstno = $_GET['stnumber'];
    $searchstname = $_GET['stname'];

    $where = "WHERE (LTRIM(STNOFULL) = '$searchstno') AND 
                (STNAMEFULL LIKE '%$searchstname%') AND (WORKTYPE ='SEPBSA' OR  WORKTYPE = 'SEPLN') AND (APBLDGSTAT <> 'V') AND (STAT <> 0) 
                ORDER BY COMPDTTM DESC";
    $query1 ="SELECT  [APKEYFULL]
                      ,[COMMENTS]
                      ,[APNOFULL]
                      ,[APINSPKEY]
                      ,[COMPDTTM]
                      ,[COMMENTSFAILED]
                      ,[FAILED]
                      ,[LOC]
                      ,[LOT]
                      ,[STNOFULL]
                      ,[STNAMEFULL]
                      ,[STSUBFULL]
                      ,[AREA]
                      ,[HSEPTYP]
                      ,[HPERPRD]
                      ,[DESCRIPT]
                      ,[COMMENTS255]
                      ,[WORKTYPE]
                      ,[STAT]
                      ,[WAIVED]
                      ,[APBLDGSTAT]
                      ,[FINDTTM]
                      ,[PARTIAL]
                      ,[DESCRIPT478]
                      ,[HALTECH],
                      CASE WHEN STAT = 0 THEN 'No Action'
                         WHEN STAT = 1 THEN 'Passed'
                         WHEN STAT = 2 THEN 'Failed'
                         WHEN STAT = 3 THEN 'Cancelled'
                         WHEN STAT = 4 THEN 'Hold'
                         WHEN STAT = 5 THEN 'Closed'
                         WHEN STAT = 6 THEN 'Pending(not used)' END AS STATTYPE
                  FROM [my].[dbo].[view]
                                         $where";


    if(isset($_GET['search'])){
        $sql1 = sqlsrv_query($conn, $query1);
        if ($sql1 === false){
            die( print_r( sqlsrv_errors(), true));
        }
        $row1 = sqlsrv_fetch_array($sql1, SQLSRV_FETCH_ASSOC);
            $stno = $row1['STNOFULL'];
            $stname = $row1['STNAMEFULL'];
            $apno = $row1['APNOFULL'];
            $hperprd = $row1['HPERPRD'];
            $hseptyp = $row1['HSEPTYP'];
            echo "<br><h2>".$stno." ".$stname." Permit Number: ".$apno."</h2>";
            echo "<p><strong>Inspection Period:</strong> ".$hperprd." months<br>";
            echo "<strong>System Type:</strong> ".$hseptyp."</p>";
            echo "<hr>";

            $sql2 = sqlsrv_query($conn, $query1);
            if ($sql2 === false){
                die( print_r( sqlsrv_errors(), true));
            }
            while ($row2 = sqlsrv_fetch_array($sql2, SQLSRV_FETCH_ASSOC)){
                if (is_null($row2['COMPDTTM'])){
                    $compdttm = '';
                } else {
                    $compdttm = date("m-d-Y", strtotime($row2['COMPDTTM']));
                }
                echo "<h3>Inspection Date:  ".$compdttm."</h3>";
                echo "<p><strong>Inspection Status:</strong> ".$row2['STATTYPE']."<br>";
                if(is_null($row2['DESCRIPT478'])){
                echo "";
                } else {
                    echo "<strong>Code Violation Status:</strong> ".$row2['DESCRIPT478']."<br>";
                }
                echo "<strong>".$row2['FAILED']." ".$row2['DESCRIPT']."</strong></p>";
                echo "<p>".$row2['COMMENTS255']." ".$row2['LOC']." ".$row2['COMMENTSFAILED']."</p>";
                echo "<hr>";
                }
                sqlsrv_free_stmt($sql2);
                sqlsrv_free_stmt($sql1);
        }

解决此问题的最佳方法是什么? for,foreach,if/else,while等?

好吧,我无缘无故地让这件事变得极其困难。在过去几天搜索 Google 的某个时候,我做了类似的事情,但是,我没有将 $prevapno = $apno 放在正确的位置,所以它从来没有工作过。我创建了一个附加变量 $prevapno,然后添加了一个 if() 语句,如下所示:

    if(isset($_GET['search'])){

            $sql1 = sqlsrv_query($conn, $query1, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
            if ($sql1 === false){
                die( print_r( sqlsrv_errors(), true));
            }
            $prevapno = ""; // created the new variable
            while($row1 = sqlsrv_fetch_array($sql1, SQLSRV_FETCH_ASSOC)){
                $apkey = $row1['APKEYFULL'];
                $stno = $row1['STNOFULL'];
                $stname = $row1['STNAMEFULL'];
                $apno = $row1['APNOFULL'];
                $hperprd = $row1['HPERPRD'];
                $hseptyp = $row1['HSEPTYP'];

                //added this if statement

                if ($apno != $prevapno) {     
                    echo "<br><h2>".$stno." ".$stname." Permit Number: ".$apno."</h2>";
                    echo "<p><strong>Inspection Period:</strong> ".$hperprd." months<br>";
                    echo "<strong>System Type:</strong> ".$hseptyp."</p>";
                    echo "<hr>";
                }
                $prevapno = $apno; //set the new variable to compare here

                if (is_null($row1['COMPDTTM'])){
                    $compdttm = '';
                } else {
                    $compdttm = date("m-d-Y", strtotime($row1['COMPDTTM']));
                }
                echo "<h3>Inspection Date:  ".$compdttm."</h3>";
                echo "<p><strong>Inspection Status:</strong> ".$row1['STATTYPE']."<br>";
                if(is_null($row1['DESCRIPT478'])){
                    echo "";
                } else {
                    echo "<strong>Code Violation Status:</strong> ".$row1['DESCRIPT478']."<br>";
                }
                echo "<strong>".$row1['FAILED']." ".$row1['DESCRIPT']."</strong></p>";
                echo "<p>".$row1['COMMENTS255']." ".$row1['LOC']." ".$row1['COMMENTSFAILED']."</p>";
                Echo "<hr>";

            }
        }