如果数据不存在则如何隐藏表并显示其数据(PHP,PDO)

时间:2016-04-05 08:50:22

标签: php pdo

我以表格格式获取记录并在前端显示数据但是如果没有数据我需要隐藏包括表格标题在内的总表格。这是我的代码。

<!DOCTYPE html>
<html>
<body>
<head>
<style>
   table, th {
    border: 1px solid black;
    border-collapse:collapse;
    text-align:center;
   }
   th{
    width: 100px !important;
    background:#E1EBEC !important;
   }
   tr:nth-child(odd) {
    background:#999 !important;

</style>

</head>
<?php

$id = "ACDB-";

$user_ID = get_current_user_id();
$number = str_pad($user_ID, 4, '0', STR_PAD_LEFT);

$ESTCCB_ID=$id.$number;

echo"<b>Show My Application</b>";

echo "<table class="table">";
echo "<tr><th>ApplicationID</th>
<th>Name</th>
<th>Email</th>             
<th>Company</th>
<th>How you want to collaborate with us?</th>
<th>Number boards you need to </th>
<th>Special_requirements</th><th>Details on Collaboration Request</th>  
<th>Status</th>
<th>Application Review Comments</th></tr>";

class TableRows extends RecursiveIteratorIterator {
 function __construct($it) {
     parent::__construct($it, self::LEAVES_ONLY);
 }

 function current() {
     return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
 }

 function beginChildren() {
     echo "<tr>";
 }

 function endChildren() {
     echo "</tr>" . "\n";
 }
}

$servername = "localhost";
$username = "*******";
$password = "********";
$dbname = "**********";

try {
 $conn = new PDO("mysql:host=$servername;dbname=$dbname",$username,         $password);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $stmt = $conn->prepare("SELECT Application_ID,name,email,company,collaborateMS,ARM64_Board,collaboration_Details,special_requirements,Status,Application_Comments FROM wp3_cte where userid='$ESTCCB_ID' ");
 $stmt->execute();

 // set the resulting array to associative
 $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

 foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
     echo $v;
   }

}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?> 

如果数据库中没有数据并且应该显示表单,我需要隐藏这些总表。

1 个答案:

答案 0 :(得分:0)

您需要改进应用程序中逻辑和表示的分离。在绘制表之前调用查询,并在整个表中添加if语句。

这可以帮助您检查结果:http://php.net/manual/en/pdostatement.rowcount.php