Using a .txt document within a select tag

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amstin213
    New Member
    • Mar 2013
    • 1

    #1

    Using a .txt document within a select tag

    I have an assignment that I need to use a select tag to list three cities. I know how to code in html for a select tag.
    This is what I have already working


    Code:
    <select name="city" size="1">
            <option value="0">-</option>
            <option value="Chicago">Chicago</option>
            <option value="Detroit">Detroit</option>
            <option value="Toronto">Toronto</option>
        </select>


    Now what I need is the same results ^^ that gives you but by using a seperate .txt file for the cities. This is the code I have written. I know that the read file code is working fine. What I can't figure out is how to input the information in the select tag. I am sure, I am having a simple syntax issue but I really need to get this figured out.


    Code:
    $filename = 'data/'.'cities.txt';
    	
    		$lines_in_file = count(file($filename));
    
    		$fp = fopen($filename, 'r');   
    
    		for ($ii = 1; $ii <= $lines_in_file; $ii++)
    		{
    			$line = fgets($fp);  
    			$city = trim($line);
    			
    			print"<select  size='1'>;
    				'$_POST['$city']';	 
    				</select>";
    		}
    				 
    		fclose($fp);

    Thanks for the help!
    Last edited by Rabbit; Mar 1 '13, 09:52 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    On line 13, why are you printing out the city from the post array? Just use $city. The $_POST array is to retrieve data that was passed to the page from a form.

    Comment

    • divideby0
      New Member
      • May 2012
      • 131

      #3
      It's been a couple of years since I did anything php or html for that matter, but if your data file had each city on its own line in the file, then you might try something like below.

      Code:
      $out = "<select name='city' size='1'><br />"
           . "<option value='0'>--</option><br />";
      
      while(($buf = fgets($fp)) !== FALSE)
      {
         $out .= "<option value='" 
              . trim($buf)
              . "'>"
              . trim($buf)
              . "</option><br />";
      }  
      
      if(!feof($fp))
      {
         fclose($fp);
         exit("FILE READ ERROR: " . __LINE__);
      }
      
      $out .= "</select>";
      echo $out;
      
      ....

      Comment

      Working...