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.

MSD
  • 171
  • 3
  • 15
  • 3
    Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Rajdeep Paul Dec 21 '16 at 19:36
  • http://stackoverflow.com/questions/10860371/how-to-fix-syntax-error-unexpected-t-if-error-in-php – Md. Abutaleb Dec 21 '16 at 19:36
  • 1
    **WARNING**: If you're just learning PHP, please, do not learn the obsolete [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It's awful and has been removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) helps explain best practices. Make **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will end up with severe [SQL injection bugs](http://bobby-tables.com/). – tadman Dec 21 '16 at 21:28

5 Answers5

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>"; 
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I have tried your way but still have some issues can you please see my [question](https://stackoverflow.com/questions/63148838/how-to-properly-view-table-data-using-if-statement-in-yii2)? – Moeez Jul 29 '20 at 10:24
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>
  • @user7186749 can you check my answer –  Dec 22 '16 at 04:50
  • 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 . – MSD Dec 22 '16 at 20:57
  • what version of php do you have? –  Dec 22 '16 at 22:48
  • My brother, I fixed it. Thanks for the reply back! – MSD Dec 22 '16 at 23:55
  • I have tried your way but still have some issues can you please see my [question](https://stackoverflow.com/questions/63148838/how-to-properly-view-table-data-using-if-statement-in-yii2)? – Moeez Jul 29 '20 at 10:20
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)

ka_lin
  • 9,329
  • 6
  • 35
  • 56
  • 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. – MSD Dec 21 '16 at 20:46
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>";
Mik
  • 71
  • 6
  • 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. – MSD Dec 21 '16 at 20:46
-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

BadAddy
  • 346
  • 1
  • 4
  • 12
  • it returns integers like 40, 45, 50, 55, 60 – MSD Dec 21 '16 at 19:55
  • 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 ? – BadAddy Dec 21 '16 at 19:59
  • 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 Dec 21 '16 at 20:11
  • @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? – MSD Dec 21 '16 at 20:27
  • @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. – MSD Dec 21 '16 at 20:32