Search code examples
phphtmltext-fileshtml-select

load text file list into <option> tags


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


Solution

  • $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;