Search code examples
phparraysthermal-printer

Adding items dynamically to array Mike42 php


Actually I am using escpos-php thermal printer mike42 library and to add items on the invoice there is an array so how can add them dynamically ?

There array is:

$items = array(
    new item("Example item #1", "4.00"),
    new item("Another thing", "3.50"),
    new item("Something else", "1.00"),
    new item("A final item", "4.45"),
);

I want something like:

$items = array(
while($fetch = mysqli__fetch_assoc($sql)){
        new item($fetch['item'], $fetch['price']),
}
);

Solution

  • You were almost there, just take the while loop out of your array.

    $items = []; // Or array()
    
    while($fetch = mysqli__fetch_assoc($sql)){
        $items[] = new item($fetch['item'], $fetch['price']);
    }