fcamp
fcamp

Reputation: 51

Dynamic Input Fields from PHP Array

I've been trying to wrap my head around this for the last week or so, but really need some guidance.

I need to create dynamic form fields from PHP arrays. The PHP arrays vary in size, but they are always very simple arrays.

I'd like to be able to create the # of input fields by the number of items in the array, and populate the input fields with the array data.

The array data will look like the below (2 examples).

Array
(
    [0] => 19001.WAV

    [1] => 19307.WAV

    [2] => 19002.WAV

    [3] => 19308.WAV

    [4] => 19003.WAV

    [5] => 19009.WAV

    [6] => 19004.WAV

    [7] => 19310.WAV

    [8] => 19005.WAV

    [9] => 19311.WAV

    [10] => 19009.WAV

    [11] => 19307.WAV

    [12] => 19010.WAV

    [13] => 19308.WAV

    [14] => 19013.WAV

    [15] => 19309.WAV

    [16] => 19015.WAV

)

Or:

Array
(
    [0] => 101.WAV

    [1] => 101.WAV

    [2] => 102.WAV

    [3] => 102.WAV

    [4] => 103.WAV

    [5] => 103.WAV

)

I just don't even really know what direction to go.

Upvotes: 1

Views: 59

Answers (3)

ProgrammerV5
ProgrammerV5

Reputation: 1962

Not a PHP programmer but I think it should go along the lines of:

<?php
reset($Array);
foreach ($arr as $key => $value) {
    echo "<input name='data[]' id='$key' value='$value'></input>\n";
}
?>

Hope it helps.

Upvotes: 0

Matteo Kovacic
Matteo Kovacic

Reputation: 159

If you just want to render values use foreach:

<?php 
   foreach($array as $item) {
      echo '<input type="text" name="data[]" value="'. $item .'">'
   }
?>

Upvotes: 1

Sanchit Gupta
Sanchit Gupta

Reputation: 3224

Please refer foreach

foreach ($arr as $value) {
    echo "<input name='data[]' value='$value'> <br />";
}

Upvotes: 1

Related Questions