MJ93
MJ93

Reputation: 5286

How can I use this php empty function to properly display CSS?

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

Upvotes: 0

Views: 68

Answers (1)

Anonymous
Anonymous

Reputation: 3689

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!

Upvotes: 1

Related Questions