0

I have a .txt file where I have list of Windows 8 apps, written in JSON, that is being used in an Windows 8 app. I want to be able to have an online form, where I can input a new app, and then it puts it in the JSON .txt file. It Works find with the PHP file_put_contents and the JSON_encode, but everytime it inserts a new app, it inserts it in a new root [], instead of inserting it in the already made root. Another thing is that the pretty_printing function is not working, somebody who can help me make the JSON input look good?

if(isset($_POST['send'])){
    $bi = $_POST['background_image'];
    $ad = $_POST['description'];
    $ei = $_POST['extra-images'];
    $group = $_POST['group'];
    $tr = $_POST['teacherReview'];
    $rating = $_POST['rating'];
    $st = $_POST['shortTitle'];
    $ti = $_POST['tileImage'];
    $title = $_POST['title'];
    $vc = $_POST['verify'];
    $savePath = "/var/www/kthorndahl.com/public_html/windowsstoreData/Apps.txt";

    if ($group == "ms"){
        $gbi = "http://kthorndahl.com/windowsstoreData/images/math/geogebra.png";
        $gd = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ";
        $gi = "http://kthorndahl.com/windowsstoreData/images/math/geogebra.png";
        $gk = "Math and Science";
        $gst = "Math and Science";
        $gt = "Math and Science";
    }
    else{
        $gbi = "http://kthorndahl.com/windowsstoreData/images/math/geogebra.png";
        $gd = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ";
        $gi = "http://kthorndahl.com/windowsstoreData/images/math/geogebra.png";
        $gk = "Human Science";
        $gst = "Human Science";
        $gt = "Human Science";
    }

    $content = array();

    if($vc == "Thisisaverysecretpassword123456789"){
        //get the old data

        $content = json_decode( file_get_contents( $savePath ) , true );
        //add the new data
        $content[] = array( 
            'backgroundImage' => $bi ,
            'description' => $ad,
            'extraImages' => $ei,
            'group' => array('backgroundImage' => $gbi, 'description' => $gd, 'groupImage' => $gi, 'key' => $gk, 'shortTitle' => $gst, 'title' => $gt,),
            'teacherReview' => $tr,
            'rating' => $rating,
            'shortTitle' => $st,
            'tileImage' => $ti,
            'title' => $title
            );

        //decode the new data
        $content = json_encode( $content);
        file_put_contents( $savePath , $content , FILE_APPEND );
    }
}

UPDATE: My current output:

[{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Nu tester jeg lige igen","extraImages":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","group":{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ","groupImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","key":"Math and Science","shortTitle":"Math and Science","title":"Math and Science"},"teacherReview":null,"rating":"9","shortTitle":"Test 2","tileImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","title":"Test 2"}]
[{"backgroundImage":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","description":"Tester igen","extraImages":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","group":{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ","groupImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","key":"Math and Science","shortTitle":"Math and Science","title":"Math and Science"},"teacherReview":"stadig nice","rating":"3","shortTitle":"Test 3","tileImage":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","title":"Test 3"}]

The output that I want:

[{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Nu tester jeg lige igen","extraImages":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","group":{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ","groupImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","key":"Math and Science","shortTitle":"Math and Science","title":"Math and Science"},"teacherReview":null,"rating":"9","shortTitle":"Test 2","tileImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","title":"Test 2"},
    {"backgroundImage":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","description":"Tester igen","extraImages":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","group":{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ","groupImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","key":"Math and Science","shortTitle":"Math and Science","title":"Math and Science"},"teacherReview":"stadig nice","rating":"3","shortTitle":"Test 3","tileImage":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","title":"Test 3"}]

1 Answers1

1

Don't use FILE_APPEND. You're already appending the new entry to $content, so you need to rewrite the file completely, not add the new array after the old one.

    $content = json_decode( file_get_contents( $savePath ) , true );
    //add the new data
    $content[] = array( 
        'backgroundImage' => $bi ,
        'description' => $ad,
        'extraImages' => $ei,
        'group' => array('backgroundImage' => $gbi, 'description' => $gd, 'groupImage' => $gi, 'key' => $gk, 'shortTitle' => $gst, 'title' => $gt,),
        'teacherReview' => $tr,
        'rating' => $rating,
        'shortTitle' => $st,
        'tileImage' => $ti,
        'title' => $title
        );

    //encode the new data
    $content_json = json_encode($content);
    file_put_contents( $savePath , $content_json );
Barmar
  • 741,623
  • 53
  • 500
  • 612