【问题标题】:OctoberCMS Working with ArraysOctoberCMS 使用数组
【发布时间】:2018-04-14 17:11:31
【问题描述】:

我在理解如何使用数组时遇到了一些麻烦,我正在尝试创建一张发票,通过它返回订单的价格和详细信息,我大部分都在工作,但我想显示命令。到目前为止,我的代码如下所示:

public $items;

public function prepareVars() {
    $this->items = $this->items();
}


public function items() {
    $plates = Db::table('orders')->where('quote_no', $this->quoteNo())->value('total_plate_qty');
    $hires = Db::table('orders')->where('quote_no', $this->quoteNo())->value('req_hires');
    $hardcopy = Db::table('orders')->where('quote_no', $this->quoteNo())->value('req_hardcopy_proof');
    $pdfproof = Db::table('orders')->where('quote_no', $this->quoteNo())->value('req_pdf_proof');
    if ($plates < 1) {
        $plates = "Total Plates:" . $plates;
    } else {
        $plates = "";
    }
    if ($pdfproof === 'yes') {
        $pdfproof = 'PDF Proof @ R25.00';
    } else {
        $hires = '';
    }
    if ($hires === 'yes') {
        $hires = 'HiRes PDF @ R50.00';
    } else {
        $hires = '';
    }
    if ($hardcopy === 'yes') {
        $hardcopy = 'HardCopy Proof @ R150.00';
    } else {
        $hardcopy = '';
    }
    return Response::json([
        'pdf' => $pdfproof,
        'hires' => $hires,
        'hardcopy' => $hardcopy,
        'plates' => $plates
    ]);
}

这是将数据保存在我的数据库中,如下所示:

HTTP/1.0 200 OK
Cache-Control: no-cache
Content-Type:  application/json
Date:          Thu, 02 Nov 2017 08:26:11 GMT

{"pdf":"PDF Proof @ R25.00","hires":"HiRes PDF @ R50.00","hardcopy":"HardCopy Proof @ R150.00","plates":""}

然后在前端我使用了 twig 函数 {%for%} ,它看起来像这样:

{% set items = __SELF__.items %}
      {% for item in items %}
      <td>{{ item.pdf }}</br>{{ item.hires }}</br>{{ item.hardcopy }}</br>{{ item.plates }}</br></td>
      {% endfor %}

但这不会在前端返回任何内容。

这感觉就像我做错了,因为我是后端开发的菜鸟:P 任何帮助将不胜感激

【问题讨论】:

  • 我没有使用 OctoberCMS 但我认为您必须像 {% set items = json_decode(__SELF__.items) %} 那样解码项目 json 并测试!!

标签: php laravel backend octobercms


【解决方案1】:

我猜你不需要将它转换为 JSON,因为你不需要使用 Javascript

所以只需替换您的代码

return Response::json([
    'pdf' => $pdfproof,
    'hires' => $hires,
    'hardcopy' => $hardcopy,
    'plates' => $plates
]);

有了这个:

return [
    [
        'pdf' => $pdfproof,
        'hires' => $hires,
        'hardcopy' => $hardcopy,
        'plates' => $plates
    ]
];

就像在模板中一样,您正在循环遍历项目,因此您需要向它发送一个数组。它都是 php 代码,所以不需要 json。

我们返回主数组(item),它确实有它的项目,当我们遍历主数组时,我们每次都会得到它的项目(item

如果您需要任何进一步的帮助,请发表评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-01
    • 2018-04-12
    • 2016-11-06
    • 1970-01-01
    • 2019-12-17
    • 2016-10-17
    • 1970-01-01
    • 2017-04-05
    相关资源
    最近更新 更多