仅显示具有特定值的动态创建的表行

时间:2013-03-25 12:38:38

标签: php if-statement html-table

首先,我将展示我目前所拥有的内容,以便(希望)明确提出问题。

我有一个SQL查询来从我的数据库中获取数据,如下所示:

$strSQL1        = "SELECT Klant.ID, Klant.Naam, Klant.Email, Klant.Soort, Subscriptions.Klant_ID, Subscriptions.Status, Subscriptions.Datum, Subscriptions.ID
                  FROM NAW.Klant 
                  JOIN NAW.Subscriptions 
                  ON Klant.ID = Subscriptions.Klant_ID,
                  (SELECT MAX(Subscriptions.ID) as ids, Klant_ID 
                  FROM NAW.Subscriptions
                  WHERE Mail_ID ='".$_GET["ID"]."'  
                  GROUP BY Klant_ID) table2
                  WHERE Subscriptions.ID=table2.ids"; 
$objQuery1      = mysql_query($strSQL1);

然后我将数据添加到HTML表格中,如下所示:

<table width="700" border="1">  
<tr> 
   <th width="91"> <div align="center">ID  </div></th>  
   <th width="91"> <div align="center">Naam  </div></th> 
   <th width="91"> <div align="center">Email  </div></th> 
   <th width="91"> <div align="center">Soort  </div></th> 
   <th width="91"> <div align="center">Status  </div></th> 
   <th width="91"> <div align="center">Datum  </div></th> 
</tr> 
<?php for ($i = 0; $objResult1 = mysql_fetch_array($objQuery1); $i++) : ?> 
<tr>  
   <td><div align="center"><?=$objResult1["Klant_ID"];?></div></td>
   <td><div align="center"><?=$objResult1["Naam"];?> </div></td> 
   <td><div align="center"><?=$objResult1["Email"];?> </div></td> 
   <td><div align="center"><?=$objResult1["Soort"];?> </div></td> 
   <td><div align="center"><?=$objResult1["Status"];?> </div></td>
   <td><div align="center"><?=$objResult1["Datum"];?> </div></td>
<?php endfor; ?>
</table>  

该表目前显示此数据:

enter image description here (这些都是由姓名/电子邮件组成的)

我的问题是,是否有办法只显示“状态”为10的行?可能使用'if'语句但我无法弄清楚如何做到这一点。如果有人知道如何做到这一点或推动我朝着正确的方向前进,那就太好了。提前谢谢!

注意: 我知道我不应该使用mysql_ *但是因为我稍后会更改为PDO所以你们不必提及^^

注意#2: 我忘了添加我希望不要更改我的SQL查询,因为它会弄乱我从数据库中获取的数据。 (我知道这有点模糊,但我不知道如何解释它)

4 个答案:

答案 0 :(得分:2)

<?php for ($i = 0; $objResult1 = mysql_fetch_array($objQuery1); $i++) :
    if($objResult1["Status"]!= 10) continue;
?> 

答案 1 :(得分:0)

在WHERE子句中添加您希望强制执行的条件。

"SELECT Klant.ID, Klant.Naam, Klant.Email, Klant.Soort, Subscriptions.Klant_ID, Subscriptions.Status, Subscriptions.Datum, Subscriptions.ID
                  FROM NAW.Klant 
                  JOIN NAW.Subscriptions 
                  ON Klant.ID = Subscriptions.Klant_ID,
                  (SELECT MAX(Subscriptions.ID) as ids, Klant_ID 
                  FROM NAW.Subscriptions
                  WHERE Mail_ID ='".$_GET["ID"]."'  
                  GROUP BY Klant_ID) table2
                  WHERE Subscriptions.ID=table2.ids
                  AND Subscriptions.Status = 10"; 

答案 2 :(得分:0)

尝试在查询的where子句中添加Subscriptions.Status = 10

$strSQL1        = "SELECT Klant.ID, Klant.Naam, Klant.Email, Klant.Soort, Subscriptions.Klant_ID, Subscriptions.Status, Subscriptions.Datum, Subscriptions.ID
                  FROM NAW.Klant 
                  JOIN NAW.Subscriptions 
                  ON Klant.ID = Subscriptions.Klant_ID,
                  (SELECT MAX(Subscriptions.ID) as ids, Klant_ID 
                  FROM NAW.Subscriptions
                  WHERE Mail_ID ='".$_GET["ID"]."'  
                  GROUP BY Klant_ID) table2
                  WHERE Subscriptions.ID=table2.ids AND Subscriptions.Status = 10"; 
$objQuery1      = mysql_query($strSQL1);

答案 3 :(得分:0)

这是你要找的吗?

<?php
for ($i = 0; $objResult1 = mysql_fetch_array($objQuery1); $i++) :
if($objResult1p['Status'] == 10){
?>
<tr>  
   <td><div align="center"><?=$objResult1["Klant_ID"];?></div></td>
   <td><div align="center"><?=$objResult1["Naam"];?> </div></td> 
   <td><div align="center"><?=$objResult1["Email"];?> </div></td> 
   <td><div align="center"><?=$objResult1["Soort"];?> </div></td> 
   <td><div align="center"><?=$objResult1["Status"];?> </div></td>
   <td><div align="center"><?=$objResult1["Datum"];?> </div></td>
</tr>
<?php
}
endfor;
?>