M. Doe M. Doe - 9 days ago 6
PHP Question

HTML and PHP, filling a dropdown with lines from file

I have a file which currently contains

C41234
B36355
B41234


and I can't seem to find a way to read these from the file and then add each to my dropdown. The contents of the file are subject to change so I need to use a variable of some sort. I'm using php to read my file. I took a shot in the dark and tried

$j= 0;
echo'<b>Choose the reservation you would like to change </b>';
echo'<p>';
echo'<select name="change">';

while (!feof($fp))
{
echo'<option> $reservations[$j]</option>';
$j++;
}
echo '</select>';


where $reservations[] already contains the file contents. it's echoing the html just fine, i'm just not filling the dropdown correctly.
any help is much appreciated!

Answer
$fp = fopen("reservations.txt", "r");

echo '<b>Choose the reservation you would like to change </b>';
echo '<p>';
echo '<select name="change">';

while(!feof($fp)){
    $line = trim(fgets($fp));
    echo "<option>{$line}</option>";
}

fclose($fp);
echo '</select>';

Hope this code helps you!