fufaso Posted December 17, 2010 Share Posted December 17, 2010 I'm hoping someone can shed a bit of light on what is wrong with my code here as I've been playing around with it for a few hours and haven't solved the riddle! $fp=@fopen("test.txt","r") or die ("Cannot open the file, not there!"); while ($line=@fgets($fp, 1024)) { $list = explode("\n", trim($line)); sort($list); $count = count($list); for($i=0;$i<$count;$i++){ print "<option>".$list[$i]; }} @fclose($fp) or die ("Still open!!!"); ?> </select> </form> my text file looks like this Canada Brazil US Mexico I'm trying to explode that text file into an array, sort it, and populate the options box. This isn't working, and all the different iterations I've tried are yielding no success either... :-\ Link to comment https://forums.phpfreaks.com/topic/221988-explode-based-on-n/ Share on other sites More sharing options...
marcus Posted December 17, 2010 Share Posted December 17, 2010 Your code is working just fine for me. I only modified the print statement to close the option field. Output: <select> <option>Canada</option> <option>Brazil</option> <option>US</option> <option>Mexico</option> </select> Link to comment https://forums.phpfreaks.com/topic/221988-explode-based-on-n/#findComment-1148691 Share on other sites More sharing options...
fufaso Posted December 17, 2010 Author Share Posted December 17, 2010 really? it's working? sorting the countries by name and inserting them?? i'm getting a blank space as my first dropdown option, then canada, then another blank space, then brazil and so on until the end of the file. Link to comment https://forums.phpfreaks.com/topic/221988-explode-based-on-n/#findComment-1148696 Share on other sites More sharing options...
marcus Posted December 17, 2010 Share Posted December 17, 2010 You could use file() if you wanted. <select> <?php $file = file('t1.txt'); sort($file); foreach($file AS $country){ echo "<option>".$country."</option>\n"; } ?> </select> Link to comment https://forums.phpfreaks.com/topic/221988-explode-based-on-n/#findComment-1148700 Share on other sites More sharing options...
fufaso Posted December 17, 2010 Author Share Posted December 17, 2010 that worked wonderfully, but for some reason when i plug that php code into the <select> tags, the second form disappears? nvm, i just removed the close file and it worked... Link to comment https://forums.phpfreaks.com/topic/221988-explode-based-on-n/#findComment-1148704 Share on other sites More sharing options...
fufaso Posted December 17, 2010 Author Share Posted December 17, 2010 ok, so I have it all working now, but I have one last question. when I insert a new value and append it to my file, I am submitting back to the test.php page, however my new value is not being populated in the dropdown when i do that. I have to hit the reload button on the web browser to have it show up. Is there a way to have it repopulate when I hit submit?? Link to comment https://forums.phpfreaks.com/topic/221988-explode-based-on-n/#findComment-1148708 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.