编辑包含sql,所需输出与当前和db info样本
我不确定我是否理解循环。
我希望以特定格式显示查询结果以进行许可检查。对于每个许可证编号,我希望将一些信息显示为标题部分,然后遍历每个检查日期并在其下方显示其信息。我遇到的问题是,由于房产的年龄,一些地址有多个许可证。使用我当前的代码,我可以提取我想要的数据并正确格式化,但是,它从不显示" new"标题部分,包含该地址的下一个许可证号。它会继续在最后一行结果中循环检查其余的检验日期信息。
使用表中的列名,这实际上就是我想要显示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;
}
但我似乎无法解决这个问题。
最后,我的实际代码和示例数据库表:
+-----------+----------+------------+----------+-----------+---------+----------+----------+-------------+--------+-----------------+-------------+----------+----------------+
| 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);
}
最好的方法是什么?对于,foreach,if / else,while等等?
答案 0 :(得分:0)
我无缘无故地把这个变得非常困难。在过去几天搜索谷歌的某些时候我做了类似的事情,但是,我没有在正确的位置$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>";
}
}