Need help completing my code, and making it work.

I’ve created a code to allow the user to change the background color and scheme of my site. However, I’m having trouble getting a random color to show up if the user has not yet set a color choice. Here’s my code. Could someone put it together to make it work?

[php]<?php
if ($_POST[‘change’]){
$thevar = $_POST[‘change’];
$_SESSION[‘bcg’] = $thevar;
}

if(isset($_SESSION[‘thecolor’]))
{
$thecolor = $_SESSION[‘thecolor’];
}
else
{
$num = rand(0,9);
if ($num == 0)
$thecolor = “0DEBDF”; // CYAN
if ($num == 1)
$thecolor = “AE00FF”; // PURPLE
if ($num == 2)
$thecolor = “FFDC01”; // YELLOW
if ($num == 3)
$thecolor = “FF6D2D”; // PEACH
if ($num == 4)
$thecolor = “20ff7f”; // LIGHTER-GREEN
if ($num == 5)
$thecolor = “00E612”; // GREEN
if ($num == 6)
$thecolor = “E6005D”; // PINK
if ($num == 7)
$thecolor = “E600CF”; // PINK-PURPLE
if ($num == 8)
$thecolor = “e2ff20”; // LIME
if ($num == 9)
$thecolor = “8868ff”; // LAVENDER
// send a cookie that never expires
setcookie(“thecolor”,$thecolor, time()+604800);
}
?>[/php]

Thanks. :slight_smile:

Are you finding that the variable is being set?

If it is, then, perhaps, the problem is that they are not yet explicitly hex values. Unless, in the display part of your code, you turn them into hex values? Seems like there’s code missing from here that we might want to see.

if(isset($_COOKIE[‘thecolor’])) {

$thecolor = $_COOKIE[‘thecolor’];

} else {

$num = rand(0,9);

switch($num){
case 0: $thecolor = “0DEBDF”; break; // CYAN
case 1: $thecolor = “AE00FF”; break; // PURPLE
case 2: $thecolor = “FFDC01”; break; // YELLOW
case 3: $thecolor = “FF6D2D”; break; // PEACH
case 4: $thecolor = “20ff7f”; break; // LIGHTER-GREEN
case 5: $thecolor = “00E612”; break; // GREEN
case 6: $thecolor = “E600CF”; break; // PINK
case 7: $thecolor = “e2ff20”; break; // PINK-PURPLE
case 8: $thecolor = “e2ff20”; // LIME
case 9: $thecolor = “8868ff”; // LAVENDER
}
setcookie(“thecolor”,$thecolor, time()+604800);
}

This echo’s out atleast one color when I tried. 8868ff

[php]if(isset($_COOKIE[‘thecolor’])) {

$thecolor = $_COOKIE[‘thecolor’];

} else {

$num = rand(0,9);

switch($num){
case 0: $thecolor = “0DEBDF”; break; // CYAN
case 1: $thecolor = “AE00FF”; break; // PURPLE
case 2: $thecolor = “FFDC01”; break; // YELLOW
case 3: $thecolor = “FF6D2D”; break; // PEACH
case 4: $thecolor = “20ff7f”; break; // LIGHTER-GREEN
case 5: $thecolor = “00E612”; break; // GREEN
case 6: $thecolor = “E600CF”; break; // PINK
case 7: $thecolor = “e2ff20”; break; // PINK-PURPLE
case 8: $thecolor = “e2ff20”; break; // LIME
case 9: $thecolor = “8868ff”; break; // LAVENDER
}
setcookie(“thecolor”,$thecolor, time()+604800);
}[/php]

Damn, forgot to add breaks at the end… try this one instead.

Sponsor our Newsletter | Privacy Policy | Terms of Service