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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!