dynamic_routines_php

    1| <?php
    2| //Reusable php functions
    3| 
    4| //Determines if file is on disk
    5| function file_lives($fileID)
    6| {
    7|     $exists = true;
    8| 
    9|     if(!file_exists($fileID))
  10|     {
  11|         $exists = false;
  12|     }
  13| 
  14|     return $exists;
  15| 
  16| }
  17| 
  18| //Determines if file can be read
  19| function file_is_readable($fileID)
  20| {
  21|     $isReadable = true;
  22| 
  23|     if(!is_readable($fileID))
  24|     {
  25|         $isReadable = false;
  26|     }
  27| 
  28|     return $isReadable;
  29| }
  30| 
  31| //Display an error page
  32| function error_page($heading, $message)
  33| {
  34|     echo "<html>";
  35|     echo "<head>";
  36|     echo "<title>Error Page</title>";
  37|     echo "<style type='text/css'>";
  38|     echo "body {background-color: red; color: white}";
  39|     echo "</style>";
  40|     echo "</head>";
  41|     echo "<body>";
  42|     echo "<h1>$heading</h1>";
  43|     echo "<h2>$message</h2>";
  44|     echo "</body>";
  45|     echo "</html>";
  46| }
  47| 
  48| //Open a file
  49| function open_a_file($fileID, $how)
  50| {
  51|     $fHandle = fopen($fileID, $how);
  52| 
  53|     return $fHandle;
  54| }
  55| 
  56| //read a record
  57| function read_file_by_record($fHandle)
  58| {
  59|     $allRecords = array();
  60| 
  61|     //Read to end of file
  62|     while(!feof($fHandle))
  63|     {
  64|         //read one record
  65|         $record = fgets($fHandle);
  66| 
  67|         //remove the \n character
  68|         $record = trim($record);
  69| 
  70|         //Add a record to the end of an array
  71|         array_push($allRecords, $record);
  72|     }
  73| 
  74|     return $allRecords;
  75| }
  76| 
  77| //Close a file
  78| function close_a_file($fileID)
  79| {
  80|     fclose($fileID);
  81| 
  82|     return $fHandle;
  83| }
  84| 
  85| //validate records
  86| function validate_records($all_records, $len, $symbol, $position)
  87| {
  88|     $goodRecords = array();
  89| 
  90|     foreach($all_records as $record)
  91|     {
  92|         //valid record length
  93|         if(strlen($record) > $len - 1)
  94|         {
  95|             $pos = stripos($record, $symbol, 0);
  96| 
  97|             //Is the symbol in the right postion
  98|             if($pos == $position - 1)
  99|             {
100|                 //Add a valid record to the array
101|                 array_push($goodRecords, $record);
102|             }
103|         }
104|     }
105| 
106|     return $goodRecords;
107| }
108| 
109| //retrieve pure data
110| function retrieve_data_only($all_records, $symbol, $field_index)
111| {
112|     $just_data = array();
113| 
114|     foreach($all_records as $record)
115|     {
116|         $fields = explode("$symbol", $record);
117| 
118|         array_push($just_data, $fields[$field_index]);
119|     }
120| 
121|     return $just_data;
122| }
123| 
124| //add a record to a data file
125| function add_record_to_file($sortkey, $content, $fileid, $symbol)
126| {
127|     $written = true;
128|     $bytes_written = 0;
129| 
130|     $record = $sortkey.$symbol.$content."\n";
131| 
132|     $fHandle = open_a_file($fileid, "a");
133| 
134|     if($fHandle)
135|     {v 136|         $bytes_written = fwrite($fHandle, $record);
137| 
138|         fclose($fHandle);
139| 
140|         //was record written
141|         if($bytes_written < 1)
142|         {
143|             $written = false;
144| 
145|         }
146|     }
147|     else
148|     {
149|         $written = false;
150|     }
151| 
152|     return $written;
153| }
154| 
155| function page_selection()
156| {
157| 
158|     echo "<html>
159|     <head>
160|     <title>Dynamic File Maintenance</title>
161|     </head>
162|     <body>
163|     <h1>Dynamic File Maintenance</h1>
164|     <h3>Please select one of the following:</h3>
165| 
166|     <a href='dynamic_add_form.html'>Add Records</a><br/><br/>
167| 
168|     <a href='dynamic_delete_form.php'>Delete Records</a><br/><br/>
169| 
170|     <a href='dynamic.html'>Home</a>
171| 
172|     </body>
173|     </html>";
174| 
175| }
176| 
177| //function to delete selected record
178| function rewrite_delete_data_file($fileid, $all_records, $record_number)
179| {
180|     $deleted = true;
181| 
182|     $fHandle = open_a_file($fileid, "w");
183| 
184|     //Did file open OK?
185|     if(!$fHandle)
186|     {
187|         $deleted = false;
188|     }
189|     else
190|     {
191|         $count_records = count($all_records);
192| 
193|         //read each record and delete the selecetd one
194|         //the count function starts at '1' not '0'
195| 
196|         $record = "";
197| 
198|         for($x = 0; $x < $count_records; ++$x)
199|         {
200|             if($x != $record_number)
201|             {
202|                 $record .= $all_records[$x]."\n";
203|             }
204| 
205|         }
206| 
207|         //cut last '\n' off
208|         $lastpos = strripos($record,"\n");
209|         $record = substr($record,0,$lastpos);
210| 
211|         //finally write to file
212|         fwrite($fHandle,$record);
213| 
214|         fclose($fHandle);
215|     }
216| 
217|     //change the file rights on the new file
218|     chmod($fileid, 0777);
219| 
220|     return $deleted;
221| }
222| 
223| ?>