문제

I am trying to use a jQuery gantt as a wordpress plugin. Currently I'm stuck on editing the data.json. I use a php form to populate a new item. When submitting the form, it will add data to the file, but behind the closing square brackets.

[{
  ...

 },
 {  "name"  : "Vermessung"
  , "desc"  : ""
  , "values": [
   {  "id"         : "5"
   , "from"       : "/Date(1363132800000)/"
   , "to"         : "/Date(1368655200000)/"
   , "desc"       : "Vom Beauftragen der Vermessung bis zur tatsächlichen Vermessung"
   , "customClass": "ganttBlue"
   , "label"      : "Vermessung"
  }
  ]
 }
]

After submitting the form it looks like this:

[{
  ...

 },
 {  "name"  : "Vermessung"
  , "desc"  : ""
  , "values": [
      {  "id"         : "5"
       , "from"       : "/Date(1363132800000)/"
       , "to"         : "/Date(1368655200000)/"
       , "desc"       : "Vom Beauftragen der Vermessung bis zur tatsächlichen Vermessung"
       , "customClass": "ganttBlue"
       , "label"      : "Vermessung"
      }
  ]
 }
]{"name":null,"desc":null,"values":{"id":null,"from":null,"to":null,"desc":null,"customClass":null,"label":null}}

This is the requested php Code which will adding stuff to the json:

$file = jQg_BASENAME_DIR.'/inc/data.json';
log_me('This is a message for debugging purposes');

if(isset($_POST['submit'])){

$json = file_get_contents( $file );
$data = json_decode($json);

// convert form data to json format
    $postArray = array(
      "name" => $_POST['name'],
      "desc" => $_POST['desc'],
      "values" => array(
         "id" => $_POST["value_id"],
         "from" => $_POST['value_from'],
         "to" => $_POST['value_to'],
         "desc" => $_POST['value_desc'],
         "customClass" => $_POST['value_class'],
         "label" => $_POST['value_label']
        )
    ); //you might need to process any other post fields you have..

$json = json_encode( $postArray );
array_push($json, $postArray);
// write to file
file_put_contents( $file, $json, FILE_APPEND);

I also can't establish the square bracket after value. How can I fix this?

도움이 되었습니까?

해결책

As I said in my comment

$json = file_get_contents( $file );

// $json is now a string 

$data = json_decode($json);

// $data is a PHP object
// So lets call the second array $data->someArray
// since I do not know what it is called looking at your file

// convert form data to PHP array format
    $postArray = array(
      "name" => $_POST['name'],
      "desc" => $_POST['desc'],
      "values" => array(
         "id" => $_POST["value_id"],
         "from" => $_POST['value_from'],
         "to" => $_POST['value_to'],
         "desc" => $_POST['value_desc'],
         "customClass" => $_POST['value_class'],
         "label" => $_POST['value_label']
        )
    ); //you might need to process any other post fields you have..

// $postArray is a PHP object

// $json = json_encode( $postArray ); // do NOT convert here

array_push($data->someArray, $postArray);
$json = json_encode($data);
// write to file
file_put_contents( $file, $json, FILE_APPEND);

다른 팁

Your values field is an array of objects instead of an object (the encoding of a php associative array is a json oject). So for values to have square brackets instead of "values" => array() you would need "values" => array( array("id" => ... etc. ) )

As for your first problem you've inverted the json_encoding. First push your postArray into data, then json_encode data.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top