Two files to edit/maintain a data array.
data.php looks something like this:


$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.

1 Year
Discussion Span
Last Post by slyme
if (isset($_POST['submit']) && $_POST['submit'] == 'add record') {
    echo add_record();
elseif (isset($_POST['submit']) && $_POST['submit'] == 'edit record') 
    echo edit_record();
    echo get_data();

As you don't post content of add_record function, can't comment too much on it.

Edited by diafol


Thanks for taking a look diafol.

I didn't lay out all of the code hoping that there was an obvious and glaring mistake with the general principle that I had adopted.

So, here's the add function ($the_file is products.php):

function add_product($the_file) {
    $result = "<?php\n";
    $name = htmlentities($_POST['new_name'], ENT_QUOTES, 'UTF-8');
    $unit_price = htmlentities($_POST['new_unit_price'], ENT_QUOTES, 'UTF-8');
    $image = htmlentities($_POST['new_image'], ENT_QUOTES, 'UTF-8');
    $description = htmlentities($_POST['new_description'], ENT_QUOTES, 'UTF-8');
    $result .=  '$' . "product_array[0]['name'] = '$name';\n";
    $result .=  '$' . "product_array[0]['unit_price'] = '$unit_price';\n";
    $result .=  '$' . "product_array[0]['image'] = '$image';\n";
    $result .=  '$' . "product_array[0]['description'] = '$description';\n\n";
    include 'includes/products.php';
    $i = 1;
    foreach ($product_array as $product) {
        $name = $product['name'];
        $unit_price = $product['unit_price'];
        $image = $product['image'];
        $description = $product['description'];
        $result .=  '$product_array[' . $i . "]['name'] = '$name';\n";
        $result .=  '$product_array[' . $i . "]['unit_price'] = '$unit_price';\n";
        $result .=  '$product_array[' . $i . "]['image'] = '$image';\n";
        $result .=  '$product_array[' . $i++ . "]['description'] = '$description';\n\n";
    $result .= "?>\n";
    $file_handle = fopen($the_file, "w") or die("Unable to open file!");
    fwrite($file_handle, $result);
    $_SESSION['added'] = true;
    $add_result = '<div id="add_result">Product added.</div>';
    return $add_result;

Thanks again for any help.


I would store the data as json via json_encode if I was you and json_decode to retrieve the data. Storing and updating in pure PHP is a tricky thing to do. WHat do you think?

You could retrieve and write with file_get_contents and file_put_contents

Another way would be to use XML with DOMDocument or SimpleXML, but I find that tedious.

Edited by diafol


Just a quick note on your recent script. Where did you initialize your $product_array? It doesn't seem to be in the function, so I am guessing it is declared somewhere outside of the function scope? Not sure you could do the "unset" from the inner scope (doesn't make sense in scope-wise)...


The array is created when data.php is included - the array is the data in that file. Yes, that unset was a bit of a long shot.


It just got a bit more odd ...

So, I decided to header('Location: page.php') in the hope that requesting the page anew would solve the problem. It didn't. I had to refresh again (?!) to get the correct result.

Then ... I put it on my external server to show someone what was happening and it worked as expected on that server.

I haven't tried my original approach (including data.php twice) on the external server as I'm confused enough already and the issue is solved in a functional sense - I can hand the page over to the user as is.

But I still want to know what/why/how did this happen (it's definately bothering me that I don't understand this) so I'll leave this open for a bit in case anyone has any pointers.

Thanks to all for your help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.