问题
Im working on a PHP project that involves selecting a song, hitting the submit button and then having the PHP loading the song player to the correct option.
For the song selection I want to load these options from a txt file with the following formatting:
<br />
<code>All For Love <br /> Break Free <br />Come to the River <br />For You Nam </code>
Which I want to be processed to end up like this:
<br /> <code>
< option value="All For Love"> All For Love< /option> <br />
< option value="Break Free">Break Free< /option> <br />
< option value="Come to the River">Come To The River< /option> <br />
< option value="For Your Name">For Your Name< /option> <br />
How would I go about achieving this? project so far
回答1:
$songs = file('path/to/file/with/songs.txt');
$options = '';
foreach ($songs as $song) {
$options .= '<option value="'.$song.'">'.$song.'</option>';
}
$select = '<select name="songs">'.$options.'</select>';
echo $select;
回答2:
<?php
$file_array = file($file_path);
foreach ($file_array as $song)
echo '<option value="'.$song.'">'.$song.'</option>'.PHP_EOL;
?>
回答3:
$file_path = 'data/canada.txt';
$file_array = file($file_path); $options = '';
foreach ($file_array as $song){
$options .= ''.$song.'';
}
$select = ''.$options.'';
echo $select;
来源:https://stackoverflow.com/questions/3767076/load-text-file-list-into-option-tags