3
\$\begingroup\$

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>

Styling 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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Firstly, you could shorten the names of the variables e.g from $nav_color_i_active to $i_active and so on. Secondly, there is a problem in your logic. You see else part will run if $navcolorhas any value other than 1. You should use elseif instead of else in both parts of code.

\$\endgroup\$
1
  • \$\begingroup\$ Well, if the owner of my script changes the navbar value in the database (tinyint(1)), to, for example 2, by accident, then it won't be fatal for the site's design. But thanks for the suggestion. \$\endgroup\$ Commented Aug 27, 2014 at 12:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.