Manage a Text File List

2019-01-06

Flat files are suprisingly useful for keeping track of lists.  The following example shows an application for managing pairs of data - names and birthdays. (And, no - I don't really have 7 cats!).  You can see a demo HERE.

Two web pages are provided -  index.php and create-read-update-delete-cat.php

Index.php 

Mange a text List - example

 

 create-read-update-delete-cat.phpcreate-read-update-delete-cats screen

 

These are the 4 files required by this application. 

4 files used in list application

list.txt contains data pairs separated by delimters:  "***" and "#"

Chakra#Jun 24, 2017***Henri#July 1, 2012***Juniper#April 8, 2016***Ralph#May 19, 2017***Rosebud#May 3, 2002***Snowball#June 3, 2014***Tanster#May 17, 2006

index.php:


<!DOCTYPE html>
<html>
<head>       
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">  
    <title>Home</title>  
    <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Elsie" rel="stylesheet"> 
    <link rel= 'stylesheet' type='text/css' href= 'style.css'> 
</head>
    
<body>     
    <main>
        <br><br><a  href = 'index.php'><h1>All My Cats Birthdays</h1></a> 
        <div class = 'content-column'>       
                    
            <?php  
            //DEFINE DELIMITERS
            $delim1 = "***";
            $delim2 = "#";
            
            //RETRIEVE TEXT STRING AND CONVERT TO ARRAY
            $string = file_get_contents ("list.txt");
            if ($string !== "") {
                $array1 = explode ($delim1, $string);
                sort ($array1);
            
                foreach ($array1 as $item1) { 
                
                   //LOOP THROUGH EACH ITEM IN ARRAY
                   if ($item1 !== "") {

                       //RETRIEVE EACH  LABEL - VALUE PAIR
                       $array2 = explode ($delim2, $item1);
                       if (array_key_exists (0, $array2) && array_key_exists (1, $array2)) {
                           echo "<div class = 'row'>";
                           echo "<div class = 'label'>" . trim($array2[0]) . "</div><div class = 'value'>" . trim($array2[1]) . "</div><br>";
                           echo "</div>";
                       }
                   }
               }
            }
            ?>

        </div><div class = 'sidebar-column'>
            <a class = 'adminbutton' href = 'create-read-update-delete.php'>Update List</a>  
        
        </div>          
    </main>
</body></html>

create-read-update-delete.php

<?php   

//DEFINE DELIMITERS
$delim1 = "***"; 
$delim2 = "#";

//INITIALIZE TWO ARRAYS AND TWO INPUT FIELDS
$newlabel = $newvalue = "";
$labelarray = $valuearray = array();

if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {
    
    //RETRIVE EXISTING LABELS 
    if (isset ($_POST ['labelarray'])){
         $labelarray = $_POST['labelarray'];
    }
    
    //RETRIEVE EXISTING VALUES 
    if (isset ($_POST ['valuearray'])){
        $valuearray = $_POST['valuearray'];
    }
    
    //RETRIEVE NEW LABEL
    if (isset ($_POST ['newlabel'])){
         $newlabel = $_POST['newlabel'];}
    
    //RETRIEVE NEW VALUE
    if (isset ($_POST ['newvalue'])){
        $newvalue = $_POST['newvalue'];
    }

    
    //DEFINE A NEW STRING FOR THE UPDATED LABELS AND VALUES 
    $newstring =  "";
    
    //FIND THE NUMBER OF ELEMENTS IN THE LABEL ARRAY  
    $count = count ($labelarray); 
    $x = 0;
    
    //FOR EVERY ITEM IN THE LABEL ARRAY AND THE VALUE ARRAY, CREATE A LABEL-VALUE PAIR
    for ($x ; $x <= $count; $x++) {
        if (array_key_exists ($x, $labelarray)  && array_key_exists ($x, $valuearray)) {          
            if (trim ($labelarray[$x]) !== "") {
                
                //ADD PAIR TO THE NEW STRING UNLESS LABEL FIELD IS BLANK
                $pair = trim($labelarray[$x]) . $delim2 . trim($valuearray[$x]);       
                $newstring = $newstring . $delim1 . $pair;
            }
        }
    }
    
    //ADD NEW LABEL-VALUE PAIR TO END OF STRING IF THE LABEL FIELD IS NOT BLANK
    if ($newlabel !== "") {
        $newstring = $newstring . $delim1 . trim($newlabel) . $delim2 . trim($newvalue);
    }
      
    //WRITE STRING TO ORIGINAL FILE
    $newstring = trim ($newstring);
    $newstring = trim ($newstring, $delim1);
  
    $filename = "list.txt";
    file_put_contents ($filename, $newstring);   
  
       
}
?>
<!DOCTYPE html>
<html>
<head>       
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">  
    <title>Update</title> 
    <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Elsie" rel="stylesheet"> 
    <link rel= 'stylesheet' type='text/css' href= 'style.css'> 
</head>
    
<body>     
<main>
    
    <br><br><a  href = 'index.php'><h1>All My Cats Birthdays</h1></a>  
    <h2>Create - Read - Update - Delete</h2>
    <div class = 'content-column'>
        <div class = 'label'>Cat</div>
        <div class = 'value'>Birthday</div>
        <form method = 'post' action = 'create-read-update-delete.php'>

        <?php
        //RETRIVE TEXT STRING
        $string = file_get_contents ("list.txt");        
        if ($string !== "") {  
        
            //CONVERT STRING TO ARRAY
            $array1 = explode ($delim1, $string);
             sort ($array1);

             //LOOP THROUGH ARRAY DISPLAYING LABEL - VALUE PAIRS
             foreach ($array1 as $item1) {
                 $item1 = trim ($item1);
                 $array2 = explode ($delim2 , $item1);
                
                 if (array_key_exists (0, $array2) && array_key_exists (1, $array2)) {                                            
                         
                     echo  "<input class = 'label' type = 'text' name=  'labelarray[]' value =' " . $array2[0] . "' />";
                     echo  "<input class = 'value' type = 'text' name=  'valuearray[]' value =' " . $array2[1] . "' /><br>";          
                 }
             }
         }  

         ?>

         <br><br>
          <div class = 'label'>Add Cat</div><div class = 'value'>Add Birthday</div> <br>
          <input class = 'label' type = 'text'  name='newlabel' /> 
          <input class = 'value' type = 'text' name = 'newvalue' /> <br><br>

         <input  class = 'submitbutton' type = 'submit' value='Update'/>
         </form> 
    </div><div class = 'sidebar-column'>
           <a class = 'adminbutton' href = 'index.php'>View List</a>
          
    </div>
</main>
</body></html>


To post a comment Login or Register