load text file list into <option> tags

半腔热情 提交于 2019-12-25 01:47:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!