Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Silverstripe中将JSON编码的字符串格式化为更易于阅读的格式_Php_Arrays_Json_Silverstripe - Fatal编程技术网

Php 在Silverstripe中将JSON编码的字符串格式化为更易于阅读的格式

Php 在Silverstripe中将JSON编码的字符串格式化为更易于阅读的格式,php,arrays,json,silverstripe,Php,Arrays,Json,Silverstripe,在Silverstripe网站中,用户提交表单,并将其存储在CMS上,以便内容经理查看提交的表单 功能正常,但问题是我在PHP数组中返回了$data,我想将其输出到CMS 我找到的唯一方法是将其转换为JSON,但随后它只输出一个JSON字符串,我希望有类似于HTML表的内容,使其更具可读性。我该怎么做 到目前为止,我的代码是: // converts array to jason, on controller $SubmitedResult->SerialisedForm = Conver

在Silverstripe网站中,用户提交表单,并将其存储在CMS上,以便内容经理查看提交的表单

功能正常,但问题是我在PHP数组中返回了
$data
,我想将其输出到CMS

我找到的唯一方法是将其转换为JSON,但随后它只输出一个JSON字符串,我希望有类似于HTML表的内容,使其更具可读性。我该怎么做

到目前为止,我的代码是:

// converts array to jason, on controller
$SubmitedResult->SerialisedForm = Convert::array2json($data);

// $db on dataobject
private static $db = array(
  'SerialisedForm' => 'Text',
);

// JSON string received below
{"url":"\/test\/test-test\/testSubmit","Name":"Tom","Email":"tom@gmail.com","Phone":"564456","SecurityID":"c5efe841e26d6d088dd94dfcfe76f6ec80acac86","action_submit":"Submit"}

通常,您希望构建一个
DataObject
,用于存储提交的表单数据。看起来您已经有了它,但您使用它将所有数据存储在名为
SerialisedForm
的字段中。我建议您为所有表单字段创建一个单独的字段

例如:

class FormSubmission extends DataObject
{
    private static $db = [
        'Name' => 'Varchar(255)',
        'Email' => 'Varchar(255)',
        'Phone' => 'Varchar(64)'
    ];

    // The summary_fields ensure that your fields directly show up in the GridField
    private static $summary_fields = [
        'Name' => 'Name',
        'Email' => 'Email',
        'Phone' => 'Phone'
    ];
}
然后在表单提交处理程序中执行以下操作:

public function testSubmit($data, $form)
{
    $submittedResult = FormSubmission::create();
    $form->saveInto($submittedResult);
    $submittedResult->write();
    // redirect back or somewhere else…
}

在CMS中,您可以使用页面上的
GridField
ModelAdmin
实例查看提交内容。

请根据我收到的JSON字符串发布一个最小、完整、可验证的示例