Texan78 Posted May 7, 2013 Share Posted May 7, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/ Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428815 Share on other sites More sharing options...
Texan78 Posted May 7, 2013 Author Share Posted May 7, 2013 (edited) 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 May 7, 2013 by Texan78 Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428824 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428826 Share on other sites More sharing options...
Texan78 Posted May 7, 2013 Author Share Posted May 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428832 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428833 Share on other sites More sharing options...
Texan78 Posted May 7, 2013 Author Share Posted May 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428853 Share on other sites More sharing options...
Solution DarkKnight2011 Posted May 7, 2013 Solution Share Posted May 7, 2013 (edited) 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 May 7, 2013 by DarkKnight2011 Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428858 Share on other sites More sharing options...
Texan78 Posted May 7, 2013 Author Share Posted May 7, 2013 BINGO!!! That works perfectly! I didn't even think of doing that. I was trying to make it harder then it was suppose to be! Thank you so much!!! Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428871 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 Your welcome mate, good luck with the project Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428874 Share on other sites More sharing options...
Texan78 Posted May 7, 2013 Author Share Posted May 7, 2013 Yeah it's almost done now. Just a couple of hopefully small tweaks. Here is what it looks like. I added some CSS shadow to the severity titles since some of the colors were light. It looks absolutely perfect in Coda. I wished it looked that good in Firefox. http://www.mesquiteweather.net/wxsvr.php -Thanks Again! Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428876 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 nice mate, well done Quote Link to comment https://forums.phpfreaks.com/topic/277743-assigning-colors-from-xml-variable-in-an-array/#findComment-1428881 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.