Skip to content
Advertisement

Remove last empty line from select field with “for” loop wordpress

I am create a custom widget in WordPress and in the widget I try to create a select field with for loop and shows in it 10 numbers from 1 to 10.

I created BUT underneath number 10 it have an empty line.

Can you help me to remove that empty line?

this is number 10 this is an empty line under number 10

<label for="<?php echo $this->get_field_id('text'); ?>">
<select width="50" style="width: 50px"  class='widefat' id=" <?php $this->get_field_id('timet8t2'); ?>" name="<?php $this->get_field_name('timet8t2'); ?>" type="text">

<?php 
    for($i=1; $i<11; $i++)
    {

        echo "<option value=".$i.">"."$i<br>"."</option>";
    }
?> 


    <option name="<?php $this->get_field_name('timet8t2'); ?>"> </option> 
  </select> 
</label>

Advertisement

Answer

Part 1

You need to remove the last option:

<option name="<?php $this->get_field_name('timet8t2'); ?>"><?= $this->get_field_name('timet8t2'); ?></option>

Part 2

To display the option label with a leading 0, you can use sprintf().

See Below:

<label for="<?php echo $this->get_field_id('timet8t2'); ?>">
    <select width="50" style="width: 50px"  class='widefat' id=" <?php $this->get_field_id('timet8t2'); ?>" name="<?php $this->get_field_name('timet8t2'); ?>" type="text">

<?php 
    for($i=1; $i<11; $i++)
    {
        echo "<option value=".$i.">". sprintf("%'.02d", $i) . "</option>";
    }
?> 
  </select> 
</label>

Simplified PHPSandbox

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement