Two files to edit/maintain a data array.
data.php looks something like this:
<?php
$data_array[0]['key1'] = 'value';
$data_array[0]['key2'] = 'value';
$data_array[1]['key1'] = 'value';
$data_array[1]['key2'] = 'value';
$data_array[2]['key1'] = 'value';
$data_array[2]['key2'] = 'value';
?>
admin.php works something like this:
function get_data() {
include 'data.php';
// do a load of stuff with $data_array to create an HTML form
// so that the user can edit it.
return $html_form;
}
function add_record() {
include 'data.php';
// read the POST data and format it into a string
// read $data_array into a string and append that string the POST data string
// write string back to data.php overwriting the contents
return $result;
}
function edit_record() {
// read the POST data and format it into a string (except where a record is marked for deletion)
// write string back to data.php overwriting the contents
return $result;
}
if ($_POST['submit'] == 'add record') {
echo add_record();
}
if ($_POST['submit'] == 'edit record') {
echo edit_record();
}
echo get_data();
When I simply open the page everything works as expected. I can see all of the correct data in the form.
If I edit the data it all loads properly, complete with the new data and deleted records have gone.
However, if I add a new record then $data_array
does not load properly - it loads the last version without the new record. If I look at the file I find that it has been updated. If I reload the page I will see the correct data.
The obvious suspect is the fact that data.php gets loaded twice - I do unset $data_array
before reloading data.php - if I don't reload data.php before running get_data() then $data_array
comes back Undefined index
so I just can't figure out why it isn't loading the array properly after loading data.php for the second time.
Any help greatly appreciated.