Hi there,
If I have a text file with the word "Alive" in it, how can I set up a variable that looks like this using that word.

<?php

$Tree_Alive= "<img src='images/LiveTree.png' width='150' height='250' border='0'>";

  echo $Tree_Alive;

?>

Now if the word changes, due to form handling, to the word Dead, How can I get it so it changes the image to be of a dead Tree?

<?php

$Tree_Dead = "<img src='images/DeadTree.png' width='150' height='250' border='0'>";

  echo $Tree_Dead;

?>

So whatever the word in that file, it becomes part of a variable that shows the correct type of image.
I have been playing with this for a while using "readfile($fn)" but to no avail.
Your help is appreciated.

    If I have a text file...

    What is your program logic that's reading that file? Does the value saved in the file have a new-line character on the end of it? Is your program logic taking into account any new-line characters?

    Turn a word in a text file to a variable.

    Having different variables with names that indicate the data in the variable is not good programming. Variable names should indicate the purpose of the data, not what the data value is. Your variable name should be something like $tree_html and you would have program logic that assigns the correct value to the variable, using the content of your text file to control what the program logic does.

    Based on what I think you are stating you are trying to do, this is one way of implementing it -

    <?php
    
    $control_file_name = 'something.txt'; // whatever your text control file name is
    
    $control_value = trim(file_get_contents($control_file_name)); // get the value in the text file and trim any white-space and new-line characters from around it
    
    // define the permitted control values and the corresponding image name
    $permitted_values = array();
    $permitted_values['Alive'] = 'LiveTree.png';
    $permitted_values['Dead'] = 'DeadTree.png';
    
    // get the image name corresponding to the supplied control value or use a default name
    $image_name = isset($permitted_values[$control_value]) ? $permitted_values[$control_value] : ''; // change the '' to a default value if you want one
    
    // use the image name to produce the html markup or set up an error message if the value isn't valid
    if($image_name)
    {
    	$image_html = "<img src='images/{$image_name}' width='150' height='250' border='0'>"; 
    
    }
    else
    {
    	$image_html = "The content of the text file doesn't correspond to a defined image.";
    }
    
    
    // usage in your html document
    echo $image_html;

      I think I might do something like...

      $treeImages = array(
          'alive' => 'LiveTree.png',
          'dead'  => 'DeadTree.png'
      );
      $treeStatus = strtolower(trim(file_get_contents('tree.txt')));
      if(isset($treeImages[$treeStatus])) {
          "<img src='images/{$treeImages['treeStatus']}' width='150' height='250' border='0'>";
      }
      

        Hi there,
        I worked it out in the end and it is already up and running.
        It is about kites, trees was just an example. My friend wanted to have a sale on his webpage hence me wanting to find a way to do it right.
        This is what I came up with in the end, and I will have a go at your answers as it will happen once again that is for sure.
        What I am doing is sending the script to a certain folder called "sold", both images are called the same but are variant in both folders.

        <?php
        $kite_one_Img = "";
        $kite_one_Img_Active = "../img/Sale2017/";
        $kite_one_Img_Sold = "../img/Sale2017/sold/";
        
        $folder = "ActiveOrSold/";
        
        $Img = $folder ."kite_one.inc";
        $fn = $folder ."kite_one.txt";
        if (isset($_POST['kite_one']))
        
        {   $kite_one = $_POST['kite_one'];
        
        $fp = fopen($fn,"w") or die ("Error opening file in write mode!");
        
        fputs($fp,$kite_one);
        
        fclose($fp) or die ("Error closing file!");
        
        }
        if (isset($_POST['kite_one'])){
        
        $kite_one_file= fopen($Img,"w") or die ("Error opening file in write mode!");
            fwrite($kite_one_file, "<?php");
            fwrite($kite_one_file, "\n");
            fwrite($kite_one_file, "$" ."kite_one_Img" ." = "); 
            fwrite($kite_one_file, "$" ."kite_one_Img_" .$kite_one); 
            fwrite($kite_one_file, " ;" ."\n" ."?>");
            fclose($kite_one_file); 
        }
        
        include ($folder ."kite_one.inc");
        
        ?>
        
        <tr class="item">
        <td align="left"><strong>Kite_one</strong></td>
        <td width="60"><img src="<?=$kite_one_Img;?>kite_one.jpg" style="width:60px; height:60px; border:none;"></td>
        <td align="center"><form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post"><input type="text" name="kite_one"  size="3" id="kite_one" maxlength="8" value="<?php readfile($fn); ?>" >&nbsp;&nbsp;<input  type="submit" value="change">
        </form>
        </td>
        </tr>

        So basicaly it writes to a file the variable

        $kite_one_Active

        or

        $kite_one_Sold

        whatever the client types into the form.
        Then at the html side of things I just call for the .inc file and print the variable $kite_one_Img

        The .inc file looks like this after the client types Active.

        <?php
        $kite_one_Img = $kite_one_Img_Active ;
        ?>

        The other file it writes to, writes just the word "Sold" or "Active" so the client does not have to read PHP code .
        There were over 30 items on sale so I had to do this for every item, maybe one of your answers is a much friendlier way?
        I will work with them for sure.
        Thanks a lot for your help.

          Write a Reply...