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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记