Change Table Row Background Color When Qty Below 10 Using PHP/MySQL

This tutorial will teach you on how to change table row background color when the qty is bellow 10. I use 10 as a default minimum value of qty, you can change it to any number if you want. This tutorial is helpful for thus creating a system that involves products. This is useful because it provides a legend or a prom the certain product is bellow the minimum qty. The admin can easily identify which product need to be order by the use of this feature. Follow the guidelines bellow on how to this feature.

Creating Our Database

First we are going to create our database which stores our data. To create a database: 1. Open phpmyadmin 2. Then create database and name it as "tutorial". 3. After creating a database name, click the SQL and paste the following code.
  1. CREATE TABLE IF NOT EXISTS `products` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(200) NOT NULL,
  4. `price` int(11) NOT NULL,
  5. `qty` int(11) NOT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

Creating our Database Connection

Next step is to create a database connection and save it as "connect.php". In this Step, we will write our connection script in PDO format.
  1. <?php
  2. /* Database config */
  3. $db_host = 'localhost';
  4. $db_user = 'root';
  5. $db_pass = '';
  6. $db_database = 'tutorial';
  7.  
  8. /* End config */
  9.  
  10. $db = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass);
  11. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12.  
  13. ?>

Creating Our Display

The code bellow will give us the idea on how to change the bgcolor when the qty is bellow 10. I use if else statement to change the bgcolor. Copy the code bellow and save it as "index.php".
  1. <table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4">
  2. <thead>
  3. <tr>
  4. <th> Name </th>
  5. <th> Price </th>
  6. <th> Quantity </th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <?php
  11. include('connect.php');
  12. $result = $db->prepare("SELECT * FROM products");
  13. $result->execute();
  14. for($i=0; $row = $result->fetch(); $i++){
  15. $availableqty=$row['qty'];
  16. if ($availableqty < 10) {
  17. echo '<tr class="record" bgcolor="red" style="color: white;">';
  18. }
  19. else {
  20. echo '<tr class="record">';
  21. }
  22. ?>
  23.  
  24. <td><?php echo $row['name']; ?></td>
  25. <td><?php echo $row['price']; ?></td>
  26. <td><?php echo $row['qty']; ?></td>
  27. </tr>
  28. <?php
  29. }
  30. ?>
  31. </tbody>
  32. </table>
This is the part of the code above that trigger to change the bgcolor to red if the qty is bellow 10.
  1. $availableqty=$row['qty'];
  2. if ($availableqty < 10) {
  3. echo '<tr class="record" bgcolor="red" style="color: white;">';
  4. }
  5. else {
  6. echo '<tr class="record">';
  7. }
Make sure that you follow properly all the instruction above to run this feature. Hope this code will help you in your project.

Add new comment