sandaru1sx Posted January 6, 2014 Share Posted January 6, 2014 Hi, Somehow I downloaded an example, modified and uploaded to server, also connected to mysql database. Php properly is working here insertareg.php Im new to PHP, just only know very little and earning by changing example codes. Want to do this: Only ( 0,1,2,3) will be inserted into Trays column and cells of the Trays column should change according to the inserting values as this o - red 1 - orange 2 - yellow 3 - green With the color of the cell number also should be displayed in each cell of Trays column. Help modifying the code <html> <head> <title>Data of Sensor</title> </head> <body> <h1>Data from the temperature and moisture sensors</h1> <form action="add.php" method="get"> <TABLE> <tr> <td>P Line</td> <td><input type="text" name="temp1" size="20" maxlength="30"></td> </tr> <tr> <td>Trays</td> <td><input type="text" name="moi1" size="20" maxlength="30"></td> </tr> </TABLE> <input type="submit" name="accion" value="Grabar"> </FORM> <hr> <?php include("conec.php"); $link=Conection(); $result=mysql_query("select * from tempmoi order by id desc",$link); ?> <table border="1" cellspacing="1" cellpadding="1"> <tr> <td> P Line </td> <td> Trays </td> </tr> <?php while($row = mysql_fetch_array($result)) { printf("<tr><td> %s </td><td> %s </td></tr>", $row["temp1"], $row["moi1"]); } mysql_free_result($result); ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/285138-changing-the-table-cell-color-depending-on-input-value/ Share on other sites More sharing options...
TinyI Posted January 6, 2014 Share Posted January 6, 2014 Hey, inside your while loop, you need two switch statements to determine the colour. They will be pretty identical so I'll give you an example for you to change. // get colour from value switch($value) { case 0: // value goes after the case. so where I've put 0. You could even put words (make sure they're quoted). $colour = red; break; // after doing what you need to do in a case, always add a break. There are a few times where you won't, but you generally will. case 1: $colour = blue; break; default: // anything not covered above can be caught in the default $colour = black; break; } Hopefully this helps? If you need me to explain, just ask Quote Link to comment https://forums.phpfreaks.com/topic/285138-changing-the-table-cell-color-depending-on-input-value/#findComment-1464066 Share on other sites More sharing options...
sandaru1sx Posted January 7, 2014 Author Share Posted January 7, 2014 (edited) Thank you very much. Don't have much programming skills. Please kindly explain where to put the switch statement exactly. <?php while($row = mysql_fetch_array($result)) { printf("<tr><td> %s </td><td> %s </td></tr>", $row["temp1"], $row["moi1"]); } mysql_free_result($result); ?> Do I have to edit the HTML tags? Can you explain how to add colors inside HTML tags. Inside <td> tags. Edited January 7, 2014 by sandaru1sx Quote Link to comment https://forums.phpfreaks.com/topic/285138-changing-the-table-cell-color-depending-on-input-value/#findComment-1464206 Share on other sites More sharing options...
GetFreaky Posted January 7, 2014 Share Posted January 7, 2014 (edited) Thank you very much. Can you explain how to add colors inside HTML tags. Inside <td> tags. printf("<tr><td bgcolor="HEX COLOR HERE "> %s </td><td> %s </td></tr>", $row["temp1"], $row["moi1"]); Edited January 7, 2014 by GetFreaky Quote Link to comment https://forums.phpfreaks.com/topic/285138-changing-the-table-cell-color-depending-on-input-value/#findComment-1464208 Share on other sites More sharing options...
Barand Posted January 7, 2014 Share Posted January 7, 2014 Set up a CSS style for each cell colour then set the class to be used in the switch statement <?php $cells = ''; for ($i=0; $i<8; $i++) { switch ($val = $i%4) { case 0: $cls = 'c0'; break; case 1: $cls = 'c1'; break; case 2: $cls = 'c2'; break; case 3: $cls = 'c3'; break; } $cells .= "<tr><td class='$cls'>$val</td></tr>\n"; } ?> <html> <head> <title>Cell colours</title> <style type="text/css"> table { border-collapse: collapse; } td { text-align: center; width: 150px; } td.c0 { background-color: red; color: white; } td.c1 { background-color: orange; color: white; } td.c2 { background-color: yellow; color: black; } td.c3 { background-color: green; color: white; } </style> </head> <body> <table border="1" > <?php echo $cells ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/285138-changing-the-table-cell-color-depending-on-input-value/#findComment-1464219 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.