Forum Moderators: coopster

Message Too Old, No Replies

using in array to compare vals to known COOKIE

         

php4U

10:40 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



I'm using the following to check if the COOKIE is set and to show an image based on that selection...COOKIE values are red, blue, and green.

This works just fine, but I wanted to see about changing it so I can use this somewhere else where it would provide a hex color like #FF0000


$theirstyle = isset($_COOKIE['theirstyle']) ? $_COOKIE['theirstyle'] : '';
// after some validation of $theirstyle
$header_image = in_array($theirstyle, array('red', 'blue', 'green')) ?
'images/header_' . $theirstyle . '.gif' : 'images/header_red.gif';

I read up on arrays and I changed the code to 'red' => '#FF0000', and removed the reference to the image etc. but it isn't working properly because it still looks for that initial name "red" "blue" "green" value. I know I can do this with an image if I really need to but I am curious if it can be done through this method. Thank you for any suggestions.

idev

10:47 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



Make sure you understand the code.

$theirstyle = isset($_COOKIE['theirstyle']) ? $_COOKIE['theirstyle'] : '';
// here you get the theirstyle from cookie

$header_image = in_array($theirstyle, array('red', 'blue', 'green'))
// if it's within the array, e.g. 'blue'

?

'images/header_' . $theirstyle . '.gif'
// use the blue.gif image

: 'images/header_red.gif';
// otherwise use the default image

So... if there's a color within that cookie, just check if the value is valid (nothing embedded), e.g. if(preg_match('/#[a-zA-Z0-9]/', $theirstyle)) echo $theirstyle;

php4U

12:19 am on Mar 20, 2009 (gmt 0)

10+ Year Member



Thanks for the reply. Yeah, I understand the code because it works fine now for what I am using it for as I mentioned. I get the logic in it, and how it works. When the style is changed it's done like this switcher.php?set=red and nothing is set as far as #FF0000. Trying to figure out if "red" is set how I can use "red" in the code for the image like above and how I could also use #FF0000 as the value in another area like...

$footer_color = in_array($theirstyle, array('red' => '#FF0000', 'blue' => '#ABC', 'green' => '#XYZ')) ?
'' . $thierstyle . '' : '#FF0000';

I want to save myself from creating 3 more images like the header. If I can somehow get the array to output #FF0000 for the "red" style I can use css to create the bottom border, but I don't think it's possible since #FF0000 isn't set in the COOKIE.

idev

1:08 am on Mar 20, 2009 (gmt 0)

10+ Year Member



Actually red, blue and green are supported by both html and css.

If you wish to use the translator array, you'll need to setup a separate array or search by key, not by value as in_array does.

Good luck.

php4U

1:28 am on Mar 20, 2009 (gmt 0)

10+ Year Member



Yeah just like black, white, yellow etc. does because the array does work with those name values. There is a reason that I am asking about a hex value, because the color such as color: red; isn't close to my red color (#FF0000 was only an ex.). I'll check out the other array types, thank you.

[edited by: php4U at 1:32 am (utc) on Mar. 20, 2009]

idev

2:31 am on Mar 20, 2009 (gmt 0)

10+ Year Member



there're no other array types. it's just the way you check if you have that value built into your script

$a = array('red' => '#FF0000', 'blue' => '#ABC', 'green' => '#XYZ');

echo $a[$theirstyle];

would give you the color code you want;

welcome!

php4U

3:11 am on Mar 20, 2009 (gmt 0)

10+ Year Member



I mis-spoke, I just meant another method other than in_array, sorry about that.

I had the idea of the array right, but had difficulty trying to tie it to my known variable $theirstyle

It worked like a charm, and I really appreciate your help on this!