dongqin5604 2017-11-07 06:22
浏览 33
已采纳

OctoberCMS Builder插件显示数组

I have built a quoting/invoicing system using the builder plugin in OctoberCMS and I have ran into a speedbump. I am trying to save an array to the database to be used as list items for the invoice but when I try to view the invoice in the backend I get the following Exception:

htmlentities() expects parameter 1 to be string, array given

I have written this as a component and the section which is causing the issue looks like this:

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 != 0) {
        $plates = "Total Plates: " . $plates;
    } else {
        $plates = "None";
    }
    if ($pdfproof === 'yes') {
        $pdfproof = 'PDF Proof @ R25.00';
    } else {
        $hires = 'None';
    }
    if ($hires === 'yes') {
        $hires = 'HiRes PDF @ R50.00';
    } else {
        $hires = 'None';
    }
    if ($hardcopy === 'yes') {
        $hardcopy = 'HardCopy Proof @ R150.00';
    } else {
        $hardcopy = 'None';
    }
    return [
        'plates' => $plates,
        'pdf' => $pdfproof,
        'hires' => $hires,
        'hardcopy' => $hardcopy
    ];
}

In the database the column in question is type TEXT and my model type in Builder for this field is also TEXT.

I am guessing that the issue is in the view of my model but I am unsure how to correct this, any help will be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dongqi7631 2017-11-09 12:13
    关注

    ok ok I got it now may be you are trying to print array instead of string as {{ item }} .. so if item is an array it will throw this error.

    $items =  [
        'plates' => [
            'name' => $platesName,
            'price' => $platesPrice,
            'quantity' => $variants,
            'total' => $platesTotal
        ],
        'pdf' => [
            'name' => $pdfName,
            'price' => $pdfPrice,
            'quantity' => $variants,
            'total' => $pdfTotal
        ],
        'hires' => [
            'name' => $hiresName,
            'price' => $hiresPrice,
            'quantity' => $variants,
            'total' => $hiresTotal
        ],
        'hardcopy' => [
            'name' => $hardcopyName,
            'price' => $hardcopyPrice,
            'quantity' => $variants,
            'total' => $hardcopyTotal
        ]
    ];
    

    I guess this is your data and you want to show it. as its multi-dimension array we need 2 loops. we assume that you have share $items variable to view so,

    {% for key, item in items %} 
        <h1> {{ key }} </h1>
        <ul>    
            <li>{{ item.name }}</li>
            <li>{{ item.price }}</li>
            <li>{{ item.quantity }}</li>
            <li>{{ item.total }}</li>
        </ul>
    {% endfor %}
    
    // output
    <h1>Plates</h1>
    <ul>
        <li>$platesName</li>
        <li>$platesPrice</li>
        <li>$variants</li>
        <li>$platesTotal</li>
    </ul>
    <h1>Pdf</h1>
    <ul>
        <li>$platesName</li>
        <li>$platesPrice</li>
        <li>$variants</li>
        <li>$platesTotal</li>
    </ul>
    // and sooo on..
    

    this link may help you to fight with loops : https://twig.symfony.com/doc/2.x/tags/for.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信夜间被转走了1w对,当天手机剪切板里就出现了这个乱码,有铁子可以看看是啥吗可以
  • ¥50 跑通github上的代码 深度学习 pytorch
  • ¥50 求写,批处理调用分区助手分区脚本
  • ¥15 求购HI3519AV100开发板
  • ¥15 请问1553 RT怎么测试,没有BC有方法吗
  • ¥100 业务编程如何选择学习方向和内容?
  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 【有偿】调用DXGI截图初始化失败,提示0xf 887a0004