0

I made a page where users can input a Hex Code for different colors they want to change on their page. It all works fine, but I would like to make it so if the box the user is inputting to is empty, then display the default CSS. Otherwise, display a custom one.

I'll use an example:

The users choice gets sent to a form validation:

$this->form_validation->set_rules('channelNameColor',
                    lang('user_themes_channel_name_color'),
                    'trim|strip_tags|xss_clean|min_length[6]|max_length[6]');

Then it eventually gets passed to a file where it can be used in CSS:

It is declared here:

$channelNameColor = $theme['channelNameColor'];

Then down below all those declarations it gets put in as CSS:

.channelNameBar h1{
font-size:20px;
color:#<?=$channelNameColor?>;
float: left;
}

I would like to do something like this:

<?php
if (empty($channelNameColor)){
     echo ".channelNameBar h1{";
     echo "font-size:20px;";
     echo "color:#FFFFFF;";
     echo "float: left;";
     echo "}";
}
else
 {
     echo ".channelNameBar h1{";
     echo "font-size:20px;";
     echo "color:#$channelNameColor;";
     echo "float: left;";
     echo "}";
 }
?>

But it is not recognizing $channelNameColor in: if (empty($channelNameColor)){

Why is it not recognizing it there, but it recognizes it fine when I use it in the css? Sorry if this is a very simple question to answer, I'm pretty new to PHP

5
  • 1
    Maybe it does recognize it, but it's just that the variable isn't empty if the user didn't write anything. How about testing what the variable contains with var_dump($channelNameColor) Commented Oct 23, 2011 at 3:38
  • well i say it doesnt recognize it because if I type in $ in the IDE it usually shows up in the list of options as $channelNameColor, but in the empty function, it does not Commented Oct 23, 2011 at 3:41
  • 1
    well, IDEs aren't all-knowing, so you should test the code somehow :P Commented Oct 23, 2011 at 3:45
  • BTW, if you want you can reduce those rules to something like is_numeric|exact_length[6]. Commented Oct 23, 2011 at 4:00
  • Great, thank you. It turns out it was a problem with the IDE... now I know Commented Oct 23, 2011 at 4:08

1 Answer 1

1

Generally it would make more sense not to declare channelNameColor at all if the user input is empty. You can do this in your validation!

Then later on you can use isset() instead of empty(), which is a bit more reliable after you did your validation!

Also make sure that you include your else-statement in curly brackets, otherwise it will output it anyway!

Sign up to request clarification or add additional context in comments.

4 Comments

Could I add a callback function inside my form validation? Saying to quit if there is no value to the channelNameColor?
I didnt actually use what I have up there, I wrote that on here and accidently forgot to add the braces
Sure you can, but therefore you'd need to extend/edit your validation class. I don't know what class you are using, but that should be easy. The question it what happens if the input doesn't have 6 characters and is not in the proper hex format ? If its a proper validation class it'll return false if not valid. Then just do something like 'if(!$validation) { $channelNameColor = $theme['channelNameColor']; }'
Oh I'm using code igniter and I already have all those boundaries set. But thank you! It turns out it was just an IDE error and thats why it wasnt showing up. I got it working just the way I wanted to now! But I might go ahead and look into doing it in the form validation so that the information does not have to pass through those extra steps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.