Asmat Ali
Asmat Ali

Reputation: 335

Can I create multiple SELECT boxes using loop in PHP?

I am trying to create 12 select boxes for names to be selected for each month of the year. This could be done by creating all of the 12 select boxes one by one, but I wanted this to be done with a for loop but it didn't work. On the frontend, the code displayed all the 12 boxes, but on the backend, when I tried to get data from the boxes, no data was received.

The HTML code with the loop:

<?php for($i = 1; $i <= 12; $i++){ ?>
<select name ="<?php echo $i; ?>" >
        <option value="">- <?php echo $i; ?> -</option>
        <?php foreach($names as $name){  ?>
         <option value ="<?php echo $name['insp_name']; ?>"><?php echo $name['insp_name']; ?></option>
         <?php  } ?>
</select>
<?php } ?>

I wrote some test code in the controller but this output nothing.

$month = filter_input(INPUT_POST, '1');
        echo $month;

Please note that I tested the array $names and the names have been correctly retrieved from the database. I guess the problem is here <select name ="<?php echo $i; ?>" > , but how to deal with it? Or I just have to write code for each box separately?

Upvotes: 0

Views: 95

Answers (0)

Related Questions