2016-04-05 4 views
0

レコードを表形式で表示し、フロントエンドにデータを表示するが、データがない場合は、テーブル見出し。私のコードはありますか?データが存在しない場合にテーブルを非表示にする方法(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>"; 
?> 

データベースにデータがなく、フォームを表示する必要がある場合は、これらの合計テーブルを非表示にする必要があります。

答えて

0

アプリケーションでロジックとプレゼンテーションの分離を改善する必要があります。 の前にテーブルを描画し、ifステートメントをテーブル全体に追加します。

これは結果を確認するのに役立ちます:http://php.net/manual/en/pdostatement.rowcount.php

+0

ありがとう – JMR