Jump to content

Change background color based off value returned


Tmod
Go to solution Solved by Psycho,

Recommended Posts

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!!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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 by Psycho
  • Like 1
Link to comment
Share on other sites

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

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.