2016-09-02 38 views
1

以下PHP腳本正在從MySQL數據庫中提取數據並在HTML表中顯示結果。使用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(); 

我想一個<span>類添加到,如果單元格中的數值大於50.正在做的計算以創建行的數據都是十進制兩種數據類型,其輸出$percent_complete<td>。我試圖做到以下幾點:

// echo flagged class to span element in table for percent complete 
    if ($percent_complete > 50) echo $flag = '<span class="flagged">'; 
    else echo '<span>'; 

我編輯的HTML表格的echo語句是:

// 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>'; 

然而,這增加了<span class="flagged">每一行。的$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語句以分別對這些字符串值進行測試?感謝您的幫助。

回答

1

您的邏輯不成立,因爲您定義$flag變量一次,如果$percent_complete > 50,但從來沒有將它定義回空字符串。所以它的值是總是<span class="flagged">此後每次迭代。

此代碼

if ($percent_complete > 50) echo $flag = '<span class="flagged">'; 
else echo '<span>'; 

應該

if ($percent_complete > 50) { 
    $flag = '<span class="flagged">'; 
} else { 
    $flag = '<span>'; 
} 
+0

這完美地工作。你有一個小錯字 - '$ flat'應該是'$ flag'。謝謝! – Liz

+0

是的,已更正。謝謝。 – Sherif

相關問題