Tutorial: Conditional formatting

ASP.NET, PHP, Tutorials

This tutorial shows how to use conditional formatting in PHPRunner/ASPRunnerPro projects.

Our first example illustrates the profitability of the product line where we have a list of products with their respective monthly profit figures. All positive numbers are displayed in black and all negative in red. Changing the font color of this field based on its value helps the viewer immediately spot the losing products and take necessary actions.


To set the formatting proceed to Visual Editor and double-click on the field you want to format. In our case it is the field “Profitability”. Select ‘Custom’ View As option. Add your code in the custom code editor. If the value of my current field is greater than zero I will set the font color to black, otherwise to red. Always remember to check the syntax of your code.

PHP

if ($value < 0) {
     $color="black";
} else {
   $color="red";
}
$value="<span style='color: " . $color . "'>" .$value . "</span>";

ASP

if strValue < 0 then
   color="black"
else
   color="red"
end if
strValue="<span style='color: " & color & "'>" & strValue & "</span>"

C#

string color;
if (value > 0) {
color="black"; 
}else { 
color="red"; 
}
value="<span style='color: " + color + "'>" + value.ToString() + "</span>";

You can also represent the positive and negative results with images. For example I have added the red and green circles to highlight the performance of each product. To make this work you will simply need to add two more lines to your custom code. Don’t forget to place the actual images in the project folder.

PHP

if ($value < 0) {
  $value =$value. ' <img src="green.png" alt="" />';
   $color="black";
} else {
   $value ='<strong>'.$value. '</strong> <img src="red.png" alt='' />';
   $color="red";
}
$value="<span style='color: " . $color . "'>" . $value . "</span>";

ASP

if strValue < 0 then
  strValue = strValue & " <img src='green.png' alt='' />"
  color="black"
else
  strValue = "<strong>" & strValue & "</strong> <img src='red.png' alt='' />"
  color="red"
end if
strValue="<span style='color: " & color & "'>" & strValue & "</span>"

C#

string color;
if (value < 0) {
  value = String.Format("{0} <img src='green.png' alt='' />", value); 
  color="black"; 
} else  {
  value = String.Format("<strong>{0} <img src='red.png' alt='' />", value);  
  color="red";  
}
value = String.Format( "<span style='color: {0}'>{1}" , color, value); 

Now, lets say we want to highlight not only the font color itself but also change the background of the cell to yellow.

Because we are no longer working with just the field values, but with the table structure, we will have to use the events to set the background color.

Proceed to the Events screen in the software and select ‘After record processed’ event for the list page of the Products table. The code will be similar to the one we used earlier. If the value of the ‘Profitability’ field is less than zero and will set the background of the cell to yellow. You can refer to the software manual for more examples and code syntax.

PHP

if ($data["Profitability"]<0)
   $record["Profitability_css"].='background:yellow';

ASP

if data("Profitability")<0 then
   record("Profitability_css") = record("Profitability_css") & "background:yellow"
end if

C#

if (data["Profitability"]<0)  
   record["Profitability_css"]+="background:yellow";  

Let’s say you wanted to take it a step further and highlight the entire row of the losing products.

In the same event you can use the same code for the condition check and only change the code for setting the background of the row. You code should look like this. Once again you can find the exact syntax for this example in the software manual.

PHP

if ($data["Profitability"] < 0)
   $row["rowstyle"]='style="background:yellow"';

ASP

if data("Profitability") < 0 then
   row("rowstyle")="style='background:yellow'"
end if

C#

if (data["Profitability"]<0)  
   row["rowstyle"]+="background:yellow";  

And if you want to change the background color of all rows regardless of the condition, you can simply comment out the condition in this event:

PHP

$row["rowstyle"]='style="background:yellow"';

ASP

 row("rowstyle")="style='background:yellow'"

C#

row["rowstyle"]+="background:yellow";  

Watch the video tutorial

Other tutorials: PHPRunner, ASPRunnerPro

12 thoughts on “Tutorial: Conditional formatting

  1. Hi !
    Thanks for the tutorial.

    it works fine when you compare numerical values, but doesn’t seem to work when you compare strings.
    I have this:

    if ($data[“pt42”] > 0)
    {
    $record[“pt42_style”].=’style=”background:olivedrab”‘;
    }
    if ($data[“pt42”] == 0)
    {
    $record[“pt42_style”].=’style=”background:LightSlateGray”‘;
    }
    if ($data[“pt42”] == “R/-197”)
    {
    $record[“pt42_style”].=’style=”background:yellow”‘;
    }
    if ($data[“pt42”] == “S10/258”)
    {
    $record[“pt42_style”].=’style=”background:white”‘;
    }

    The first 2 conditions are working well, but not the others, why ?
    i want the cell background to be yellow if the content = “R/-197” or white if the content is = “S10/258”

  2. ok i found a workaround, it works if you test the STRINGS first and numerics values at the end, like this :

    if ($data[“pt42”] == “R/-197”)
    {
    $record[“pt42_style”].=’style=”background:yellow”‘;
    }
    elseif ($data[“pt42”] == “S10/258”)
    {
    $record[“pt42_style”].=’style=”background:white”‘;
    }

    elseif ($data[“pt42”] > 0)
    {
    $record[“pt42_style”].=’style=”background:olivedrab”‘;
    }
    elseif ($data[“pt42”] == 0)
    {
    $record[“pt42_style”].=’style=”background:LightSlateGray”‘;
    }

  3. Me again !
    There is a mistake in the tutorial
    Replace the hardcoded color #0c0000 with: $color

    in the PHP first example:
    $value=”$value”;

    Should be:
    $value=”$value”;

  4. the text editor removed my messed with my code…

    Well you just have to know that you have to replace #0c0000 with: $color

    in the examples.

  5. I have been using this code and changed for use with Dates,but it changes color for everything. What do I have to do to make this work correctly. Thank you

    if strValue >= now() then
    color=”green”
    else
    color=”red”
    end if
    strValue=”” & strValue & “”

  6. I have Events Management Application. There are two fields named “date_booked” and “location”. I want the system to give an alert if for a particular location more than three events were booked on the same day. Please the code could be in PHP or Javascript.

  7. @Leon,

    in HTML no, in PHP – yes. In other words – yes, you can create conditional output very easy.

  8. hi i just want to ask on how to change the value. here is my code

    if ($data[“DATE”]< Now()){
    $data["FILE_STATUS"]= "FOR STORAGE";
    }

    is this code posible to change the value of the FILE_STATUS to FOR STORAGE, if not can you help me

  9. How can we have 3 conditions

    if ($data[“Profitability”] 1 31)
    $record[“css”].=’background:green’;

  10. Welcome
    I have a “order status” field in the database whose values are not a number but a closed selection list, e.g. (To be accepted, accepted, to be improved) I would like the background of the entire record to be in a specific color depending on the status selected, but as I do with the tutorial, the records are still colorless

    if ($data[“status”] == “Do akceptacji”)
    {
    $record[“status_style”].=’style=”background:red”‘;
    }
    elseif ($data[“status”] == “Zaakceptowano”)
    {
    $record[“status_style”].=’style=”background:blue”‘;
    }
    elseif ($data[“status”] == “Do poprawy”)
    {
    $record[“status_style”].=’style=”background:green”‘;
    }

  11. I have this code but it will not work, the thing is when I put a value, it work but I want to put a field that contain the value because it changed for each product. Can ou help?

    if ($data[“QUANTITY”] < $data["TARGET"])
    $record["css"]="background:yellow;";

    TKS.

Leave a Reply

Your email address will not be published. Required fields are marked *