Frilleee Frilleee - 1 month ago 7
PHP Question

creates variables in loop or fromArray

So i have a little problem that i cant figure out and not sure how to do it
so i have a loop that creates form boxes and im gonna send all of them to a excel file(but thats later)
now i just want to create all variables in the loop

here is the loop with 1 textbox just to show

<?php

$i=1;
while ($i<=31){
?>

<input type = "text" name="day" />
<br />

<?php
$i=$i+1;
}

?>


so now how do i create a unique name for each box? or whats the best way to do it i read that array, fromarray is good at it if so im not good with arrays so how do i do it?

Answer

Change your input name like day[]

<?php
$i=1;
while ($i<=31){
    ?>
    <input type = "text" value="day<?php echo $i; ?>" name="day[]" />
    <br />
    <?php
    $i++;
 }
?>

When you process form value with GET/POST like bellow (My example with POST) :

<?php
$days = $_POST['day'];
foreach($days as $day) {
     echo $day;
}
?>
Comments