If the value is 1 the cell bg is green with the # 1 in the cell
If the value is 0 the cell bg is yellow with a 0 in it.
I would like to display "Yes" instead of "1" and "No" instead of "0"
if($row['play'] == 1){
echo "<td bgcolor='green'>" . $row['play'] . "</td>";
}
else if ($row['play'] == 0){
echo "<td bgcolor='yellow'>" . $row['play'] . "</td>";
}
The values come from a checkbox (1) and a hidden field (0) The MySQL field is BOOL.
Is there an easier/better way to achieve this?
You can do this:
if($row['play'] == 1){
echo "<td bgcolor='green'>Yes</td>";
}
else if ($row['play'] == 0){
echo "<td bgcolor='yellow'>No</td>";
}
but I'd say that switch/case thing is more comfortable:
switch( $row['play'] ) {
case 1:
echo "<td bgcolor='green'>Yes</td>";
break;
default:
echo "<td bgcolor='yellow'>No</td>";
break;
}
You can cast it to an int, and then make a condition if it's above zero:
if((int)$row['play'] > 0) {
echo "<td bgcolor='green'>Yes</td>";
}
else {
echo "<td bgcolor='yellow'>No</td>";
}
This way, play could be 1, 2, 3, 4, 5, ..etc.
something like this will do it -
echo "<td bgcolor='" . $row['play'] == 1 ? "green" : "yellow" . "'> . $row['play'] == 1 ? "No" : "Yes" . "</td>";
You can cut down on an echo statement by doing:
if($row['play'] == 1){
$color = 'green';
$text = 'yes';
}
else
{
$color = 'red';
$text = 'no';
}
// Assign the color and text values based on the input.
echo "<td bgcolor=$color>$data</td>";
$words = array(
0 => "No",
1 => "Yes"
) ;
if($row['play'] == 1){
echo "<td bgcolor='green'>" . $words[(int)$row['play']] . "</td>";
} else if ($row['play'] == 0){
echo "<td bgcolor='yellow'>" . $words[(int)$row['play']] . "</td>";
}
Or even better:
$map = array(
0 => array("word"=>"No", "color"=>"yellow"),
0 => array("word"=>"Yes", "color"=>"green"),
) ;
$current = (int) $row['play'] ;
echo "<td bgcolor='{$map[$current]['color']}'>{$map[$current]['word']}</td>";
Try This
if($row['play'] == 1)
{
echo "<td bgcolor='green'>Yes</td>";
}
else if ($row['play'] == 0)
{
echo "<td bgcolor='yellow'>No</td>";
}
Related
I need to display:
/images/image1.jmp if the number is positive
or
/images/image2.jpg if the number is negative
or
/images/image3 if the number is 0.
$stmt = sqlsrv_query($conn,$sql);
echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
echo "<td>" . $row['']. "</td>";
}
echo "</table>";
?>
I found this code and have tried different ways if incorporating it in the echo "<td>" . $row['']. "</td> but not having any luck.
I get the code but can not manipulate it to fit what it needs to do. I'm sure it's a simple solution. Just frustrated.
switch ($myNumber) {
case 0:
echo "Zero is not a valid value.";
break;
case $myNumber < 0:
echo "Negative numbers are not allowed.";
break;
default:
echo "Great! Ready to make calculations.";
break;
}
Thanks for the help guys. Got the answer that worked.
I think this is a more readable solution.
I use an array to hold the links and use a calculation to see if it is negative or positive.
$img = ["-1" => "/images/image2.jpg",
"0" => "/images/image3.jpg",
"1" => "/images/image1.jpg"];
$number = 0;
Echo ($number == 0 ? $img[$number] : $img[$number/abs($number)]);
https://3v4l.org/JstZl
If the number is positive the calculation will be 15/15 => 1.
If the number is negative -10/10 => -1.
Edit:
$stmt = sqlsrv_query($conn,$sql);
echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
$img = ["-1" => "/images/image2.jpg",
"0" => "/images/image3.jpg",
"1" => "/images/image1.jpg"];
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
Echo '<td><img src="' . ($row['percentchange'] == 0 ? $img[$row['percentchange']] : $img[$row['percentchange']/abs($row['percentchange'])]) . '"></td>';
}
echo "</table>";
You didn't show us where $myNumber comes from so if the variable name is different just modify it in the code below.
echo '<td>/images/image' . ($myNumber == 0 ? '3' : ($myNumber >= 1 ? '1' : '2')) . '.jpg</td>';
Try:
if($myNumber < 0) {
//show /images/image2.jpg
}
if($myNumber == 0) {
//show /images/image3
}
if($myNumber > 0) {
//show /images/image1.jmp
}
In your code:
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
echo "<td>";
if($row['percentchange'] < 0) {
//show /images/image2.jpg
}
if($row['percentchange'] == 0) {
//show /images/image3
}
if($row['percentchange'] > 0) {
//show /images/image1.jmp
}
echo "</td>";
}
This is assuming, based off of your other comments, that $row['percentchange'] is the number in particular that you care about checking against.
I am new to PHP, iam trying to color the cells of column4,column7,column9 based on condition which you can see in the IF block,below is my code what i tried,kindly help me to understand how to achieve this. i am using array because i have got more than 80 columns,in below example iam showing only 10 for explaining.
<?php
$con=mysqli_connect("server","user","password","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM table1");
echo "<table id='table_id' class='mytable'>
<thead>
<tr>
<th class='heading'>Column1</th>
<th class='heading'>Column2</th>
<th class='heading'>Column3</th>
<th class='heading'>Column4</th>
<th class='heading'>Column5</th>
<th class='heading'>Column6</th>
<th class='heading'>Column7</th>
<th class='heading'>Column8</th>
<th class='heading'>Column9</th>
<th class='heading'>Column10</th>
</tr>
</thead>";
echo "<tbody>";
while ($row = mysqli_fetch_array($result))
{
$colorfields = array($row['Column4'],$row['Column7'],$row['Column9']);
if ($colorfields < 195 && $colorfields !='')
$classname = "red";
else if($colorfields >= 195 && $colorfields < 199.99)
$classname = "yellow";
else if($colorfields >= 199.99)
$classname = "green";
echo "<tr>";
echo "<td class='normal_cell'>" . $row['Column1']."</td>";
echo "<td class='normal_cell'>" . $row['Column2']."</td>";
echo "<td class='normal_cell'>" . $row['Column3']."</td>";
echo "<td class=".$classname.">". $row['Column4']."</td>";
echo "<td class='normal_cell'>" . $row['Column5']."</td>";
echo "<td class='normal_cell'>" . $row['Column6']."</td>";
echo "<td class=".$classname.">". $row['Column7']."</td>";
echo "<td class='normal_cell'>" . $row['Column8']."</td>";
echo "<td class=".$classname.">". $row['Column9']."</td>";
echo "<td class='normal_cell'>" . $row['Column10']."</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
?>
I'd write a function that returns the 'red', 'yellow' or 'green', depending the value that is passed in as an argument.
function class_from_val($val) {
$classname = '';
if( $val < 195 && $val !='' ) {
$classname = 'red';
} else if( $val >= 195 && $val < 199.99 ) {
$classname = 'yellow';
} else if( $val >= 199.99 ) {
$classname = 'green';
} else {
// ? val = ""
}
return $classname;
}
Then I'd call the function where I need the classname returned
echo "<td class='normal_cell'>" .$row['Column3']."</td>";
echo "<td class='".class_from_val($row['Column4'])."'>".$row['Column4']."</td>";
echo "<td class='normal_cell'>" .$row['Column5']."</td>";
echo "<td class='normal_cell'>" .$row['Column6']."</td>";
echo "<td class='".class_from_val($row['Column7'])."'>".$row['Column7']."</td>";
Follow these general principles:
indent the code and space it
choose carefully your variable names
divide and rule (use functions)
example:
function getClassName($col, $field) {
if ( !in_array($col, [ 'Column4', 'Column7', 'Column9' ]) || empty($field) )
return 'normal_cell';
if ( $field >= 199.99 )
return 'green';
if ( $field >= 195 )
return 'yellow';
return 'red';
}
$cellFormat = '<td class="%s">%s</td>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
foreach ($row as $col => $field) {
printf($cellFormat, getClassName($col, $field), $field);
}
echo '</tr>';
}
Please be lenient on me as I am yet to learn the intricacies of PHP coding. Still at baby steps.
I am trying to practice my PHP coding skills and started with trying to create a Marks aggregation system wherein school teachers can, visually get to know the ups and downs in student's performance, based on academic scores.
Scenario: Student's Marks are stored in MySQL table and the teacher needs to look at 30(assume) students' records. My thought is to show the marks by 'coloring the individual marks(foreground-only: the color of digits are to stand out for easier reference' based on a coloring system.
That is, for UT1(unit test 1), if a student has scored:
Greater than 81, score should displayed in green color.
Greater than 60 but below 80, score should displayed in orange color.
Simply put
Marks >= 81 and <=100 should be green in color(100 marks is the benchmark)
Marks >=61 and <=80 should be orange in color
Marks >=41 and <=60 should be red in color
My code trials until now have come up to here:
<?php
// color code function
function color_marks($marks) {
$color_code = "";
// echo "Marks: " .$marks;
/* if($marks>=0 && $marks<=40) { return $color_code = "FF0000";}
elseif($marks>=41 && $marks<=60) { return $color_code = "FF4444";}
elseif($marks>=61 && $marks<=80) { return $color_code = "FF8800";}
elseif($marks>=81 && $marks<=100) { return $color_code = "00FF00";}
*/
if ($marks >= 81) {
$color_code = "00FF00";
echo "<br />Returning color:".$color_code;
return $color_code = "00FF00";
}
elseif ($marks >= 61) {
return $color_code = "FF8800";
}
elseif ($marks >= 41) {
return $color_code = "FF4444";
}
else {
return $color_code = "FF0000";
}
echo "<br />Returning color:".$color_code;
//if($marks<=60) { return $color_code = "GREEN";} else return $color_code = "RED";
/* if($marks>=81 && $marks<=100) { return $color_code = "338800";}
else { if($marks>=61 && $marks<=80) { return $color_code = "FF8800";}
else { if($marks>=41 && $marks<=60) { return $color_code = "FF4444";}
else { if($marks>=0 && $marks<=40) { return $color_code = "FF0000";}
} }} */
}
// START DEFAULT DATATABLE
echo "<div class='panel panel-default'>";
echo "<div class='panel-heading'>";
echo "<h3 class='panel-title'>Marks of X<sup>th</sup> A - Section</h3>";
echo "</div>";
echo "<div class=panel-body'>";
echo "<table class='table datatable'>";
echo "<thead>";
echo "<tr>";
echo "<th style='text-align:center;'>Roll No.</th>";
echo "<th>Student Name</th>";
echo "<th style='text-align:center;'>Unit 1</th>";
echo "<th style='text-align:center;'>Unit 2</th>";
echo "<th style='text-align:center;'>Quarterly</th>";
echo "<th style='text-align:center;'>Unit 3</th>";
echo "<th style='text-align:center;'>Half-Yearly</th>";
echo "<th style='text-align:center;'>Unit 4</th>";
echo "<th style='text-align:center;'>Annually</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
// Get data from the table from here
include('../config.inc.php');
$sql_query="SELECT * FROM learn_php.2015_2016_test_marks";
$result = mysqli_query($connecDB, $sql_query);
$color_code = '';
while($row = mysqli_fetch_array($result)) {
echo " ut1_marks - ".color_marks($row['ut1_marks'])." - ".$row['ut1_marks'];
echo " ut2_marks - ".color_marks($row['ut2_marks'])." - ".$row['ut2_marks'];
// lets find what color the marks are
if ($row['ut1_marks'] >0 ) { $color_code = color_marks($row['ut1_marks']); }
if ($row['ut2_marks'] >=0 ) { $color_code = color_marks($row['ut2_marks']); }
/*
if ($row['quarterly_marks'] >=0 ) { $color_code = color_marks($row['quarterly_marks']); }
if ($row['ut3_marks'] >=0 ) { $color_code = color_marks($row['ut3_marks']); }
if ($row['half_yearly_marks'] >=0 ) { $color_code = color_marks($row['half_yearly_marks']); }
if ($row['ut4_marks'] >=0 ) { $color_code = color_marks($row['ut4_marks']); }
if ($row['annual_marks'] >=0 ) { $color_code = color_marks($row['annual_marks']); }
*/
// echo "<font color='#".$color_code."'>". $row['ut1_marks'] . "</font>";
// Ok, got the color codes - lets show the magic in the table
echo "<tr>";
echo "<td style='text-align:center;'>" . $row['student_uid'] . "</td>";
echo "<td>" . $row['student_name'] . "</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['ut1_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['ut2_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['quarterly_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['ut3_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['half_yearly_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['ut4_marks'] . "</font>"."</td>";
echo "<td style='text-align:center;'>" . "<font color='#".$color_code."'>". $row['annual_marks'] . "</font>"."</td>";
echo "</tr>";
}
echo "</table>";
?>
Present scenario: Every field value is taking the color-input from the function. But, the color_code value is turning out be constant.
I've been trying to crack this for the past few days, but unable to do so.
Please, if someone can give me a second thought on what I had done wrong, that would be an enormous leap.
Thanks a ton in advance.
I wanted to change the background color of the table cell for different MySQL data. My situation is when the user input their weight and height, it will calculate their Body Mass Index (BMI) as well as output the BMI categories. Something like this:
Now how do I change the table cell color for BMI categories where "underweight" will be white, "Normal Weight" is yellow and "Overweight" is orange? I've tried the following but doesn't work.
This is what I have in my PHP code:
echo "<table border=\"1\"><tr><th>Name</th> //etc.
if (mysqli_num_rows($result) == 0)
echo "<tr><td colspan='2'>No records found.</td></tr>";
else {
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>" . $row['Name'] . "</td>";
//some more codes for weight, height, BMI
echo "<td class='<?php $tdClass; ?>'>" . $row['Health_Measure'] . "</td>";
}
}
echo "</table>";
if ($row['Health_Measure'] == "Underweight")
$tdClass = 'underweight';
else if ($row['Health_Measure'] == "Normal Weight")
$tdClass = 'normalweight';
else if ($row['Health_Measure'] == "Overweight")
$tdClass = 'overweight';
CSS:
.underweight {
background-color:white;
}
.normalweight {
background-color:yellow;
}
.overweight {
background-color:orange;
}
Your code to assign the $tdClass variable is after the loop that uses the $tdClass variable, so the td tags won't have the right classes. Change it to this...
echo "<table border=\"1\"><tr><th>Name</th> //etc.
if (mysqli_num_rows($result) == 0)
echo "<tr><td colspan='2'>No records found.</td></tr>";
else {
while ($row = mysqli_fetch_assoc($result))
{
if ($row['Health_Measure'] == "Underweight")
$tdClass = 'underweight';
else if ($row['Health_Measure'] == "Normal Weight")
$tdClass = 'normalweight';
else if ($row['Health_Measure'] == "Overweight")
$tdClass = 'overweight';
echo "<tr><td>" . $row['Name'] . "</td>";
//some more codes for weight, height, BMI
echo "<td class='<?php $tdClass; ?>'>" . $row['Health_Measure'] . "</td></tr>";
}
}
echo "</table>";
You just need to move your code to determine the CSS class for the TD inside the loop where you output the table rows.
I'm trying to have a "1" printed if there is a value in cDeviceRegistrationId column in the database. Here is the code:
$result is an SQL query
while($row = mysql_fetch_assoc($result))
{
if ($row['cDeviceRegistrationId'] > 0) {
$a = 1;
}
echo "<tr class='forum'>";
echo "<td class='forum'>" . $row['intUserID'] . "</td>";
echo "<td class='forum'>" . $row['cUsername'] . "</td>";
echo "<td class='forum'>" . $row['cEmail'] . "</td>";
echo "<td class='forum'>$a</td>";
echo "<td class='forum'>" . $row['uCreateDate'] . "</td>";
echo "</tr>";
}
The value of $a is not overwritten if it does not meet the condition, meaning other iterations may get the value of 1 incorrectly. Here is a fix (replace your if statement):
$a = ($row['cDeviceRegistrationId'] > 0) ? 1 : '';
How about:
if( !is_null($row['cDeviceRegistrationId']) ){
$a = 1;
}