It's not exactly a simple job, but the best functions to use would probably be fgetcsv and fputcsv. You'll need to ignore the header of the file and then read the file into an array, and provide edit buttons to identify where in the file the row is located. This is a very quick 'n' dirty example, no security or file locking here.
PHP Code:
<?php
$file = 'employees.txt'; // make sure this points to your file
if(count($_POST) > 0) {
$rows = array();
$rows[] = array( // add header row
'ID',
'FIRSTNAME',
'LASTNAME',
'AGE'
);
// read in new structure
$size = count($_POST['ID']);
for($i = 0; $i < $size; $i++) {
if(!isset($_POST['delete' . $i])) { // we don't want deleted entries
$rows[] = array(
$_POST['ID'][$i],
$_POST['firstname'][$i],
$_POST['lastname'][$i],
$_POST['age'][$i]
);
}
}
$fp = fopen($file, 'w'); // open the file for writing, truncate to zero length
foreach($rows as $row) {
fputcsv($fp, $row, ' '); // write each row
}
fclose($fp);
}
// open a file pointer as read only
$fp = fopen($file, 'r');
$rows = array();
// ignore header
fgets($fp);
while(!feof($fp)) {
$rows[] = fgetcsv($fp, 0, ' '); // last parameter is delimter
}
$html = <<< EOH
<form method="post">
<table>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th colspan="2"></th>
</tr>
EOH;
foreach($rows as $key => $row) {
$html .= <<< EOH
<tr>
<td>
<input type="hidden" name="row[]" value="{$key}"/>
<input type="text" name="ID[]" value="{$row[0]}"/>
</td>
<td>
<input type="text" name="firstname[]" value="{$row[1]}"/>
</td>
<td>
<input type="text" name="lastname[]" value="{$row[2]}"/>
</td>
<td>
<input type="text" name="age[]" value="{$row[3]}"/>
</td>
<td>
<input type="submit" name="delete{$key}" value="delete"/>
</td>
<td>
<input type="submit" name="edit{$key}" value="edit"/>
</td>
</tr>
EOH;
}
fclose($fp);
$html .= "</table></form>";
echo $html;
?>
I've not test the above, as I don't have anything to run it on available to me at the moment, but I think it should work.
