Forum Moderators: coopster
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.
$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;
$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.
[edited by: php4U at 1:32 am (utc) on Mar. 20, 2009]