PHP if语句在html中
我是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>
输出正确的数据。 但部分在桌子外面,部分在桌子内。
你将不得不从回声中删除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>
  在另一个语句如echo中,你不能有if语句(或者其他语句)。  如果要根据变量连接不同的字符串,可以使用条件(又名“三元”)运算符。 
               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>"; 
尝试:
$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>";
你不能附加到一个字符串的条件语句,例如首先分配给一个变量(比如我发布的)
链接地址: http://www.djcxy.com/p/69823.html下一篇: How to echo a MySQLi row in escaped PHP code, inside an HTML block
