I am working on a PHP application and stumbled upon a problem on auto-highlighting.. I want to ask how to auto-highlight a cell of a table, whose data came from a database, after checking if the value is negative, 0, or positive, Red for negative, yellow for 0 and green for positive.
Please note that I have no experience regarding JavaScript or Ajax and rudimentary knowledge only on CSS. Thank you.
If needed, I can post any part of my code here.
It might be helpful for you:
function getSampleStatus($sampleid){
if($sampleid==1){
$color="#007334";
}elseif($sampleid==2){
$color="#3f96e8";
}elseif($sampleid==3){
$color="#ff9900";
}elseif($sampleid==4){
$color="#ff9770";
}
else{
$color="#ff0000";
}
$query='Select status from config where status_id='.$sampleid;
$this->_db->setQuery( $query );
$status =$this->_db->loadResult();
return "<span style='color:".$color.";padding-left:200px;'>".$status."</span>";
}
This function simply adds different colors depending on sample status.
For example:
For disapproved records, color red.
For Approved records, color green.
For Lab completed records, color yellow and so on..
Edit:
For Simplicity, try this:
$yourdatafromdatabase = 3; //which is either -ve, zero or positive
if($yourdatafromdatabase < 0){
$color="#FF0000"; //red for less than zero
}elseif($yourdatafromdatabase== 0){
$color="#FFFF00"; //yellow for zero
}
else{
$color="#00FF00"; //green for positive
}
echo "<span style=\"color: $color\"> <h1> Wow Color! </h1></span>";
?>
Working Demo:>>
just write css in table tag e.g
if ($value <0)
{echo "<td bgcolor=\"#FF0000\">$value</td>";}
Related
I am having a goal for the user that needs to hit and that user passed the goal I want it to show a different color. So right now I have total goals needed by having goals_needed * time_spent / day_length.
For example if the goal is 10, and user gets 11 points I want it to show a different color because he passed the goal. How would I get the result to show passed goal ?
I was thinking something like this
$color_performace = function($value, $goal) {
if($value < $goal)
return 'notenough';
elseif($value >$goal)
return 'awesome';
else
return 'enough';
};
and function
$total_goals_class = $color_performance($total_goals,round($goals_needed*$goal_multiplier));
It is not clear exactly what you are after but if you want to deal with getting things on the page to change colour you will need to output html & css.
So perhaps have something like:
$color_performace = function($value, $goal) {
if($value < $goal)
return '<p style="color: red;">notenough</p>';
elseif($value >$goal)
return '<p style="color: green;">awesome</p>';
else
return '<p style="color: yellow;">enough</p>';
};
That is a quick and dirty example to show the concept, I would probably change it to use classes and have the css set in a main file somewhere, and use hex codes for colours.
I try show all the hours of the day in order 8, 9, 10 ... but to distinguish the hours that are in the database from the hours that are not. For instance, to give a different color. Can anyone help? (I am not a programmer and I am just learning php alone, so any I would appreciate any help, but explain it in a simple way, please)
This gives me the hours that I have in the database in blue. But I cannot to get hours that are not in the database and I cannot give them another color and the right position: 8, 9,10, 11...
$result = mysqli_query($con, 'SELECT * FROM consulta
WHERE professional=1
AND client=0');
while ($row = mysqli_fetch_array($result)) {
if ($row['hour']=='09:00:00') { echo '<p style="color:blue;">9</p>'; }
elseif ($row['hour']=='10:00:00') { echo '<p style="color:blue;">10</p>'; }
elseif ($row['hour']=='11:00:00') { echo '<p style="color:blue;">11</p>'; }
elseif ($row['hour']=='12:00:00') { echo '<p style="color:blue;">12</p>'; }
}
Here's some sudo code for you. Make an array with all the hours in it. Then iterate that array instead of the result from the database.
for ($i = 0; $i < 24; $i++) {
if(in_array("$i:00:00", $row)) {
// hour was found in the database
echo '<p style="color:blue;">'.$i.'</p>';
} else {
// hour was not found
echo '<p style="color:red;">'.$i.'</p>';
}
}
This way you're iterating over hours that are in the database. Just them. So if 11 is not in the database, if simply won't show. You need a different logic.
You need to iterate over all 24 hours and for each hour check if it is in the database, if so, apply styling, if not, render normally. And a hint, it might be easier to defince CSS classes instead of using inline styling (<p class="db">9</p> instead of <p style="color:blue;">9</p>, and of course, having the db class defined properly; that way you only need to change one rule instead of styling of each element in case you want to change something).
I have bits of code I want to throw in to my site, and provisioned a space right after <body> using 'flairs' (divs) that sit outside the design. Here's the code:
//Add Flair Containers as needed
if($flairs>0){
echo "<!--Flair Graphics (if needed)-->\n";
while($fQty = --$flairs+1){ //-- subracts 1, +1 accounts for 1 being 0
$flair = array($flair1, $flair2, $flair3);
foreach($flair as $flairCode){
echo "<div id=\"flair-".$fQty++."\">".$flairCode."</div>\n";
};
};
};
It prints correctly, where content = $flair1, $flair2, and so on.
<div id="flair-1">Content1</div>
<div id="flair-2">Content2</div>
<div id="flair-3">Content3</div>
But if $flair2/$flair3 is empty, it still prints a div. How can I fix this?
Within your foreach loop you can check if the value is empty and continue (i.e. skip) to the next value if it is.
Like so:
if($flairs>0){
echo "<!--Flair Graphics (if needed)-->\n";
while($fQty = --$flairs+1){ //-- subracts 1, +1 accounts for 1 being 0
$flair = array($flair1, $flair2, $flair3);
foreach($flair as $flairCode){
if (empty($flairCode)) continue;
echo "<div id=\"flair-".$fQty++."\">".$flairCode."</div>\n";
};
};
};
I suspect that you could simply prepend if($flairCode) to your echo statement. That would make your inner loop:
foreach($flair as $flairCode){
if($flairCode) echo "<div id=\"flair-".$fQty++."\">".$flairCode."</div>\n";
};
Some points to note:
Since the $flair array will always be the same, construct it outside of the loop (this will let you evaluate the condition only once too.
Using $fQty++ is not enough to guarantee unique ID's, especially since every time it hits the while the value is reset. I suggest $fQty should not be part of the while condition and simply stay as an independent tally.
Stop using double-quotes. They're slow.
Here is the Aim of my Program.
I Want around 5-6 Circles to be shown on the page, of colors red and green.
When, any of the circle is clicked. Its color changes to BLUE.
Then, if i click any other circle on the page its color should also change to BLUE, and the circle previously clicked should also show as BLUE.
So, if i click all circles one by one, in succesive pageloads, all the circles should show up as blue.
And, when a blue circle is clicked, it should show its original color.
This loop should continue for-ever, until the page is closed.
This is what i have done till now.
define("SIZE", 5); //the no. of circles
class redblock
{
var $color;
function set_color($data)
{
$this->color = $data;
}
function get_color()
{
return $this->color;
}
function image_source()
{
$rval2 = $this->get_color();
echo $rval2;
}
function display_block()
{
$rval = $this->get_color();
echo "<img src = '",$rval,"' width=120> </br> " ;
}
}
for ($i=0 ; $i < SIZE ; $i++ ) //INIT color blocks
{ $rb[$i] = new redblock ; }
for ($i=0 ; $i < SIZE ; $i++ ) //color set
{
if(!isset($_POST[$i.'form']))
{
if ($i % 2 == 0) //even blocks are green
$rb[$i]->set_color("green.jpg");
else //odd blocks are red
$rb[$i]->set_color("red.jpg");
}
if ( $_POST[$i.'form'] == "blue.jpg" )
$rb[$i]->set_color("blue.jpg");
if(isset($_POST[$i.'form']))
$rb[$i]->set_color("blue.jpg");
} ?>
<?php
// BOTH BLOCKS when clicked repeatedly should turn to white and fro.
for ($i=0 ; $i < SIZE ; $i++ ) //display the blocks
{
echo "<form method ='post'>";
echo "<input type = 'image' width='120' src ='",$rb[$i]->image_source(),"'>";
echo "<input type='hidden' name='".$i,"form' value='",$rb[$i]-get_color(),"'>";
echo "</form>";
}
?>
What Happens, in this code.
1. Suppose, i click Circle 1. It turns to blue.
2. Then i click circle 2, it turns blue, but circle 1 becomes green again. I want circle one to retain the blue color.
Please give me the correct code, or guide me in the right direction. Even, a small help will be largely appreciated.
I have no knowledge of Javascript, etc. I only know PHP and HTML/CSS.
Thank you.
You can use either $_SESSION variables or set a cookie using setcookie() and then retrieving it with $_COOKIE on the successive page loads. Either way will get you temporary storage of the values. Sessions are the way to go if you want the values to disappear after the browser is closed. Cookies will persist until they expire or are cleared by the user.
You can use $_SESSION to store each of the circle's state and use it on successive page loads.
For a project I need to work in RadPHP XE2
In this project I created a DBGRid.
This is all working fine.
Now I need to change the cell color for each cell.
The color for the cell is depending on the value inside that cell.
Like this:
if value > 0
return green
else
return red
Well here is the PHP code for what you need. =) Don't really understand your question all that well though.
if( $value > 0 ){
return "red";
}else{
return "green";
}
I think you should add the color changing code in OnBeforeShow event of the dbgrid component.