Tmod Posted January 20, 2017 Share Posted January 20, 2017 Hi, I am stuck here and would appreciate another set of eyes to look at this code and tell me why it isn't working. Basically what I am trying to do is change the background color based off the value returned, For example let's say Honda was returned I would want the background to be red, whereas with Kawasaki I would want a green background. I have tried various codes from the web and couldn't get them to accomplish what I was looking for. Anyway here is the code. <?php include('openL5_list_df_colors_include.php'); ?> <div class="lvl1-4-5"><?php echo $row_rsStockDynoFk['vs_brand']; ?></div> <div class="lvl1-4-6"><?php echo $row_rsStockDynoFk['vs_yrmodsize']; ?> Code from 'openL5_list_df_colors_include.php' <?php $color_hon = '#FFD9D5'; // red 210 = #FFC6BF . . . 220 = #FFD9D5 $color_husky = '#FED6F5'; // purple 210 = #FEC0F1 . . . 220 = #FED6F5 $color_kaw = '#D6F5D3'; // green 200 = #BCF0B9 . . . 215 = #D6F5D3 $color_ktm = '#FFE4CA'; // gold 200 = #FFD5AA . . . 215 = #FFE4CA $color_suz = '#FFFFD5'; // yellow 200 = #FFFFAA . . . 220 = #FFFFD5 $color_tm = '#D5FFFF'; // turq 200 = #AAFFFF . . . 220 = #D5FFFF $color_yam = '#DFDFFF'; // blue 215 = #CACAFF . . . 225 = #DFDFFF if ( $row_rsStockDynoSh['vs_brand'] == 'Honda' ) { $rowColor = $color_hon; }elseif ( $row_rsStockDynoSh['vs_brand'] == 'Husky' ) { $rowColor = $color_husky; }elseif ( $row_rsStockDynoSh['vs_brand'] == 'Kawasaki' ) { $rowColor = $color_kaw; }elseif ( $row_rsStockDynoSh['vs_brand'] == 'KTM' ) { $rowColor = $color_ktm; }elseif ( $row_rsStockDynoSh['vs_brand'] == 'Suzuki' ) { $rowColor = $color_suz; }elseif ( $row_rsStockDynoSh['vs_brand'] == 'TM' ) { $rowColor = $color_tm; }elseif ( $row_rsStockDynoSh['vs_brand'] == 'Yamaha' ) { $rowColor = $color_yam; }else{ $rowColor = '#FF95FF'; } ?> I am a newbie so take it easy on me. Thanks!! Quote Link to comment Share on other sites More sharing options...
maxxd Posted January 20, 2017 Share Posted January 20, 2017 What did it accomplish? Anything at all? You're assigning a value to a variable and then doing nothing at all with that variable or value as far as I can tell. You have other issues with the code - you need to escape output, for example, not to mention that if you're just learning PHP it's a great time to learn a templating system like Twig as well, but as to your original question I'd start there. Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted January 20, 2017 Solution Share Posted January 20, 2017 (edited) Create classes for the different colors you want. Then have the logic determine which class to use. Or, you can make this much easier by just creating styles named for the brands and then use the dynamic name in defining the class to use. No additional coding needed to set the color: <?php //Test values $row_rsStockDynoFk['vs_brand'] = "Honda"; $row_rsStockDynoFk['vs_yrmodsize'] = "Crx"; ?> <html> <head> <style> .brandHonda { background-color: #FFD9D5; } .brandHusky { background-color: #FED6F5; } .brandKawasaki { background-color: #D6F5D3; } .brandKTM { background-color: #FFE4CA; } .brandSuzuki { background-color: #FFFFD5; } .brandTM { background-color: #D5FFFF; } .brandYamaha { background-color: #DFDFFF; } </style> </head> <body> <div class="lvl1-4-5 brand<?php echo $row_rsStockDynoFk['vs_brand']?>"><?php echo $row_rsStockDynoFk['vs_brand']; ?></div> <div class="lvl1-4-6"><?php echo $row_rsStockDynoFk['vs_yrmodsize']; ?></div> </body> </html> Edited January 20, 2017 by Psycho 1 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 20, 2017 Share Posted January 20, 2017 In addition to Psycho's code, you can use the lvl1-4-5 class to add a default color. <style type="text/css"> .lvl1-4-5 { background-color: #FF95FF; } .brandHonda { background-color: #FFD9D5; } /* ... */ </style> Quote Link to comment Share on other sites More sharing options...
Tmod Posted January 20, 2017 Author Share Posted January 20, 2017 Psycho & cyberRobot, Thanks for the code and recommendations, I tend to overthink things and am my own worst enemy, You simplified it and it works like a champ. I ended up changing the vs_yrmodsize code to the following and it changed the background on it as well to properly display depending on vs_brand. <div class="lvl1-4-6 brand<?php echo $row_rsStockDynoSh['vs_brand']?>"><?php echo $row_rsStockDynoSh['vs_yrmodsize']; ?> Once again thanks!! Tmod Quote Link to comment 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.