-2

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); 
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Jstin
  • 9
  • 1
  • 4
    Please [edit] your post and add your code there as text. It makes it much easier to read. – aynber Jun 17 '20 at 12:54
  • Your `if($handler) { ` code should either include the `printer_write()` parts or stop if the printer fails to connect. At the moment it will display a message and then just carry on and try printing. – Nigel Ren Jun 17 '20 at 13:16

2 Answers2

0

You want to concatenate $content.

Change

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

to

foreach($orders as $order) {
   $content .= ....   // here-> .= 
}
B001ᛦ
  • 2,036
  • 6
  • 23
  • 31
  • 1
    it is posible to print data in 2 printers at the same time? – Jstin Jun 17 '20 at 13:32
  • _it is posible to print data in 2 printers..._ Inever tried that, but I think you'll need 2 separate handlers – B001ᛦ Jun 17 '20 at 13:34
  • using condition statement right? – Jstin Jun 17 '20 at 13:36
  • Just something like `$handler1 = printer_open($printer1);$handler2 = printer_open($printer2)` etc – B001ᛦ Jun 17 '20 at 13:37
  • mate have you tried printing using network connection? – Jstin Jun 18 '20 at 09:22
  • Ah no sorry, but there must be some good tutorials out there – B001ᛦ Jun 18 '20 at 12:49
  • mate what's wrong to my code please help me – Jstin Jun 23 '20 at 14:57
  • printer_draw_text($handle, "CUSTOMER " . $this->uri->segment(2)."", 10, 10); printer_draw_text($handle, "". $record->Pax . "PAX", 10, 10); printer_draw_text($handle, "" . $date_time . "", 10, 10); foreach ($orders as $order) { printer_draw_text($handle, "" . $order->Quan . " " . strtoupper($order->Description). "", 10, 10); if (!empty($order->Remarks) || $order->Remarks != "") { printer_draw_text($handle,"" . strtoupper($order->Remarks) . ""); } } – Jstin Jun 23 '20 at 14:58
0

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.

Hirumina
  • 738
  • 1
  • 9
  • 23