[英]OctoberCMS Working with Arrays
我在理解如何使用數組時遇到一些麻煩,我正在嘗試建立一個發票,通過它返回訂單的價格和詳細信息,我有大部分工作,但我想顯示訂單的項目。 到目前為止我的代碼看起來像這樣:
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任何幫助都非常感激
我猜您不需要將其轉換為JSON,因為您不需要使用Javascript
所以只需替換你的代碼
return Response::json([
'pdf' => $pdfproof,
'hires' => $hires,
'hardcopy' => $hardcopy,
'plates' => $plates
]);
有了這個 :
return [
[
'pdf' => $pdfproof,
'hires' => $hires,
'hardcopy' => $hardcopy,
'plates' => $plates
]
];
在模板中,您循環遍歷項目,因此您需要向它發送一個數組。 它的所有PHP代碼所以不需要json。
我們返回擁有其項目的主數組( 項目 ),當我們遍歷主數組時,我們每次都得到它的項目( 項目 )
如果您需要任何進一步的幫助,請評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.