Changing cell data - php

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.

Related

Compare 2 strings in PHP

I'm in trouble because i can't figured it out how to find my answer.
I'm creating a wordpress website and i have a search engine
I've created an option in my admin panel and code something to write in my option table, but my problem is now
firstly i grab my options from my database with
$mysynonymvalue = get_option( 'synonym-custom' );
I precise that it returne to me something like this ( mango, apple, banana)(this is an example of course)
My Url is something like this :
http://supserwebsite/wordpress/?sfid=2675&_sf_s=toto
Or this
http://superwebsite/wordpress/?sfid=2675&_sf_s=virtualisation cloud devops
so i've created a variable to catch the queries
$motsclefs3= $_GET['_sf_s'];
Now i want to compare the string $mysynonymvalueconvert with $motsclefs3 to find if it match so i write
if (strpos ($mysynonymvalue, $motsclefs3) ){
echo '<script >
$(document).ready(function(){
$(".vc-tabs-li:nth-child(2)").get(0).click();
});
</script>';
}
else{
echo '
<script >
$(document).ready(function(){
$(".vc-tabs-li").get(0).click();
});
</script>';
};
};
The solution seem to work correctly but i can't have the first result, it coompare indeed with all the results but not my first one.
And it doesn't work so fine because with only one letter it return a match ( for example 'a')
Any solution ?
Thanks
*provided url(s) are not accessible.
Solution:
Create a dictionary of option from db and then search the element which you are looking.
So far i move up a bit !So i came with this
I still have
$mysynonymvalue = get_option( 'synonym-custom' );
$mysynonymvalueconvert = preg_split('/[,]+/',$mysynonymvalue);
To grab my words from my database and to convert it into an array.
(the point of this is to get elements that were wrote by an user elsewhere on the admin panel of wordpress)
I also still have
$motsclefs3= $_GET['_sf_s'];
To grab my actual queries ( that will serv me to compare ). I precise that it return me a string. To be more specific, an URL like this (http://mywebsite/wordpress/?sfid=2675&_sf_s=examen) return me (in string) "examen".
Now my point is still to compare if
$motsclefs3;
is inside
$mysynonymvalueconvert
So i've created a "for" loop like this
for ($i = 0; $i <= count($mysynonymvalueconvert); $i++){
if(in_array($motsclefs3, $mysynonymvalueconvert)){
echo'yes';
break;
}
else{
echo 'no';
break;
};
};
But i'm still blocked, this return "yes" only if it match with the first element from
$mysynonymvalueconvert
So any ideas to help me ?
Thanks!

Display image instead of text

I am very new in php. I have a site with the CMS joomla 3.6.
In the register of a third part component I have a select box to the members inform his gender. In the html of the page, returns the text 'male' and 'female'.
What I need is to replace the text 'male' and 'female' with images.
In the PHP file I have this:
public function getFieldData($field) {
$options = array("COM_COMMUNITY_MALE" => "COM_COMMUNITY_MALE", "COM_COMMUNITY_FEMALE" => "COM_COMMUNITY_FEMALE");
$value = strtoupper($field['value']);
if ( isset($options[$value])) {
return JText::_($options[$value]);
}else {
return '';
}
}
So, I know that the second "COM_COMMUNITY_MALE" and "COM_COMMUNITY_FEMALE" are the output html and I have tried to replace them with that code <img src="image/image.png"/>. But no luck, can someone tell me what is the path to do this?
Thank you in advance for your attention.
The problem is that you are using a dropdown box, and, for example, COM_COMMUNITY_MALE is the value and the label of one of the options in that dropdown box, and you can't have a value in a dropdown that is HTML code (for example, an img tag). You will need to review your requirements.

Display div half the time

I'd like to display a div half the time. I thought about something like this :
Generate a number with : $nb = rand(0,1);
Then, for my div, I use a condition if:$nb == 0, it shows, if it is 1, it doesn't.
What do you think ?
Is there a easiest way because it looks a little untidy...
1. You should post such questions on [codereview.SE]
2. You can simplify your code to this:
if (rand(0,1)) {
//Show the div
}
0 will be converted to false, while 1 is being converted to true.
It's fine. If you're lookin for another way to do it use javascript :)
<script type="text/javascript">
if(Math.round(Math.random())==1) {
document.getElementById("myDiv").style.display='none';
} else {
document.getElementById("myDiv").style.display='block';
}
</script>
(gettimeofday()["usec"] % 2 == 1)
could be your randomization. gettimeofday()["usec"] is the current millisecond and we look at it whether it is odd or not.

PHP Auto-highlight cell depending on data from database

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>";}

Display Product Attribute Value only if it exists

Surprisingly can't find anything on this... basically I call my custom product attributes on the product view page like this:
Men / Women: <?php echo $this->htmlEscape($_product->getmenwomen()) ?>
This displays the men/women attribute fine, but these are optional values, so if a product doesn't have a value for this particular attribute, the line is still displayed, just with no value:
Men / Women:
I'd like that line to not show at all if there is indeed, no value for a product. Any ideas?
You have to check if the value of getmenwomen() really contains something you expect it to contain (eg men/women) before printing it. In this example i presume that anything but whitespace is a valid value.
$menwomen = $_product->getmenwomen();
if (trim($menwomen)) {
echo "Men / Women: ".$this->htmlEscape($menwomen);
}
Not 100% on this because I am not sure if getmenwomen() will return false if its empty
If nothing is to be returned, by default it should return false.
<?php
if ($var = $this->htmlEscape($_product->getmenwomen()) {
echo "Men / Women: " + $var;
}
?>
You mean you want to display everything if there is a value and nothing if there isn't? Just add a simple conditional:
if (!empty($this->htmlEscape($_product->getmenwomen())))
echo 'Men / Women: '.$this->htmlEscape($_product->getmenwomen());
and that's it, you don't even need an else in there.

Categories