Code for "All My Cats" Flat File Application

Dabase Functions

<?php


//SET TABLE FIELDS
$dbtables = array (
      "cats" => array ("name", "color", "sex", "age", "details") 
    );


function readDatabaseRecord($table, $recordid) {
   
    global $dbtables;   
    
    //INITIALIZE TABLE TO EMPTY
    $record = array();
    foreach ($dbtables[$table] as $label) {
       $record[$label] = "";
    }

    //FILL IN FIELD VALUES FROM TEXT STRING 
    $filename = "data/" . $table . "/" . $recordid . ".txt";    
    if (file_exists($filename)) {
        $string = file_get_contents ($filename);
   
        $array1= explode ("***", $string);
        foreach ($dbtables[$table] as $id => $label) {
            $record[$label] = "";  
            if (array_key_exists ($id, $array1)) {
                $record [$label] = $array1[$id]; 
            }
        }
    }
    return $record;
}
           


function writeDatabaseRecord ($table, $recordid, $record) {  

    $newstring = implode ("***", $record);   
    $newstring = html_entity_decode ($newstring, ENT_QUOTES ); 
    $filename = "data/" . $table . "/" . $recordid . ".txt";
    file_put_contents ($filename, $newstring);    
}

function createUniqueKey($name, $table) {

     $newkey = ""; 
     if ($name === '') {
         echo "<div class = 'error'>Missing Name</div>";         
     }
     else {
        $newkey  = str_replace(" ", "-", $name);  
        $newkey = strtolower ($newkey);
        $newkey = preg_replace('/[^\A-Za-z0-9-]+/', '', $newkey); 
        if ($newkey === "") {
             echo "<div class = 'error'>Invalid Record Name</div>";              
        }    
        else {          
            // CHECK THAT THIS RECORD DOESN'T ALREADY EXIST   
            $filename = "data/" . $table . "/" . $newkey . ".txt";  
            if (file_exists ($filename)) {
               echo "<div class = 'error'>record with this name already exists</div>"; 
               $newkey = "";
            }
        }
     }
     return $newkey;
}

?>

index.php

<?php include ("inc/header.php");  ?>
  
<div class = 'content-column'>

    <?php           

    //SHOW ALL NAMES AS LINKS TO DISPLAY PAGE 
    $array1 = scandir ("data/cats");
    foreach ($array1 as $item) {
        $item = trim ($item);
        if ($item !== "." && $item !== ".." && $item !== 'removed') {
            $catid = str_replace (".txt", "", $item);          
           
            $catrecord = readDatabaseRecord("cats", $catid);
            echo "<a  class = 'box' href = 'display-cat.php?cat=" . $catid . "'>" . $catrecord['name'] . "</a>";          
        }
    }
    ?> 
</div><div class = 'sidebar-column'>          
    <a class = 'button' href = 'create-update-cat.php'>Add A New Cat</a> <br>   

</div> 
<br><br><br>

 <?php include ("inc/footer.php"); ?>  

display-cat.php

<?php 

include ("inc/header.php");

//RETRIEVE RECORD ID FROM QUERY STRING
$catid =  "";
if (isset ($_GET["cat"])){   
    $catid = filter_input(INPUT_GET, "cat", FILTER_SANITIZE_STRING) ;
    $catid = trim ($catid); 
}  
//USE ID TO READ CONTENTS OF RECORD 
$catrecord = readDatabaseRecord("cats", $catid);

?>
<h2><?php echo $catrecord['name']; ?></h2><br>     
<div class = 'content-column' >
     <?php
     //DISPLAY LABELS AND VALUES FOR THIS RECORD
     echo "<div class = 'label'>Id:</div>" . $catid . "<br>";
     foreach ($dbtables["cats"] as $label) {  
         if (array_key_exists ($label, $catrecord)) {
             echo "<div class = 'label'>" . ucwords ($label) . ":</div>" .   $catrecord[$label] . "<br>";  
         }
     }
     ?>                          
     <br><br>
</div><div class = 'sidebar-column'>        
     <a class = 'button' href = 'index.php'>Return to Home Page</a>
     <a class = 'button' href = 'create-update-cat.php?cat=<?php echo $catid; ?>'>Edit</a>
     <a class = 'button' href = 'remove-cat.php?cat=<?php echo $catid; ?>'>Remove</a><br>
</div>
   
<?php include ("inc/footer.php"); ?>

create-update-cat.php


<?php 

include ("inc/header.php");

//RETRIEVE RECORD ID FROM QUERY STRING
$catid =  "";
if (isset ($_GET["cat"])){   
    $catid = filter_input(INPUT_GET, "cat", FILTER_SANITIZE_STRING) ;
    $catid = trim ($catid); 
}

//USE ID TO READ CONTENTS OF RECORD - NULL OF NONE EXISTS
$catrecord = readDatabaseRecord ("cats", $catid);
 
//RETRIEVE FORM INPUT 
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {     
    
    //ASSIGN FORM DATA TO RECORD 
    foreach ($dbtables["cats"] as $label) {      
       if (isset ($_POST[$label])) {            
           $catrecord [$label] =  trim($_POST[$label]);             
       }
    }

    //CREATE A UNIQUE RECORD ID IF NONE EXISTS
 
    if ($catid === '') {
        $catid = createUniqueKey ($catrecord['name'], "cats");       
    }
    if ($catid !== "") {        
        writeDatabaseRecord("cats", $catid, $catrecord);  
    }     
 }

//DISPLAY FORM
if ($catid === "") {
    echo "<h2>Add a Cat</h2>";
}
else {
    echo "<h2>Edit " . $catrecord['name'] . "</h2>";    
}
?>    
<div class = 'content-column'>

    <form method='post' action='create-update-cat.php?cat=<?php echo $catid ;?>'> 

        <?php
        if ($catid !== ""){
            echo "<div class = 'label'>Id: </div>" . $catid . "<br><br>";
        }
        //DISPLAY INPUT FIELD FOR EACH ITEM IN RECORD
        foreach ($dbtables["cats"] as $label) {         
            echo "<div class = 'label'>" . ucwords ($label) . ":</div>";            
            echo "<input name = '" . $label . "' type = 'text' value = '" . $catrecord[$label] . "'><br>";            
        }
        ?>                          
        <br>
        <input  type = 'submit' name = 'submit' value='Update'/>
        
    </form> 
</div><div class = 'sidebar-column'>   

    <a class = 'button' href = 'index.php'>Return to Home Page</a>
    <a class = 'button' href = 'display-cat.php?cat=<?php echo $catid; ?>'>View</a>      
    <a class = 'button' href = 'remove-cat.php?cat=<?php echo $catid; ?>'>Remove</a><br>

</div>
<br><br><br>

<?php include ("inc/footer.php"); ?>

remove-cat.php

<?php
include ("inc/header.php");

//RETRIEVE RECORD ID FROM QUERY STRING
$catid =  "";
if (isset ($_GET["cat"])){   
    $catid = filter_input(INPUT_GET, "cat", FILTER_SANITIZE_STRING) ;
    $catid = trim ($catid); 
} 

//USE ID TO READ CONTENTS OF RECORD
$catrecord = readDatabaseRecord("cats", $catid); 

//RETRIEVE FORM INPUT 
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {  
    
   if (isset($_POST ['removeflag'])) {            
      $filename = "data/cats/" . $catid . ".txt";   
      $newfilename = "data/removed/" . $catid . ".txt";
      if (file_exists ($filename)) {  
          rename ($filename, $newfilename);               
      }  
   }   
}
?>

<div class = 'content-column'>    
    <?php
    //IF CAT HAS BEEN REMOVED, DISPLAY ERROR MESSAGE
    if (! file_exists ("data/cats/" . $catid . ".txt")) {
        echo "<h4>" . $catid . " has been removed</h4><br>";   
    }
    else {
        //DISPLAY FORM
    ?>
        <form method = 'post' action = 'remove-cat.php?cat=<?php echo $catid; ?>'>

            <h3>Are you sure you want to remove <?php echo $catrecord['name']; ?>?</h3><br>  
            YES <input type = 'checkbox' name = 'removeflag' value = 'REMOVE' />     
            <br><br><input  type = 'submit' name = 'submit' value='Remove'/>

        </form> 
    <?php
    }
    ?>

</div><div class = 'sidebar-column'>
    <a class = 'button' href = 'index.php'>Return to Home Page</a>   
    <?php 
    if (file_exists ("data/cats/" . $catid . ".txt")) {
        echo "<a class = 'button' href = 'display-cat.php?cat=<?php echo $catid; ?>'>View</a><br><br>";
    }
    ?>
    
</div>
<br><br><br>

<?php include ("inc/footer.php"); ?>