0

I have looked at some examples but don't seen to be able to find what I am looking for.

I need to make the cells in a table change colour if the amount in quantity is less than the threshold.

<?php
$result = mysql_query("SELECT * FROM stock WHERE type='hardware' ORDER BY item ASC");

echo "<table border='1' align='center' width='600'>
<tr>
<th align='left' bgcolor='#00a3e0'><font face='Arial'>Hardware Stock Item</font></th>
<th bgcolor='#00a3e0'><font face='Arial'>Quantity</font></th>
<th bgcolor='#00a3e0'><font face='Arial'>Location</font></th>
</tr>";

while($row1 = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><font face='Arial'>" . $row1['item'] . "</font></td>";

if ($row1['quantity'] => $row1['threshold']) {
echo "<td align='center'><font face='Arial'>" . $row1['quantity'] . "</font></td>";
} else {
echo "<td align='center' bgcolor='red'><font face='Arial'>" . $row1['quantity'] . "</font></td>";
}

echo "<td align='center'><font face='Arial'>" . $row1['location'] . "</font></td>";
echo "</tr>";
}
echo "</table>";
?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
Coops
  • 13
  • 3
  • Did you mean `>=`? – David Aug 21 '17 at 12:44
  • FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Aug 21 '17 at 12:45
  • Also, please do *not* abuse the snippet tools. That is for HTML/CSS/Javascript *only*. – John Conde Aug 21 '17 at 12:45
  • your if has wrong syntax – ArtOsi Aug 21 '17 at 12:45
  • Implement a Counter and if the Value is higher than the Counter, set the Color – Bernhard Aug 21 '17 at 12:46
  • 1
    `=>` is not a valid if operator use `>=` instead – julianstark999 Aug 21 '17 at 12:46
  • if ($row1['quantity'] >= $row1['threshold']) {.... operator is wrong – Mr Alb Aug 21 '17 at 12:49

2 Answers2

0

You did not describe what exact issue you were having, but for one thing, this line :

if ($row1['quantity'] => $row1['threshold']) {

is wrong.

Maybe you made a typo error that went unnoticed, anyway there is a syntax error and the correctly written operator does the opposite of what you expect :

=> isn't a comparison operator, it's an assignation operator when assigning array values, the correct comparison operator is >=.

Besides, >= is the lte operator, in other words it means larger than or equal. Since you apparently intend to change the element's colour when the quantity is lesser than the threshold, you should use the lesser than operator which is < :

if ($row1['quantity'] < $row1['threshold']) {

If however the mistake is in your explanaition and you planned to include equlaty in your condition, then you should go for the lesser than or equal operator, which is <= :

if ($row1['quantity'] <= $row1['threshold']) {
Sarkouille
  • 1,275
  • 9
  • 16
0

My code is working but else part runs 2 times.

If my condition is wrong then it displays Invalid Email & Password single time but it runs multiple time. Why?

    <?php
    require_once('connection.php');
    if (isset($_POST['submit'])) {
        $email = $_POST['email'];
        $pass_wrd = $_POST['pass'];
    
        $qry = "SELECT email,pswd FROM registration ";
        $fetchdata = mysqli_query($con, $qry);
    
        while ($arraydata = mysqli_fetch_array($fetchdata)) {
            if (($email == $arraydata['email']) && ($pass_wrd == $arraydata['pswd'])) {
                echo '<script>alert("Data Submitted")</script>';
            } else {
                echo "not";
            }
        }
    }
    ?>
Dark Knight
  • 6,116
  • 1
  • 15
  • 37