Jump to content

Assigning colors from XML variable in an array


Texan78
Go to solution Solved by DarkKnight2011,

Recommended Posts

Forgive me if the topic isn't correct. I will try to explain this the best I can as I am not sure how to explain this in the proper terms. I have an XML file which I have parsed. The variable I have created from the XML element to output is $severity. It works great and outputs a value in my table. The value could be 4 different things. Extreme, Severe, Moderate, Minor.  I want to be able to assign a certain color to each one of the 4 values for when it's displayed. I have another script which I didn't write which does this. Here is an example of what it looks like.

 

Keep in mind $severity is the variable from the XML file. I added that for example purposes but that is the variable that will return these values below.

                if(!empty($severity) and $severity == 'Extreme') {
			$intensity = '<span style="color: red"><b>  '.$intensity.'  </b></span>';}       //    change intensity color
		if(!empty($severity) and $severity == 'Severe') {
			$intensity = '<span style="color: #F66"><b>  '.$intensity.'  </b></span>';}      //    change intensity color
		if(!empty($severity) and $severity == 'Moderate') {
			$intensity = '<span style="color: #FF9"><b>  '.$intensity.' </b> </span>';}      //    change intensity color
                if(!empty($severity) and $severity == 'Minor') {
			$severity = '<span style="color: #FF6600"><b>  '.$intensity.' </b> </span>';}    //    change intensity color

I am sure this is incomplete but you get the idea. I am sure I need to create an array but I am not sure how to properly write this so it assigns a set color to each value I include it in my HTML for output.

 

If I need to provide any additional information please let me know. Once again I apologize for the vague description. I hope this makes sense.

 

-Thanks

Link to comment
Share on other sites

Hi,

 

I think i am understanding this correctly,

 

i would personally create 4 CSS styles such as

 

severity-extreme {
    color: red;
    font-weight: bold;
}

severity-severe {
    color: amber;
    font-weight: bold;
}

Then in your code you could just do this...

if (!empty($severity) {
   echo "<span class='severity-$severity'>$intensity</span>"; 
}

Basically i've just improved what you already shown, I didnt really see a question to the issue your having, Hope this helped

Link to comment
Share on other sites

Thank you for your reply. I know it's a little hard to understand. I agree with you on the CSS part and on my site I will do that as I prefer it that way as well. Unfortunately since this script will be shared with others. Having the inline styles makes the setup a little more easier and straight forward for others to setup on their sites.

 

What I am needing is to take the values of $severity which are generated from the XML file I parsed which is a total of four values. Loop them through an array and assign each value a color. Then produce a new variable based on that array that I could using as the global variable for the output.

 

In your example I don't see how the color is assigned based on value.

 

I hope that's a little better explanation. If you need some more information just let me know.

 

-Thanks

Edited by Texan78
Link to comment
Share on other sites

sorry i dont understand what your trying to do, could you maybe give some example code even if it is wrong and i'll see if i can fix it? will help to understand the issue.

 

things i need to know...

  • what values would you expect to receive
  • what logic do you want to apply to those values
  • what is the output

Thanks, sorry i just dont get it lol

Link to comment
Share on other sites

No problem, I have some sample code in my original post. That is from a working script I has that takes the values from a variable and assigns dynamic colors to the text.

 

The values from the XML file with the variable $severity could be 4 possible outcomes Extreme, Severe, Moderate, Minor.

 

So basically it will take the values from the XML file with the variable $severity and if it's Extreme it will assign it as Extreme then Moderate etc and that is how it will show outputted to the page.

 

Here is an example of what it will look like

 

http://www.mesquiteweather.net/wxsvr.php

 

That is actually the page it will be used on. Right now the color is being assigned inline but I need it to be assigned dynamically based on the vaule in an array but I am not sure how to put that together. I have just started using PHP with XML so it's a little different then a normal array but I know it can be done as I have a script that does that same thing for something else. I am just not sure how it is constructed. The code from my first post is the code used and the general idea.

Link to comment
Share on other sites

so you need to know how to get the $severity variable assigned from the code in the XML file itself?

 

you would need to provide a sample from the xml file or a sample of the array

 

 

No, the variable is already assigned and outputting from XML. That works fine. I just need to assign colors to the values from the $severity variable that is produced from the XML. That way when it is outputted in my HTML each value has an assigned color.

Link to comment
Share on other sites

  • Solution

sorry I'm really missing something here I think, maybe this helps...

$color = '';

switch($severity)
{
    case 'Extreme':
        $color = 'red';
        break;
    case 'Severe':
        $color = 'amber';
        break;
    default:
        $color = 'white';
}

echo "<span style='color: $color;'>$severity</span>";

Does that help?

Edited by DarkKnight2011
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.