次のPHPスクリプトは、MySQLデータベースからデータを取り出し、結果をHTMLテーブルに表示しています。私はそのセルの値がその行のデータを作成するために、計算を行っている両方のデータ・タイプ50より大きい場合$percent_complete
値を出力<td>
に<span>
クラスを追加したいPHPを使用してクラスにセルを追加するMySQLデータのHTMLテーブル
<?php
require_once 'config.php';
// create connection
$conn = new mysqli($currentConfig['host'], $currentConfig['user'], $currentConfig['pass'],
$currentConfig['name']);
// check connection
if ($conn->connect_error) {
die('Connection failed: '.$conn->connect_error);
}
$sql = "SELECT Client, EstimateNumber, Status, TotalEstimatedTime, CostToDateRoleTotal, " .
"ROUND((CostToDateRoleTotal/TotalEstimatedTime) * 100) AS PercentComplete " .
"FROM Estimates " .
"WHERE Status != 'Invoiced' AND Status != 'Cancelled' AND TotalEstimatedTime > 0 " .
"AND CostToDateRoleTotal > 0 ORDER BY PercentComplete DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$client = $row['Client'];
$estimate_number = $row['EstimateNumber'];
$status = $row['Status'];
$total_estimated_time = $row['TotalEstimatedTime'];
$role_total = $row['CostToDateRoleTotal'];
$percent_complete = $row['PercentComplete'];
// echo data into HTML table
echo '<tbody>'.'<tr>'.'<td>'.$client.'</td>'.'<td>'.$estimate_number.'</td>'.'<td>'.
$status.'</td>'.'<td>'.$total_estimated_time.'</td>'.'<td>'.$role_total.'</td>'.
'<td>'.$percent_complete.' %'.'</td>'.'</tr>'.'</tbody>';
}
} else {
echo 'No results';
}
$conn->close();
はDECIMALです。
// echo flagged class to span element in table for percent complete
if ($percent_complete > 50) echo $flag = '<span class="flagged">';
else echo '<span>';
そして私はあることをHTMLテーブルのエコー文を編集した:私は、次の操作を実行しようとしたが、これはすべての行に<span class="flagged">
を追加
// echo data into HTML table
echo
'<tbody>'.'<tr>'.'<td>'.$client.'</td>'.'<td>'.$estimate_number.'</td>'.'<td>'.$status.'</td>'.'<td>'.$total_estimated_time.'</td>'.'<td>'.$role_total.'</td>'.'<td>'.$flag.$percent_complete.' %'.'</span>'.'</td>'.'</tr>'.'</tbody>';
。 $percent_complete
のvar_dump()
は私を与える:
string(4) "1838" string(4) "1591" string(3) "592" string(3) "416" string(3) "367" string(3) "346" string(3) "305" string(3) "267" string(3) "266" string(3) "231" string(3) "193" string(3) "169" string(3) "157" string(3) "149" string(3) "142" string(3) "136" string(3) "134" string(3) "127" string(3) "114" string(3) "109" string(3) "106" string(3) "103" string(2) "96" string(2) "88" string(2) "71" string(2) "71" string(2) "70" string(2) "67" string(2) "59" string(2) "58" string(2) "46" string(2) "44" string(2) "38" string(2) "38" string(2) "37" string(2) "34" string(2) "18" string(2) "16" string(1) "9" string(1) "4""
は、これらは私のテーブルの行のすべての文字列値です。前のif文を変更して、これらの文字列値のそれぞれに対して個別にテストするにはどうすればよいですか?ご協力ありがとうございました。
これは完全に働いたはずです。あなたは小さなタイプミスがあります - '$ flat'は' $ flag'でなければなりません。ありがとうございました! – Liz
はい、修正されました。ありがとうございました。 – Sherif