OctoberCMS 使用数组
OctoberCMS Working with Arrays
我在理解如何使用数组时遇到了一些困难,我正在尝试制作一张发票,其中 return 是订单的价格和详细信息,我可以使用其中的大部分内容,但我会喜欢显示订单的项目。到目前为止,我的代码如下所示:
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 %}
但这在前端没有return任何东西。
感觉我做错了,因为我是后端开发的菜鸟:P
非常感谢任何帮助
我想您不需要将其转换为 JSON,因为您不需要使用 Javascript
所以只需替换您的代码
return Response::json([
'pdf' => $pdfproof,
'hires' => $hires,
'hardcopy' => $hardcopy,
'plates' => $plates
]);
用这个:
return [
[
'pdf' => $pdfproof,
'hires' => $hires,
'hardcopy' => $hardcopy,
'plates' => $plates
]
];
在模板中,您正在遍历项目,因此您需要向它发送一个数组。它全部 php 代码所以不需要 json.
we return master array(items) 确实有它的项目,当我们遍历 master array 时,我们每次都会得到它的项目(项目)
如果您需要任何进一步的帮助,请发表评论。
我在理解如何使用数组时遇到了一些困难,我正在尝试制作一张发票,其中 return 是订单的价格和详细信息,我可以使用其中的大部分内容,但我会喜欢显示订单的项目。到目前为止,我的代码如下所示:
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 %}
但这在前端没有return任何东西。
感觉我做错了,因为我是后端开发的菜鸟:P 非常感谢任何帮助
我想您不需要将其转换为 JSON,因为您不需要使用 Javascript
所以只需替换您的代码
return Response::json([
'pdf' => $pdfproof,
'hires' => $hires,
'hardcopy' => $hardcopy,
'plates' => $plates
]);
用这个:
return [
[
'pdf' => $pdfproof,
'hires' => $hires,
'hardcopy' => $hardcopy,
'plates' => $plates
]
];
在模板中,您正在遍历项目,因此您需要向它发送一个数组。它全部 php 代码所以不需要 json.
we return master array(items) 确实有它的项目,当我们遍历 master array 时,我们每次都会得到它的项目(项目)
如果您需要任何进一步的帮助,请发表评论。