PHP if statement in html

I am new to HTML5 and PHP, I am trying to output a specific value in table data, If the database-retrieved-value is per condition.

My code:

<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> 

Error output

Parse error: syntax error, unexpected T_IF in /test/composition/login/portal/portal.php on line 204

What is the right way to solve this?

EDIT

my current code:

<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>

Output the right data. but partly outside and partly inside the 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>

You can't have an if statement (or any other statement, for that matter) in the middle of another statement like echo . If you want to concatenate different strings depending on a variable, you can use the conditional (AKA "ternary") operator.

               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>"; 

Try:

$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>";

You can't append to a string a conditional statement, assign to a variable first for example (like I posted)

链接地址: http://www.djcxy.com/p/69824.html

上一篇: 如果块内部的回声陈述?

下一篇: PHP if语句在html中