Jump to content

[SOLVED] How Do I Serialize an Array in a text file?


Dave3765

Recommended Posts

Hey Guys,

 

Grrrrrrrrrrrrrrrrrrrrrrr!

 

I have a text file called tester2.txt that looks like this:

 

array(20) {
  [0]=>
  array(4) {
    ["row0"]=>
    string(14) "Row 0 column 0"
    ["row1"]=>
    string(14) "Row 0 column 1"
  }
  [1]=>
  array(4) {
    ["row0"]=>
    string(14) "Row 1 column 0"
    ["row1"]=>
    string(14) "Row 1 column 1"
  }
  [2]=>
  array(4) {
    ["row0"]=>
    string(14) "Row 2 column 0"
    ["row1"]=>
    string(14) "Row 2 column 1"
  }
  [3]=>
  array(4) {
    ["row0"]=>
    string(14) "Row 3 column 0"
    ["row1"]=>
    string(14) "Row 3 column 1"
  }
  [4]=>
  array(4) {
    ["row0"]=>
    string(14) "Row 4 column 0"
    ["row1"]=>
    string(14) "Row 4 column 1"
  }
  [5]=>
  array(4) {
    ["row0"]=>
    string(14) "Row 5 column 0"
    ["row1"]=>
    string(14) "Row 5 column 1"
  }
  [6]=>
  array(4) {
    ["row0"]=>
    string(14) "Row 6 column 0"
    ["row1"]=>
    string(14) "Row 6 column 1"
  }
  [7]=>
  array(4) {
    ["row0"]=>
    string(14) "Row 7 column 0"
    ["row1"]=>
    string(14) "Row 7 column 1"
  }
  [8]=>
  array(4) {
    ["row0"]=>
    string(14) "Row 8 column 0"
    ["row1"]=>
    string(14) "Row 8 column 1"
  }
  [9]=>
  array(4) {
    ["row0"]=>
    string(14) "Row 9 column 0"
    ["row1"]=>
    string(14) "Row 9 column 1"
  }
  [10]=>
  array(4) {
    ["row0"]=>
    string(15) "Row 10 column 0"
    ["row1"]=>
    string(15) "Row 10 column 1"
  }
}

 

I need to open the text file, serialize the values and save it - here is how I am trying to do that (but it won't work!  >:():

 

<?php

$data = file_get_contents("./tester2.txt");

$open = fopen("./tester2.txt", 'w');
fwrite($open, serialize($data));
fclose($open);

?>

 

All it does it serialize the whole text file as 1 item, but I need to serialize the individual values.

 

Any ideas?

Link to comment
Share on other sites

It was the flat database file that I got as part of a script. It originally looked like this:

 

a:20:{i:0;a:4:{s:4:"row0";s:14:"Row 0 column 0";s:4:"row1";s:14:"Row 0 column 1";s:4:"row2";s:14:"Row 0 column 2";s:4:"row3";s:14:"Row 0 column 3";}i:1;a:4:{s:4:"row0";s:14:"Row 1 column 0";s:4:"row1";s:14:"Row 1 column 1";s:4:"row2";s:14:"Row 1 column 2";s:4:"row3";s:14:"Row 1 column 3";}i:2;a:4:{s:4:"row0";s:14:"Row 2 column 0";s:4:"row1";s:14:"Row 2 column 1";s:4:"row2";s:14:"Row 2 column 2";s:4:"row3";s:14:"Row 2 column 3";}i:3;a:4:{s:4:"row0";s:14:"Row 3 column 0";s:4:"row1";s:14:"Row 3 column 1";s:4:"row2";s:14:"Row 3 column 2";s:4:"row3";s:14:"Row 3 column 3";}i:4;a:4:{s:4:"row0";s:14:"Row 4 column 0";s:4:"row1";s:14:"Row 4 column 1";s:4:"row2";s:14:"Row 4 column 2";s:4:"row3";s:14:"Row 4 column 3";}i:5;a:4:{s:4:"row0";s:14:"Row 5 column 0";s:4:"row1";s:14:"Row 5 column 1";s:4:"row2";s:14:"Row 5 column 2";s:4:"row3";s:14:"Row 5 column 3";}i:6;a:4:{s:4:"row0";s:14:"Row 6 column 0";s:4:"row1";s:14:"Row 6 column 1";s:4:"row2";s:14:"Row 6 column 2";s:4:"row3";s:14:"Row 6 column 3";}i:7;a:4:{s:4:"row0";s:14:"Row 7 column 0";s:4:"row1";s:14:"Row 7 column 1";s:4:"row2";s:14:"Row 7 column 2";s:4:"row3";s:14:"Row 7 column 3";}i:8;a:4:{s:4:"row0";s:14:"Row 8 column 0";s:4:"row1";s:14:"Row 8 column 1";s:4:"row2";s:14:"Row 8 column 2";s:4:"row3";s:14:"Row 8 column 3";}i:9;a:4:{s:4:"row0";s:14:"Row 9 column 0";s:4:"row1";s:14:"Row 9 column 1";s:4:"row2";s:14:"Row 9 column 2";s:4:"row3";s:14:"Row 9 column 3";}i:10;a:4:{s:4:"row0";s:15:"Row 10 column 0";s:4:"row1";s:15:"Row 10 column 1";s:4:"row2";s:15:"Row 10 column 2";s:4:"row3";s:15:"Row 10 column 3";}i:11;a:4:{s:4:"row0";s:15:"Row 11 column 0";s:4:"row1";s:15:"Row 11 column 1";s:4:"row2";s:15:"Row 11 column 2";s:4:"row3";s:15:"Row 11 column 3";}i:12;a:4:{s:4:"row0";s:15:"Row 12 column 0";s:4:"row1";s:15:"Row 12 column 1";s:4:"row2";s:15:"Row 12 column 2";s:4:"row3";s:15:"Row 12 column 3";}i:13;a:4:{s:4:"row0";s:15:"Row 13 column 0";s:4:"row1";s:15:"Row 13 column 1";s:4:"row2";s:15:"Row 13 column 2";s:4:"row3";s:15:"Row 13 column 3";}i:14;a:4:{s:4:"row0";s:15:"Row 14 column 0";s:4:"row1";s:15:"Row 14 column 1";s:4:"row2";s:15:"Row 14 column 2";s:4:"row3";s:15:"Row 14 column 3";}i:15;a:4:{s:4:"row0";s:15:"Row 15 column 0";s:4:"row1";s:15:"Row 15 column 1";s:4:"row2";s:15:"Row 15 column 2";s:4:"row3";s:15:"Row 15 column 3";}i:16;a:4:{s:4:"row0";s:15:"Row 16 column 0";s:4:"row1";s:15:"Row 16 column 1";s:4:"row2";s:15:"Row 16 column 2";s:4:"row3";s:15:"Row 16 column 3";}i:17;a:4:{s:4:"row0";s:15:"Row 17 column 0";s:4:"row1";s:15:"Row 17 column 1";s:4:"row2";s:15:"Row 17 column 2";s:4:"row3";s:15:"Row 17 column 3";}i:18;a:4:{s:4:"row0";s:15:"Row 18 column 0";s:4:"row1";s:15:"Row 18 column 1";s:4:"row2";s:15:"Row 18 column 2";s:4:"row3";s:15:"Row 18 column 3";}i:19;a:4:{s:4:"row0";s:15:"Row 19 column 0";s:4:"row1";s:15:"Row 19 column 1";s:4:"row2";s:15:"Row 19 column 2";s:4:"row3";s:15:"Row 19 column 3";}}

 

And I used this site to unserialize it - I am trying to change the values and then re-serialize it.

 

If you are interested the script is an ajax html table editor.

Link to comment
Share on other sites

Use PHP's built in unserialize function on that string. Eg:

 

<?php

$data = 'a:20:{i:0;a:4:{s:4:"row0";s:14:"Row 0 column 0";s:4:"row1";s:14:"Row 0 column 1";s:4:"row2";s:14:"Row 0 column 2";s:4:"row3";s:14:"Row 0 column 3";}i:1;a:4:{s:4:"row0";s:14:"Row 1 column 0";s:4:"row1";s:14:"Row 1 column 1";s:4:"row2";s:14:"Row 1 column 2";s:4:"row3";s:14:"Row 1 column 3";}i:2;a:4:{s:4:"row0";s:14:"Row 2 column 0";s:4:"row1";s:14:"Row 2 column 1";s:4:"row2";s:14:"Row 2 column 2";s:4:"row3";s:14:"Row 2 column 3";}i:3;a:4:{s:4:"row0";s:14:"Row 3 column 0";s:4:"row1";s:14:"Row 3 column 1";s:4:"row2";s:14:"Row 3 column 2";s:4:"row3";s:14:"Row 3 column 3";}i:4;a:4:{s:4:"row0";s:14:"Row 4 column 0";s:4:"row1";s:14:"Row 4 column 1";s:4:"row2";s:14:"Row 4 column 2";s:4:"row3";s:14:"Row 4 column 3";}i:5;a:4:{s:4:"row0";s:14:"Row 5 column 0";s:4:"row1";s:14:"Row 5 column 1";s:4:"row2";s:14:"Row 5 column 2";s:4:"row3";s:14:"Row 5 column 3";}i:6;a:4:{s:4:"row0";s:14:"Row 6 column 0";s:4:"row1";s:14:"Row 6 column 1";s:4:"row2";s:14:"Row 6 column 2";s:4:"row3";s:14:"Row 6 column 3";}i:7;a:4:{s:4:"row0";s:14:"Row 7 column 0";s:4:"row1";s:14:"Row 7 column 1";s:4:"row2";s:14:"Row 7 column 2";s:4:"row3";s:14:"Row 7 column 3";}i:8;a:4:{s:4:"row0";s:14:"Row 8 column 0";s:4:"row1";s:14:"Row 8 column 1";s:4:"row2";s:14:"Row 8 column 2";s:4:"row3";s:14:"Row 8 column 3";}i:9;a:4:{s:4:"row0";s:14:"Row 9 column 0";s:4:"row1";s:14:"Row 9 column 1";s:4:"row2";s:14:"Row 9 column 2";s:4:"row3";s:14:"Row 9 column 3";}i:10;a:4:{s:4:"row0";s:15:"Row 10 column 0";s:4:"row1";s:15:"Row 10 column 1";s:4:"row2";s:15:"Row 10 column 2";s:4:"row3";s:15:"Row 10 column 3";}i:11;a:4:{s:4:"row0";s:15:"Row 11 column 0";s:4:"row1";s:15:"Row 11 column 1";s:4:"row2";s:15:"Row 11 column 2";s:4:"row3";s:15:"Row 11 column 3";}i:12;a:4:{s:4:"row0";s:15:"Row 12 column 0";s:4:"row1";s:15:"Row 12 column 1";s:4:"row2";s:15:"Row 12 column 2";s:4:"row3";s:15:"Row 12 column 3";}i:13;a:4:{s:4:"row0";s:15:"Row 13 column 0";s:4:"row1";s:15:"Row 13 column 1";s:4:"row2";s:15:"Row 13 column 2";s:4:"row3";s:15:"Row 13 column 3";}i:14;a:4:{s:4:"row0";s:15:"Row 14 column 0";s:4:"row1";s:15:"Row 14 column 1";s:4:"row2";s:15:"Row 14 column 2";s:4:"row3";s:15:"Row 14 column 3";}i:15;a:4:{s:4:"row0";s:15:"Row 15 column 0";s:4:"row1";s:15:"Row 15 column 1";s:4:"row2";s:15:"Row 15 column 2";s:4:"row3";s:15:"Row 15 column 3";}i:16;a:4:{s:4:"row0";s:15:"Row 16 column 0";s:4:"row1";s:15:"Row 16 column 1";s:4:"row2";s:15:"Row 16 column 2";s:4:"row3";s:15:"Row 16 column 3";}i:17;a:4:{s:4:"row0";s:15:"Row 17 column 0";s:4:"row1";s:15:"Row 17 column 1";s:4:"row2";s:15:"Row 17 column 2";s:4:"row3";s:15:"Row 17 column 3";}i:18;a:4:{s:4:"row0";s:15:"Row 18 column 0";s:4:"row1";s:15:"Row 18 column 1";s:4:"row2";s:15:"Row 18 column 2";s:4:"row3";s:15:"Row 18 column 3";}i:19;a:4:{s:4:"row0";s:15:"Row 19 column 0";s:4:"row1";s:15:"Row 19 column 1";s:4:"row2";s:15:"Row 19 column 2";s:4:"row3";s:15:"Row 19 column 3";}}';

$data_array = unserialize($data);

echo '<pre>' . print_r($data_array, true) . '</pre>';

?>

 

You can now modify the array's values how you want them. Once done you can re-serialize with serialize

Link to comment
Share on other sites

How would I 're-serialize with serialize'?

 

When I try and do it I just get this:

 

s:1243:"Array
(
    [0] => Array
        (
            [row0] => Row 0 column 0
            [row1] => Row 0 column 1
        )

    [1] => Array
        (
            [row0] => Row 1 column 0
            [row1] => Row 1 column 1
        )

    [2] => Array
        (
            [row0] => Row 2 column 0
            [row1] => Row 2 column 1
        )

    [3] => Array
        (
            [row0] => Row 3 column 0
            [row1] => Row 3 column 1
        )

    [4] => Array
        (
            [row0] => Row 4 column 0
            [row1] => Row 4 column 1
        )

    [5] => Array
        (
            [row0] => Row 5 column 0
            [row1] => Row 5 column 1
        )

    [6] => Array
        (
            [row0] => Row 6 column 0
            [row1] => Row 6 column 1
        )

    [7] => Array
        (
            [row0] => Row 7 column 0
            [row1] => Row 7 column 1
        )

    [8] => Array
        (
            [row0] => Row 8 column 0
            [row1] => Row 8 column 1
        )

    [9] => Array
        (
            [row0] => Row 9 column 0
            [row1] => Row 9 column 1
        )

    [10] => Array
        (
            [row0] => Row 10 column 0
            [row1] => Row 10 column 1
        )
)";

 

You see it's just serializing the whole thing.

Link to comment
Share on other sites

You use serialize() on the $data_array variable. You don't write the contents of the variable to a file then serialize the contents of that file. If you do that then the data is no longer an array and is considered a string!

 

What are you trying to do to the original serialized array.

Link to comment
Share on other sites

I am trying to get the original text file:

 

a:20:{i:0;a:4:{s:4:"row0";s:14:"Row 0 column 0";s:4:"row1";s:14:"Row 0 column 1";s:4:"row2";s:14:"Row 0 column 2";s:4:"row3";s:14:"Row 0 column 3";}i:1;a:4:{s:4:"row0";s:14:"Row 1 column 0";s:4:"row1";s:14:"Row 1 column 1";s:4:"row2";s:14:"Row 1 column 2";s:4:"row3";s:14:"Row 1 column 3";}i:2;a:4:{s:4:"row0";s:14:"Row 2 column 0";s:4:"row1";s:14:"Row 2 column 1";s:4:"row2";s:14:"Row 2 column 2";s:4:"row3";s:14:"Row 2 column 3";}i:3;a:4:{s:4:"row0";s:14:"Row 3 column 0";s:4:"row1";s:14:"Row 3 column 1";s:4:"row2";s:14:"Row 3 column 2";s:4:"row3";s:14:"Row 3 column 3";}i:4;a:4:{s:4:"row0";s:14:"Row 4 column 0";s:4:"row1";s:14:"Row 4 column 1";s:4:"row2";s:14:"Row 4 column 2";s:4:"row3";s:14:"Row 4 column 3";}i:5;a:4:{s:4:"row0";s:14:"Row 5 column 0";s:4:"row1";s:14:"Row 5 column 1";s:4:"row2";s:14:"Row 5 column 2";s:4:"row3";s:14:"Row 5 column 3";}i:6;a:4:{s:4:"row0";s:14:"Row 6 column 0";s:4:"row1";s:14:"Row 6 column 1";s:4:"row2";s:14:"Row 6 column 2";s:4:"row3";s:14:"Row 6 column 3";}i:7;a:4:{s:4:"row0";s:14:"Row 7 column 0";s:4:"row1";s:14:"Row 7 column 1";s:4:"row2";s:14:"Row 7 column 2";s:4:"row3";s:14:"Row 7 column 3";}i:8;a:4:{s:4:"row0";s:14:"Row 8 column 0";s:4:"row1";s:14:"Row 8 column 1";s:4:"row2";s:14:"Row 8 column 2";s:4:"row3";s:14:"Row 8 column 3";}i:9;a:4:{s:4:"row0";s:14:"Row 9 column 0";s:4:"row1";s:14:"Row 9 column 1";s:4:"row2";s:14:"Row 9 column 2";s:4:"row3";s:14:"Row 9 column 3";}i:10;a:4:{s:4:"row0";s:15:"Row 10 column 0";s:4:"row1";s:15:"Row 10 column 1";s:4:"row2";s:15:"Row 10 column 2";s:4:"row3";s:15:"Row 10 column 3";}i:11;a:4:{s:4:"row0";s:15:"Row 11 column 0";s:4:"row1";s:15:"Row 11 column 1";s:4:"row2";s:15:"Row 11 column 2";s:4:"row3";s:15:"Row 11 column 3";}i:12;a:4:{s:4:"row0";s:15:"Row 12 column 0";s:4:"row1";s:15:"Row 12 column 1";s:4:"row2";s:15:"Row 12 column 2";s:4:"row3";s:15:"Row 12 column 3";}i:13;a:4:{s:4:"row0";s:15:"Row 13 column 0";s:4:"row1";s:15:"Row 13 column 1";s:4:"row2";s:15:"Row 13 column 2";s:4:"row3";s:15:"Row 13 column 3";}i:14;a:4:{s:4:"row0";s:15:"Row 14 column 0";s:4:"row1";s:15:"Row 14 column 1";s:4:"row2";s:15:"Row 14 column 2";s:4:"row3";s:15:"Row 14 column 3";}i:15;a:4:{s:4:"row0";s:15:"Row 15 column 0";s:4:"row1";s:15:"Row 15 column 1";s:4:"row2";s:15:"Row 15 column 2";s:4:"row3";s:15:"Row 15 column 3";}i:16;a:4:{s:4:"row0";s:15:"Row 16 column 0";s:4:"row1";s:15:"Row 16 column 1";s:4:"row2";s:15:"Row 16 column 2";s:4:"row3";s:15:"Row 16 column 3";}i:17;a:4:{s:4:"row0";s:15:"Row 17 column 0";s:4:"row1";s:15:"Row 17 column 1";s:4:"row2";s:15:"Row 17 column 2";s:4:"row3";s:15:"Row 17 column 3";}i:18;a:4:{s:4:"row0";s:15:"Row 18 column 0";s:4:"row1";s:15:"Row 18 column 1";s:4:"row2";s:15:"Row 18 column 2";s:4:"row3";s:15:"Row 18 column 3";}i:19;a:4:{s:4:"row0";s:15:"Row 19 column 0";s:4:"row1";s:15:"Row 19 column 1";s:4:"row2";s:15:"Row 19 column 2";s:4:"row3";s:15:"Row 19 column 3";}}

 

to unserialize to this (which I can do):

 

Array
(
    [0] => Array
        (
            [row0] => Row 0 column 0
            [row1] => Row 0 column 1
            [row2] => Row 0 column 2
            [row3] => Row 0 column 3
        )

    [1] => Array
        (
            [row0] => Row 1 column 0
            [row1] => Row 1 column 1
            [row2] => Row 1 column 2
            [row3] => Row 1 column 3
        )

    [2] => Array
        (
            [row0] => Row 2 column 0
            [row1] => Row 2 column 1
            [row2] => Row 2 column 2
            [row3] => Row 2 column 3
        )

    [3] => Array
        (
            [row0] => Row 3 column 0
            [row1] => Row 3 column 1
            [row2] => Row 3 column 2
            [row3] => Row 3 column 3
        )

    [4] => Array
        (
            [row0] => Row 4 column 0
            [row1] => Row 4 column 1
            [row2] => Row 4 column 2
            [row3] => Row 4 column 3
        )

    [5] => Array
        (
            [row0] => Row 5 column 0
            [row1] => Row 5 column 1
            [row2] => Row 5 column 2
            [row3] => Row 5 column 3
        )

    [6] => Array
        (
            [row0] => Row 6 column 0
            [row1] => Row 6 column 1
            [row2] => Row 6 column 2
            [row3] => Row 6 column 3
        )

    [7] => Array
        (
            [row0] => Row 7 column 0
            [row1] => Row 7 column 1
            [row2] => Row 7 column 2
            [row3] => Row 7 column 3
        )

    [8] => Array
        (
            [row0] => Row 8 column 0
            [row1] => Row 8 column 1
            [row2] => Row 8 column 2
            [row3] => Row 8 column 3
        )

    [9] => Array
        (
            [row0] => Row 9 column 0
            [row1] => Row 9 column 1
            [row2] => Row 9 column 2
            [row3] => Row 9 column 3
        )

    [10] => Array
        (
            [row0] => Row 10 column 0
            [row1] => Row 10 column 1
            [row2] => Row 10 column 2
            [row3] => Row 10 column 3
        )

    [11] => Array
        (
            [row0] => Row 11 column 0
            [row1] => Row 11 column 1
            [row2] => Row 11 column 2
            [row3] => Row 11 column 3
        )

    [12] => Array
        (
            [row0] => Row 12 column 0
            [row1] => Row 12 column 1
            [row2] => Row 12 column 2
            [row3] => Row 12 column 3
        )

    [13] => Array
        (
            [row0] => Row 13 column 0
            [row1] => Row 13 column 1
            [row2] => Row 13 column 2
            [row3] => Row 13 column 3
        )

    [14] => Array
        (
            [row0] => Row 14 column 0
            [row1] => Row 14 column 1
            [row2] => Row 14 column 2
            [row3] => Row 14 column 3
        )

    [15] => Array
        (
            [row0] => Row 15 column 0
            [row1] => Row 15 column 1
            [row2] => Row 15 column 2
            [row3] => Row 15 column 3
        )

    [16] => Array
        (
            [row0] => Row 16 column 0
            [row1] => Row 16 column 1
            [row2] => Row 16 column 2
            [row3] => Row 16 column 3
        )

    [17] => Array
        (
            [row0] => Row 17 column 0
            [row1] => Row 17 column 1
            [row2] => Row 17 column 2
            [row3] => Row 17 column 3
        )

    [18] => Array
        (
            [row0] => Row 18 column 0
            [row1] => Row 18 column 1
            [row2] => Row 18 column 2
            [row3] => Row 18 column 3
        )

    [19] => Array
        (
            [row0] => Row 19 column 0
            [row1] => Row 19 column 1
            [row2] => Row 19 column 2
            [row3] => Row 19 column 3
        )

)

 

then to change it to this:

 

Array
(
    [0] => Array
        (
            [row0] => Row 0 column 0
            [row1] => Row 0 column 1
        )

    [1] => Array
        (
            [row0] => Row 1 column 0
            [row1] => Row 1 column 1
        )

    [2] => Array
        (
            [row0] => Row 2 column 0
            [row1] => Row 2 column 1
        )

    [3] => Array
        (
            [row0] => Row 3 column 0
            [row1] => Row 3 column 1
        )

    [4] => Array
        (
            [row0] => Row 4 column 0
            [row1] => Row 4 column 1
        )

    [5] => Array
        (
            [row0] => Row 5 column 0
            [row1] => Row 5 column 1
        )

    [6] => Array
        (
            [row0] => Row 6 column 0
            [row1] => Row 6 column 1
        )

    [7] => Array
        (
            [row0] => Row 7 column 0
            [row1] => Row 7 column 1
        )

    [8] => Array
        (
            [row0] => Row 8 column 0
            [row1] => Row 8 column 1
        )

    [9] => Array
        (
            [row0] => Row 9 column 0
            [row1] => Row 9 column 1
        )

    [10] => Array
        (
            [row0] => Row 10 column 0
            [row1] => Row 10 column 1
        )
)

 

and then re-serialize it. It's the re-serializing as an array so that it looks similar to the original that's got me stuck.  ???

 

 

Link to comment
Share on other sites

Nope.

 

The only instruction it gives is this:

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

The data contains a multidimensional array that consists of the data for the table.

It should be in the following format:

In the primary array, each element should have a key eqaul to the row id and the value is

a sub array that contains column data.

 

The column data subarray should have key values that are the column values from the db and the values

are the data to be displayed.

 

An example would be:

[1] => array(

[Name] => 'Some Guy'

[Address] => '123 Some Street'

[Phone] => '123-123-1234'

)

[2] => array(

[Name] => 'Some Girl'

[Address] => '321 Some Drive'

[Phone] => '987-987-9876'

}

etc.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

Now that doesn't help me because I still need to re-serialize it... don't I?

Link to comment
Share on other sites

In case it helps, here is the file of the script that does the serializing and un-serializing:

 

<?php
//ini_set("display_errors", "1");
//ini_set("error_reporting", "E_ALL");


include("./AjaxTableEditor.class.php");
$at = new TableEditor;

if (isset($_POST['new'])) {
//You can choose to handle the data processing part of your script
//this way, where it is all in one page.
//Just make sure that you either return nothing on success,
//or you echo out "true", otherwise it will error

extract($_POST);

//the script returns three values:
//$rowid: the id for the row
//$colid: the value for the column
//$new:   the value that was typed into the text input

$data = file_get_contents("./tester.txt");
$data = unserialize($data);

$data[$rowid][$colid] = $new;

//if this were a database, you could use the following SQL statement:
//UPDATE $tablename SET `$colid` = '$new' WHERE id = '$rowid'
//you should do some verification on the data input by the user.

$open = fopen("./tester.txt", 'w');
fwrite($open, serialize($data));
fclose($open);

} else {
//the display properties for the odd and even rows
$odd = array('style' => 'background-color: #CCCCCC;');
$even = array('style' => 'background-color: #EEEEEE;');

//the display properties for the overall table
$table = array('align' => 'center', 'cellpadding' => '3', 'cellspacing' => '0', 'width' => '50%');

//table column header information
$headers = array("Supplier Name", "Website");
$headerattrib = array('style' => 'background-color: skyblue');

$at->SetEvenRowAttribs($even);
$at->SetOddRowAttribs($odd);

$at->SetTableAttribs($table);

$at->SetHeaderAttribs($headerattrib);
$at->SetHeaders($headers);

$at->SetSubmit("example.php");

$data = file_get_contents("./tester.txt");
$data = unserialize($data);

$at->SetData($data);

echo $at->GenerateTable();

//echo "<pre>";
//print_r($data);
}


/*

//comment out everything above, and uncomment this to rebuild the sample data

for ($a = 0; $a < 20; $a++) {
for ($b = 0; $b < 4; $b++) {
	$n[$a]["row" . $b] = "Row $a column $b";
}
}

$open = fopen("./tester.txt", 'w');
fwrite($open, serialize($n));
fclose($open);

*/

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.