Read values from text file into array

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

unwiredphpnovice
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Nov 25, 2013 5:00 am

Read values from text file into array

Postby unwiredphpnovice » Mon Nov 25, 2013 5:17 am

I am very new to php and programming in general. I am trying to read values from a text file into an array. The web page is to display these array values in a selection list and once the selection has been made and the submit button activated the background-color should change to the color selected. If anything but the white page color is selected the font color should be white otherwise the font color should be black.

The page color is changing color to the correct value in the selection list but the text/font color is not working. Any assistance that can be provided to resolve this is greatly appreciated.

Here is the contents of the text file: color.txt

Code: Select all


red
blue
white



Here is the file with the form element: pageColor2.php

Code: Select all


<body>
        <form action="pageColor2.php" method="post">
        Please select a page color<br><br>
        <?php
        include "activity2.php";
        ?>
        <br><br>
        <input type="submit" id="button1" name="button1" />
        </form>
</body>



Here is the file with the php code: activity2.php

Code: Select all


<?php
// Assigns the content of "colors.txt" to $colorsArray.
$colorsArray = file("/path to/colors.txt");
// Assigns $color to the selection in the options list.
$color = $_POST['color'];
// If the submit button is activated then:
if (isset ($_POST["button1"])){
  // The beginning of the select control.
  echo "<select name='color' id='color'>";
  // Loop through each value of the array and print it as an option element.
  foreach($colorsArray as $value) {
    // Explodes the color.txt file.
    $colorsArray = explode(" ", $value);
    // Assigns each value of the $colorsArray to $color ($_POST['color])
    $colorsArray = $color;
    echo "<option value='$value' selected='selected'>$value</option><br>";
  }
// The end of the select control.
  echo "</select>";

 /* This is where the code appears to fail */

  if ($color == $colorsArray[0][0] || $colorsArray[0][1]){// I am getting an error on this line.
    echo "<body style='background-color:$color; color: white;'>";
  }else
  // Changes the page color to the value selected.
  echo "<body style='background-color:$color; color: black;'>";
}// if(isset ($_POST["button1])).
print_r ($colorsArray);

?>



The array that I am getting is this

Code: Select all


(
    [0] =>
)



When what I think I need to get is something like this:

Code: Select all


(
(
    [0] => red
)
(
    [0] => blue   
)
(
    [0] => white
)
)


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 4 guests