首页 文章

OctoberCMS Builder插件显示数组

提问于
浏览
0

我使用OctoberCMS中的builder插件构建了一个引用/开票系统,我遇到了一个speedbump . 我正在尝试将数组保存到数据库以用作发票的列表项,但是当我尝试在后端查看发票时,我得到以下异常:

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

我把它写成一个组件,导致问题的部分如下所示:

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
    ];
}

在数据库中,有问题的列是TEXT类型,我在Builder中的模型类型也是TEXT .

我猜这个问题是在我的模型的视图,但我不确定如何纠正这一点,任何帮助将不胜感激 .

2 回答

  • 1

    好的我现在得到它可能是你正在尝试打印数组而不是字符串{} ..所以如果item是一个数组,它将抛出此错误 .

    $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
        ]
    ];
    

    我想这是你的数据,你想要展示它 . 作为它的多维数组,我们需要2个循环 . 我们假设你有共享$ items变量来查看,所以,

    {% 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..
    

    这个链接可以帮助你与循环战斗:https://twig.symfony.com/doc/2.x/tags/for.html

  • 0

    所以在经历了很多挫折后我使用这种方法让它工作:

    public function items() {
        $NoOfPlates = Db::table('orders')->where('quote_no', $this->quoteNo())->value('total_plate_qty');
        $polymerPrice = Db::table('quotes')->where('quote_no', $this->quoteNo())->value('calc_polymer_price');
        $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');
        $variants = Db::table('quotes')->where('quote_no', $this->quoteNo())->value('variants');
        if ($NoOfPlates != 0) {
            $platesName = 'Plates';
            $platesPrice = $polymerPrice;
            $variants = $variants;
            $platesTotal = $platesPrice * $variants;
        } else {
            $platesName = 'No Plates';
            $platesPrice = 0;
            $variants = $variants;
            $platesTotal = $platesPrice * $variants;
        }
        if ($pdfproof === 'yes') {
            $pdfName = 'PDF Proof';
            $pdfPrice = 25.00;
            $variants = $variants;
            $pdfTotal = $pdfPrice * $variants;
        } else {
            $pdfName = 'No PDF Proofs';
            $pdfPrice = 0;
            $variants = $variants;
            $pdfTotal = $pdfPrice * $variants;
        }
        if ($hires === 'yes') {
            $hiresName = 'HiRes PDFs';
            $hiresPrice = 50.00;
            $variants = $variants;
            $hiresTotal = $hiresPrice * $variants;
        } else {
            $hiresName = 'No HiRes PDFs';
            $hiresPrice = 0;
            $variants = $variants;
            $hiresTotal = $hiresPrice * $variants;
        }
        if ($hardcopy === 'yes') {
            $hardcopyName = 'HardCopy Proofs';
            $hardcopyPrice = 100.00;
            $variants = $variants;
            $hardcopyTotal = $hardcopyPrice * $variants;
        } else {
            $hardcopyName = 'No HardCopy Proofs';
            $hardcopyPrice = 0;
            $variants = $variants;
            $hardcopyTotal = $hardcopyPrice * $variants;
        }
    
        return [
            '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
            ]
        ];
    }
    

    然后在前端我称之为:

    {% if record.items.plates.name == 'Plates' %}
    <tr>
        <td>{{ record.items.plates.name }}</td>
        <td class="text-xs-center">R{{ record.items.plates.price }}</td>
        <td class="text-xs-center">{{ record.items.plates.quantity }}</td>
        <td class="text-xs-right">R{{ record.items.plates.total }}</td>
    </tr>
    {% endif %}
    

相关问题