1
\$\begingroup\$
 <select name="checkInDay" tabindex="3" onchange="calcDay();" class="ffe selectform" id="checkInDate">
    <?php for($i=1;$i<=31;$i++) {
        $value = $i < 10 ? "0".$i : $i;
     ?> 
     <option value="<?= $value ?>" <?php if($i == 12) {?> selected="selected" <?php } ?> > <?= $i ?> </option>
     <?php } ?>

  </select>
\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

I suggest to build a function which can be reused for the options:

function select_options($options, $selected = null) {
  $_options = '';
  foreach ($options as $value => $content) {
    $_options .= sprintf("<option value=\"%s\"%s>%s</option>\n", $value, $value == $selected ? ' selected="selected"' : '', $content);
  }

  return $_options;
}

And some PHP "magic" for the values generation but it is not really needed:

<?php $values = array_map(create_function('&$v', 'return sprintf("%02d", $v);'), range(1, 31)) ?>
<select name="checkInDay" tabindex="3" onchange="calcDay();" class="ffe selectform" id="checkInDate">
  <?php echo select_options(array_combine($values, range (1, 31)), 12) ?>
</select>

But more important than all: in a HTML template, use the PHP alternative notation.

\$\endgroup\$
2
\$\begingroup\$

Remove Magic Numbers

What does "12" represent? Why is it the selected value?

Rather than leaving this as-is, I would strongly recommend replacing it with a more descriptive constant.

Business Logic

This may or may not be something worth changing. It really depends on what this drop-down is being used for.

Does it make sense for this drop-down to have 31 numbers listed?

It seems like the option 31 would only make sense for some months, whereas the options 1 - 28 make sense for all months. Is this being used next to Month drop-down? If so, perhaps it should dynamically update based on the month selected. If not, perhaps the options should be from 1 - 28, and then a last option, "Last Day of Month".

If you keep it as-is, just be sure to be clear to your users what will happen in a month like February if they select 30.

Short Tags

I would personally avoid the use of short tags.

For more information, see https://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use

Text Formatting

To pad your number with leading zeros, use a built-in function.

As @spookycoder suggested:

$value = sprintf("<option value=\"%02d\"", $i);

Code Is Read Far More Than It Is Written

Make the code as clear as is possible. As @Keven recommended, use the PHP alternative notation.

A Matter of Taste

While this is just my preference, I would also consider moving whether or not the option is selected off the HTML option line.

<?php 
    if ($day == DEFAULT_CHECK_IN_DAY):  // Changed $i to $day and made 12 a const for clarity
        $selected = ' selected="selected"';
    else:
        $selected = '';
    endif;
?>
<option value="<?php echo sprintf("%02d", $day) ?>"<?php echo $selected ?>>
    <?php echo $day ?>
</option>
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.