簡體   English   中英

html-table中的PHP if語句輸出錯誤

[英]PHP if statement in html-table outputs error

我是HTML5和PHP的新手,我試圖在表數據中輸出特定的值,如果數據庫檢索的值是按條件的。

我的代碼:

<table class="scroll">
    <thead style="background-color: #99E1D9; color: #705D56;">
        <tr>
            <th>ID</th>
            <th>Name Client</th>
            <th>Last Update</th>
            <th style="padding-left: 30%;">Status</th>
        </tr>
    </thead>
        <tbody id="hoverTable">
                 <?php

                    $connection = mysql_connect('localhost', 'root', ''); 

                    mysql_select_db('patientdb');

                    $query = "SELECT id, name, date FROM clients";

                    $result = mysql_query($query);

                    //status waarden uit
                    $status = "SELECT status FROM clients";
                    $status_ = mysql_query($status);

                    while($row = mysql_fetch_array($result)){   //Loop through results
                    echo "<tr> 

                            <td>" . $row['id'] . "</td> 
                            <td>" . $row['name'] . "</td> 
                            <td>" . $row['date'] . "</td>
                            <td style='padding-left: 30%;'>" . 

                                if ($status_ > 60){echo "red";
                                } elseif ($status_ > 50){echo "yellow";
                                } else{echo "green";}

                                . "</td>

                         </tr>"; 
                    }
                    mysql_close(); 
                ?>
</tbody>
</table> 

錯誤輸出

解析錯誤:語法錯誤,第204行的/test/composition/login/portal/portal.php中出現意外的T_IF

解決這個問題的正確方法是什么?

編輯

我當前的代碼:

<table class="scroll">
    <thead style="background-color: #99E1D9; color: #705D56;">
        <tr>
            <th>Naam Client</th>
            <th>Laatste Update</th>
            <th style="margin-left: 40%; padding-left: 0%;">Status</th>
        </tr>
    </thead>
    <tbody id="hoverTable" style="font-size: 11pt;">

<?php


    $connection = mysql_connect('localhost', 'root', ''); 
     mysql_select_db('patientdb');

    $query = "SELECT id, naam, datum FROM clients";
    $result = mysql_query($query);

    $query2 = "SELECT status FROM clients";
    $result2 = mysql_query($query2);

    if (!empty ($result2)) {
    while ($row2 = mysql_fetch_assoc($result2)) {
    echo $row2['status'] . "<br />";
    }
    }

    while($row = mysql_fetch_array($result)){   //Loop through results
    echo "<tr> 

            <td>" . $row['id'] . "</td> 
            <td>" . $row['naam'] . "</td> 
            <td>" . $row['datum'] . "</td>
            <td style='padding-left: 30%;'>";

                if ($results2 > 60 && $results2 < 70) {
                    echo "red";
                } elseif ($results2 > 50 && $results2 < 60) { 
                    echo "yellow";
                } else { 
                    echo "green";
                }

                echo "</td>

         </tr>"; 
    }
    mysql_close(); 
?>

    </tbody>
</table>

輸出正確的數據。 但部分在桌子外面,部分在桌子里面。

您不能在其他語句(例如echo )中間使用if語句(或其他任何語句)。 如果要根據變量連接不同的字符串,可以使用條件(AKA“三元”)運算符。

               echo "<tr> 

                        <td>" . $row['id'] . "</td> 
                        <td>" . $row['name'] . "</td> 
                        <td>" . $row['date'] . "</td>
                        <td style='padding-left: 30%;'>" . 
                            $status_ > 60 ? "red" : ($status_ > 50 ? "yellow" : "green" )
                            . "</td>

                     </tr>"; 

您將必須從回顯中刪除if語句以擺脫錯誤,請嘗試以下操作:

<table class="scroll">
    <thead style="background-color: #99E1D9; color: #705D56;">
        <tr>
            <th>ID</th>
            <th>Name Client</th>
            <th>Last Update</th>
            <th style="padding-left: 30%;">Status</th>
        </tr>
    </thead>
        <tbody id="hoverTable">
                 <?php

                    $connection = mysql_connect('localhost', 'root', ''); 

                    mysql_select_db('patientdb');

                    $query = "SELECT id, name, date FROM clients";

                    $result = mysql_query($query);

                    //status waarden uit
                    $status = "SELECT status FROM clients";
                    $status_ = mysql_query($status);

                    while($row = mysql_fetch_array($result)){   //Loop through results
                    echo "<tr> 

                            <td>" . $row['id'] . "</td> 
                            <td>" . $row['name'] . "</td> 
                            <td>" . $row['date'] . "</td>
                            <td style='padding-left: 30%;'>";

                                if ($status_ > 60) {
                                    echo "red";
                                } elseif ($status_ > 50) { 
                                    echo "yellow";
                                } else { 
                                    echo "green";
                                }

                                echo "</td>

                         </tr>"; 
                    }
                    mysql_close(); 
                ?>
</tbody>
</table>

嘗試:

$status = "green";
if ($status > 50)
{
    $status="yellow";
} 
elseif($status>60)
{
    $status="red";
}
echo "<tr> 
<td>" . $row['id'] . "</td> 
<td>" . $row['name'] . "</td> 
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .$status. "</td>
</tr>";

您不能在字符串后附加條件語句,例如先分配給變量(例如我發布的內容)

這部分不在正確的位置:

if ($status_ > 60){echo "red";
                            } elseif ($status_ > 50){echo "yellow";
                            } else{echo "green";}

應該:

echo "<tr>
        <td>" . $row['id'] . "</td> 
        <td>" . $row['name'] . "</td> 
        <td>" . $row['date'] . "</td>
        <td style='padding-left: 30%;'>";
if ($status_ > 60){
   echo "red";
} elseif ($status_ > 50){
   echo "yellow";
} else{
   echo "green";
}
echo "</td></tr>";

當然status_不會返回一個數字,而是一個數組。

$status_ = mysql_query($status);

不知道返回什么數據,很難提供幫助。

mysql_query

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM