Saroj Thapa Saroj Thapa - 2 months ago 5
PHP Question

Check the dropdowns and change cell colors dynamically

I have a web table displaying a sql server database table using php pdo. The table is created and displayed on index.php file. I have a form in edit.php where users can update the table. Some of the fields in edit.php have dropdowns. For example, one field has dropdown options Q1,Q2,Q3,and Q4. If the initial value is Q1 or the user updates to Q1, I want to make the background color of the cell in table to be green, if Q2=>yellow, Q3=>red. Can somebody please help me with this? I have index.php and edit.php shown below:

index.php

<?php
require_once('include/database.php');
?>

<!DOCTYPE html>
<html>
<head> </head>
<body>
<tr><th>Q Rating</th></tr>
<tr><th>Action</th></tr>

<?php
$stmt = $conn->prepare("SELECT * FROM MATRIX ORDER BY OBJECTID ASC");
$stmt ->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {

?>

<tr>
<td><?=$row['Q_Rating'];?></td>
<td><a href="edit.php?id=<?=$row['OBJECTID'];?>">Edit </a> </td>


Edit.php

<form action="" method="post">
<tr>
<td>ATFP</td>
<td><label>
<select name="txt_qrating" class="textfields" id="q_rating">
<option id="0">Select One</option>
<option id="1"> Q1</option>
<option id="2">Q2</option>
<option id="3">Q3</option>
<option id="4">Q4</option>

</select>
</label>
</td>
</tr>

<tr>
<td><label>
<input type="hidden" name="txt_id" value="<?= $object_id; ?>">
</label>
</td>
<td><label><input type="submit" name="btn_submit" value="Submit">
</label>
</td>
</tr>

Answer

Simply add some css style with proper classes:

.green { color: green;} .red { color: red;}

.. then add some inline conditionals like:

<table>
<tbody><tr>
<td class="<?php echo ("quatity1" === $myvar ? 'red' : 'green'; ?>">tdValueHere</td>
</tr></tbody></table>
Comments