Gareth
Gareth

Reputation: 102

OctoberCMS Builder Plugin displaying arrays

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.

Upvotes: 0

Views: 625

Answers (2)

Hardik Satasiya
Hardik Satasiya

Reputation: 9715

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

Upvotes: 1

Gareth
Gareth

Reputation: 102

So after much frustration I used this method to get it to work:

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

Then on the frontend I call these like this:

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

Upvotes: 1

Related Questions