0

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.

3

5 Answers 5

1

You will have to remove the if statement out of the echo to get rid of the error Try this:

<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>
Sign up to request clarification or add additional context in comments.

4 Comments

@user7186749 can you check my answer
Hey man, very nice. I changed the php part that gets my database code. Because my php did not output my database data correctly. Your code is understandable. Although I get my output no inside and outside of my table. Why? Could you please look at my EDIT .
what version of php do you have?
My brother, I fixed it. Thanks for the reply back!
1

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

Comments

0

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)

1 Comment

Could you please look at my EDIT. Your could works very well. I just figured out that the output of my mysql-retrieved-data is not what it should be. it should output as separate integers.
0

This part isn't at the right place:

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

should be:

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

1 Comment

Could you please look at my EDIT. Your could works very well. I just figured out that the output of my mysql-retrieved-data is not what it should be. it should output as separate integers.
-1

Surely status_ would not come back with a number, but an array.

$status_ = mysql_query($status);

Without knowing what data is coming back, it is difficult to help.

mysql_query

5 Comments

it returns integers like 40, 45, 50, 55, 60
I do not understand how it can come back with a singular number, other than 1, to suggest the query worked, or nothing if it failed. I would like to understand how though ?
My thinking was of context. It cannot parse $status_ because it's an array, not a singular number. But I accept I could be wrong, just never seen it deliver anything higher than 1.
@BadAddy you are right! I checked it does not output my retrieved db data as separate integer numbers. Any idea how I can obtain this?
@BadAddy I made an EDIT: where in I displayed the used mysql/php lines of code and the output. I don't know how to solve this, in order to get clear, saperate integers back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.