I have some php code to style a button and activate it, when a specific variable is true.
It's actually a radio button, styled with the bootstrap to a button.
As you can see below, this is how it looks when the variable is '1'.
Also, the buttons at the bottom are the same, but then with one extra option.
The HTML code for this is the following:
Navigation bar color:
<?php
if ($navcolor == '1') {
$nav_color_i_active = 'active';
$nav_color_i_checked = 'checked';
$nav_color_n_active = '';
$nav_color_n_checked = '';
} else {
$nav_color_i_active = '';
$nav_color_i_checked = '';
$nav_color_n_active = 'active';
$nav_color_n_checked = 'checked';
}
?>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary <?= $nav_color_i_active ?>">
<input type="radio" name="navcolor" value="1" <?= $nav_color_i_checked ?>> Inverted
</label>
<label class="btn btn-primary <?= $nav_color_n_active ?>">
<input type="radio" name="navcolor" value="0" <?= $nav_color_n_active ?>> Normal
</label>
</div>
<div class="form-group">
<label>Navigation bar position:
</label>
<?php
if ($navpos == '2') {
$nav_pos_2_active = 'active';
$nav_pos_2_checked = 'checked';
$nav_pos_1_active = '';
$nav_pos_1_checked = '';
$nav_pos_0_active = '';
$nav_pos_0_checked = '';
} elseif ($navpos == '1') {
$nav_pos_2_active = '';
$nav_pos_2_checked = '';
$nav_pos_1_active = 'active';
$nav_pos_1_checked = 'checked';
$nav_pos_0_active = '';
$nav_pos_0_checked = '';
} else {
$nav_pos_2_active = '';
$nav_pos_2_checked = '';
$nav_pos_1_active = '';
$nav_pos_1_checked = '';
$nav_pos_0_active = 'active';
$nav_pos_0_checked = 'checked';
}
?>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary <?= $nav_pos_2_active ?>">
<input type="radio" name="navpos" value="2" <?= $nav_pos_2_checked ?>> Floating
</label>
<label class="btn btn-primary <?= $nav_pos_1_active ?>">
<input type="radio" name="navpos" value="1" <?= $nav_pos_1_active ?>> Sticky to top
</label>
<label class="btn btn-primary <?= $nav_pos_0_active ?>">
<input type="radio" name="navpos" value="0" <?= $nav_pos_0_active ?>> Dynamic, stick to top
</label>
</div>
</div>
As you can see, it's a lot of code for not a lot of buttons. I've used empty variables, because otherwise php would return errors, for unknown variables.
Is it possible that this code can look a lot cleaner, and that there can be less code?