Read values from text file into an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
unwiredphpnovice
Forum Newbie
Posts: 15
Joined: Mon Nov 25, 2013 8:02 am

Read values from text file into an array

Post by unwiredphpnovice »

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 wnere 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
)
)

Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Read values from text file into an array

Post by requinix »

Code: Select all

    // Explodes the color.txt file.
    $colorsArray = explode(" ", $value);
    // Assigns each value of the $colorsArray to $color ($_POST['color])
    $colorsArray = $color;
The first line overwrites $colorsArray with the colors on whatever line it's processing at that moment, so you can't output $colorsArray at the very end to see the original contents.
But on the second line it overwites $colorsArray again (making the first line pointless) with the value from the form.

Get rid of the second line and change the variable name to something else.
unwiredphpnovice
Forum Newbie
Posts: 15
Joined: Mon Nov 25, 2013 8:02 am

Re: Read values from text file into an array

Post by unwiredphpnovice »

Thanks for the advice ... You do get around. That said; as you advised I modified the code.

I came up with this for 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) {
    echo "<option value='$value' selected='selected'>$value</option><br>";
  } 
// The end of the select control.
  echo "</select>";
  // print_r ($colorsArray);
  
/* This is where the code appears to fail */

  if ($color != "red" || "blue"){
    echo "<body style='background-color:$color; color: black;'>";
  }else {
  // Changes the page color to the value selected.
  echo "<body style='background-color:$color; color: white;'>";
  }
}// if(isset ($_POST["button1])).
     
?>

The problem is that it echoes the body style instructions from the last "if statement" for all of the array elements.(I tried it using if($color == "red" || "blue") etc. as well) instead of the ones in the code. How can I get it to read the "else portion" as well.
Last edited by unwiredphpnovice on Thu Nov 28, 2013 9:25 pm, edited 2 times in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Read values from text file into an array

Post by requinix »

You may say it in English as "the color is not red or blue" but you have to write it in programming as "the color is not red and the color is not blue". Otherwise it thinks "(the color is not red) or (blue)" and blue is always true.

Code: Select all

if ($color != "red" && $color != "blue"){
unwiredphpnovice
Forum Newbie
Posts: 15
Joined: Mon Nov 25, 2013 8:02 am

Re: Read values from text file into an array

Post by unwiredphpnovice »

I replaced the code with the more logical version that you suggested. The problem now is that when "red" and "blue" are the same as $color, I would like the text color to change to white.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Read values from text file into an array

Post by requinix »

How can $color be both red and blue?
unwiredphpnovice
Forum Newbie
Posts: 15
Joined: Mon Nov 25, 2013 8:02 am

Re: Read values from text file into an array

Post by unwiredphpnovice »

My apologies, what I meant to say is that when the color red is selected as the background color or the color blue is selected as the background color I would like the text for the page to turn white.

Thanks to your excellent advise the code now looks like this:

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) {
    echo "<option value='$value' selected='selected'>$value</option><br>";
  } 
// The end of the select control.
  echo "</select>";
  // print_r ($colorsArray);
  
/* This is wnere the code appears to fail */
 
  if ($color != "red" && $color != "blue"){
    echo "<body style='background-color:$color; color: black;'>";
  }else {
  // Changes the page color to the value selected. How do I get the text color to change to white when the page color is red or blue
  echo "<body style='background-color:$color; color: white;'>";
  }
}// if(isset ($_POST["button1])).
     
?>

unwiredphpnovice
Forum Newbie
Posts: 15
Joined: Mon Nov 25, 2013 8:02 am

Re: Read values from text file into an array

Post by unwiredphpnovice »

Ok, I have answered my own question. The code that works is here:

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 = 'color';
  // 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) {
    echo "<option value='$value' selected='selected'>$value</option><br>";
  }
  // The end of the select control.
  echo "</select>";
  //print_r ($colorsArray);

  if (isset($_POST['color'])){
    $color = $_POST['color'];
  }
// If the submit button is activated then:
if (isset ($_POST["button1"])){
  // Echos the background and font color if white is not selected.
  if ($color != $colorsArray[2]){
    echo "<body style='background-color:$color; color: white;'>";
  }else {
  // Echos the background and font color if white is selected.
  echo "<body style='background-color:$color; color: black;'>";
  }
}// if(isset ($_POST["button1])).
     
?>

Post Reply