PHP 打印机错误

PHP PRINTER EROR

大家好,请帮我解决这段代码。我想在 php 打印机中打印一个数组的所有值,但只显示 1 个。我正在使用 php codeigniter 框架 3。提前谢谢你。

enter image description here 输出: 数量说明 1 产品一

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

您想连接 $content

改变

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

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

您正在覆盖 $content 的值直到 foreach() 循环 ends.So 现在它只显示在 foreach() 中分配给 $content 的最后一个值循环。

您基本上可以做两件事。

1.Assign 将值放入数组中。

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

通过这种方法,您从循环中获得的所有值都将存储在数组中。

2.concatenate $content

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

希望对您有所帮助。