PHP PRINTER EROR

128 Views Asked by At

Good day everyone please help me to solved this code. I want to print all value of an array in php printer but only 1 only display. Im using php codeigniter framework 3. thank you in advance.

enter image description here Output: Qty Description 1 Product One

$content = "Customer " . $this->uri->segment(2) . "\n"; 
foreach ($orders as $order) { 
    $content = "Qty Description\r" . $order->Quan . " " . 
              " " . " " . $order->Description . "\r"; 
} 
$printer = ("EPSON TM-U220 Receipt"); 
$handler = printer_open($printer); 
if($handler) { 
} 
else { 
    echo "not connected"; 
} 
printer_write($handler, $content); 
printer_close($handler); 
2

There are 2 best solutions below

8
On BEST ANSWER

You want to concatenate $content.

Change

foreach($orders as $order) {
   $content = ....
}

to

foreach($orders as $order) {
   $content .= ....   // here-> .= 
}
0
On

You are overwriting the value of $content until the foreach() loop ends.So for now it's only displaying the last value that was assigned to $content inside the foreach() loop.

There are two things that you can do basically.

1.Assign the values into an array.

foreach($orders as $order) {    
$content[] = "Qty Description\r" . $order->Quan . " " . " " . " " . $order->Description . "\r";
}

By this method all the values that you are getting from the loop will be stored inside the array.

2.concatenate the values in $content

foreach($orders as $order) {
     $content .= "Qty Description\r" . $order->Quan . " " . " " . " " . $order->Description . "\r";
  }

I hope this will help you.