This is driving me nuts. Take a look at this code:

<?php 
$foo = file_get_contents('list.txt');

$jobs = array('one','two','three');

?>
<select>
	<?php foreach($jobs as $key):?>
	<option value='<?php echo $key; ?>'><?php echo $key; ?></option>
	<?php endforeach; ?>
</select>

Simple right? It creates a drop down list of jobs, values are one, two, three.

Now...take those same values, but this time we're reading them from a text file:

<?php 
$foo = file_get_contents('list.txt');

$jobs = array($foo);

?>
<select>
	<?php foreach($jobs as $key):?>
	<option value='<?php echo $key; ?>'><?php echo $key; ?></option>
	<?php endforeach; ?>
</select>

View the source and you'll see the problem. reading the file into the array doesn't break apart the individual values. It's as if the $jobs array doesn't see the single quotes and commas separaring the array values!

I've spent hours on this. Can anyone advise what the deal is here?

Regards,
Matt

    $foo = file_get_contents('list.txt');
    $jobs = explode("," , $foo);

    the rest of your code.

      matt6805 wrote:

      It's as if the $jobs array doesn't see the single quotes and commas separaring the array values!

      That's exactly what is happening; all it's seeing is a string because that's all you're giving it. No magic here.

        Ok, using the explode function does the trick to break up the array values, but what I'm really trying to accomplish is this:

        $foo = file_get_contents('list.txt'); //Value is 'one','two','three'
        
        $jobs = explode("," , $foo);
        $career_positions = array('Store Line' => array($jobs));
        

        I trying to find a way to loop though the $jobs array and print them as the values for the Key 'Store Line'. Because I don't know how many jobs will be posted, I want to use a while or foreach loop to populate that list. I've tried both of those and the syntax is wrong.

        Regards,
        Matt

          matt6805;10943906 wrote:

          I trying to find a way to loop though the $jobs array and print them as the values for the Key 'Store Line'.

          But if you start out with these values in a string, why not keep them as a string and NOT loop over anything in the end?

          I.e. rather this

          $string = "'one', 'two', 'three'";
          $career_positions = array('Store Line' => $string);
          

          than the unnecessary detour over array before going back to string.

          $string = "'one', 'two', 'three'";
          $array = explode(',' $string);
          
          $career_positions = array('Store Line' => '');
          foreach($array as $element) {
          	$career_positions['Store Line'] .= $element;
          }
          

            jobs.txt contains:

            a
            bbb
            élksdflék
            élkasdasd

            Then your select box is:

            <?php
            $inputs = file( "jobs.txt" );
            ?>
                <select name='jobs'>
                <option value=''>--select--</option>";
            <?php
            
            $job_value=isset($_POST["jobs"]) ? $_POST["jobs"] : NULL;
            foreach( $inputs AS $value )
            {
                $selected=($job_value==$value) ? "selected='selected'" : '';
                echo "<option value='$value' $selected>$value</option>";
            } 
            ?>
            </select>
            

              I finally got it. The problem was that what i was passing to the array, was another array.

              So this is a three dimensional array:

              $string = "'one', 'two', 'three'";
              $array = explode(',' $string); 
              
              $career_positions = array('Store Line' => array($array)); 
              

              But this is actually what I wanted:

              $string = "'one', 'two', 'three'";
              $array = explode(',' $string); 
              
              $career_positions = array('Store Line' => $array); 
              

              That produces 'one', 'two', 'three' as the values of the Key Store Line. God it took me two days to get that. Good thing I'm stubborn. Thanks johanafm. This is solved.

              Regards,
              Matt

                But apostrofes (') should not be a part of your data when you make select box.
                Not to mention the whitespaces too.

                  Write a Reply...