FlatFile Database
Menu Form
\n"; // utility string
$nl = "
\n"; // utility string
echo "
\n"; // we'll just keep all this output academic
// file() reads the file into array with one line per element.
$lines = file($dbfile);
$ix= $iy= 0;
foreach ($lines as $line_num => $line) {
if ($dbg1) { echo "Line #{$line_num} : '"; }
if ($line_num == 0) {
if ($dbg1) echo htmlspecialchars((string)$line) . "'
\n";
$line_1 = explode( '|', $line, -1);
if ($dbg2)
{
echo "----------\n";
echo "Here is the first line of the file parsed into the array:
\n";
print_r($line_1);
}
$t_records = trim( $line_1[1] );
$x = trim( $line_1[2] );
if ( $x != $t_members ) {
echo "t_members MISMATCH: file shows $x , t_members= $t_members
Aborting file read
\n";
exit;
}
if ($dbg2)
{
{ echo "t_records= '$t_records' , t_members = '$t_members' \n";}
echo "----------\n";
}
continue;
}
if ($line == "\n") { // bypass empty separator lines.
// echo "GOT A BLANK LINE. $nl";
continue;
}
// str_replace() works ok
// but trim() gets ALL whitespace at both ends and is faster,
$tmp_arr[$ix] = trim((string)$line);
if ($dbg1){ echo htmlspecialchars( $tmp_arr[$ix] ) ."'\n"; }
$ix++;
// add the line to the array
if ( $ix == $t_members ) {
$mtg_list[$iy] = $tmp_arr;
$iy++;
$ix = 0;
}
}
if ($dbg1)
{
echo "----------\n";
echo "Finished reading file into the array. Particulars:
iy = $iy , ix = $ix , and a print_r of \"mtg_list[$t_records][$t_members]\" : \n";
print_r($mtg_list);
echo "----------\n";
echo "So now a manual test print of all the elements of the first record:
----------\n";
foreach ($mtg_list[0] as $ix => $str) {
echo "mtg_list[0][$ix] : " . htmlspecialchars($str) . "\n";
}
echo "----------\n";
}
// Ok so the file is read into the array so now we check to see if we have
// some form submission to handle like add or delete or ????
// OK but before we handle the form we want to see if it is re-submission
// This is what I am working on now the session value check described below.
$form_id = isset($_POST['form_id']) ? $_POST['form_id'] : "";
/*
echo "=========================\n";
if ( isset($_SESSION) ) { echo "_SESSION \n"; print_r($_SESSION); }
if ( isset($_POST) ) { echo "_POST \n"; print_r($_POST); }
*/
echo "=========================\n";
if(isset($_SESSION['form_id']))
{
/*
echo "SESSION[form_id] is set
SESSION[form_id]= ". $_SESSION['form_id'] ."
form_id = ". $form_id ."
POST[form_id] = ". $_POST['form_id'] ."
";
*/
if( strcasecmp( $form_id, $_SESSION["form_id"] ) === 0 )
{
/* Put your form submission code here after processing the form data,
unset the secret key from the session
*/
echo "Processing form data for first time
\n";
// unset($_SESSION["form_id"]);
session_destroy();
$mode = isset($_POST['mode']) ? $_POST['mode'] : "";
if ($mode == "Add")
{
// Make new array with form values.
// Here we have the opportunity to qualify posted data and abort
// before assigning to the main mtg_list array
$tmp_arr[0]= $_POST['name'];
$tmp_arr[1]= $_POST['address'];
$tmp_arr[2]= $_POST['city'];
$tmp_arr[3]= $_POST['state'];
$tmp_arr[4]= $_POST['zip'];
$tmp_arr[5]= $_POST['county'];
// ok now a check for empty members, abort if so
$isbad = 0;
for ($iy = 0; $iy < $t_members; $iy++)
{
//echo "Check tmp_arr[$iy]): \n";
if ( $tmp_arr[$iy] == "") { $isbad++; echo "tmp_arr[$iy] is bad.\n";}
}
if ($isbad)
{
echo "Added record NOT stored. \n",
"You must fill in all the form members \n",
"Try again \n";
}
else //OK gp ahead and store it
{
// If all ok adjust total and add array to mtg_list[]
$t_records++;
$mtg_list[] = $tmp_arr;
// Print acknowedgment
echo "Record added for group $name as follows: \n";
// test print our results
echo "----------\n";
foreach ($tmp_arr as $ia => $str) {
echo "tmp_arr[$ia] = $str \n";
}
echo "----------\n";
if(0) {
echo "mtg_list[$t_records][$t_members] now looks like this:\n";
print_r($mtg_list);
echo "----------\n";
}
// OK no matter what form came in we should probably write
// the data change to the file at this point.
echo "Attempting to open $dbfile. \n";
$fp = fopen( $dbfile, "wb");
if ( $fp )
{
fwrite($fp, $line_1[0]." | $t_records | $t_members |\n");
echo "Writing new data to $dbfile.
\n";
for ( $iy= 0; $iy < $t_records; $iy++ )
{
for ( $ix= 0; $ix < $t_members; $ix++ )
{
fwrite($fp, $mtg_list["$iy"]["$ix"] ." \n");
}
fwrite($fp, "\n");
}
}
else
{
echo "Could not open $dbfile for writing... sorry
You'll have to copy backup file in.
\n";
}
/* Problem! -
After playing with this a little I noticed that if the
user reloads the page after doing an "add" the form data
will be seen and written to the file AGAIN!!! poop!
I see that a session check is the way to go with that.
http://phpsense.com/2006/prevent-duplicate-form-submission/
http://www.bjw.co.nz/developer/general/75-how-to-prevent-form-resubmission
*/
}
}
}
}
/////////////////////////////////////////////
// Ok time to manually print the array
// First with a foreach
/*
echo "Print via foreach \n";
foreach ( $mtg_list as $iy => $ix ) {
echo "Record $iy: \n";
foreach ( $ix as $jy => $jx ) {
echo "mtg_list[$iy][$jy]= $jx \n";
}
}
*/
// Cool now how about a for loop using the file values
// Note how the indexes must be quoted within the echo (pre expansion)
/*
echo "Print via for() loop: \n";
for ( $iy= 0; $iy < $t_records; $iy++ )
{
echo "Record $iy: \n";
for ( $ix= 0; $ix < $t_members; $ix++ )
{
// echo "mtg_list[$iy][$ix]= $mtg_list[\"$iy\"][\"$ix\"] \n";
// or
echo "mtg_list[$iy][$ix]= ". $mtg_list["$iy"]["$ix"] ." \n";
}
}
*/
// OK Now , for real, print it in a table!
echo "Printing All Records Into a Table: (using a for() loop) \n\n";
echo "
\n";
// I did the first row manually to set column widths but use of pre will
// cause width specs to have no effect.
// pre looks better anyhow but I leave this anyhow just in case.
echo "
Columns: |
". $mtg_list["0"]["0"] ." |
". $mtg_list["0"]["1"] ." |
". $mtg_list["0"]["2"] ." |
". $mtg_list["0"]["3"] ." |
". $mtg_list["0"]["4"] ." |
". $mtg_list["0"]["5"] ." |
\n";
for ( $iy= 1; $iy < $t_records; $iy++ )
{
echo " \n";
echo "Record $iy: | \n";
for ( $ix= 0; $ix < $t_members; $ix++ )
{
//echo " [$iy][$ix]= ". $mtg_list["$iy"]["$ix"] ." | \n";
echo " ". $mtg_list["$iy"]["$ix"] ." | \n";
}
echo "
\n";
}
echo "
\n";
echo " \n";
?>