文本打印机中的表格 - PHP

时间:2013-07-12 18:06:30

标签: php text printing

我正在尝试使用php printer_write函数在文本打印机中定位文本,但我知道它不会那样工作。你能否建议我在表或div中使用某种方法来使它们有条理。

以下是我正在使用的代码。

<?php

include "connect.php";
include "links.php";

$date = DATE('D, d M, Y');
$i=1;
$getData = mysql_query("SELECT * FROM facturas");

$dataCount = mysql_num_rows($getData);


$handle = printer_open("Generic / Text Only");
printer_set_option($handle, PRINTER_MODE, "RAW");
printer_set_option($handle, PRINTER_TEXT_ALIGN, PRINTER_TA_RIGHT);
printer_write($handle, $business_name . "        " . $date);
printer_write($handle, "\n");
printer_write($handle, $street . ", " . $city);
printer_write($handle, "\n");
printer_write($handle, $phone);
printer_write($handle, "\n");
printer_write($handle, "-----------------------------------");
printer_write($handle, "\n");
printer_write($handle, "\n");

    while($showData = mysql_fetch_array($getData)){

    $serial = $showData['serial'];
    $type = $showData['type'];
    $model = $showData['model'];
    $qty = $showData['qty'];
    $price = $showData['price'];

    printer_write($handle, $qty . " x " . $type . " " . $model . "....." . $price . "\n");
}
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");

printer_close($handle);

&GT;

这段代码打印好了,但是WHILE循环中的printer_qire代码会在每行上打印出所有内容,工作正常,但是如果有的话我可以在表中有这些数据吗?

这是本地服务器中的练习代码,我知道SQL注入。

由于

1 个答案:

答案 0 :(得分:1)

您只需要填充所有字符串,以便乳清是您想要的列的宽度。您可以执行类似于在数组中定义列宽的操作。

 $columnWidths = array(
      "serial"=>25,
      "type"=>15,
      "model"=>50,
      "qty"=>5,
      "price"=>10

 );

然后,您可以在while循环的开头将这些列宽应用于showData数组,如:

foreach($showData as $k=>$v){
    $showData[$k] = str_pad($showData[$k], $columnWidths[$k], " ", STR_PAD_RIGHT);
}

所以完整的代码看起来像这样:

<?php

include "connect.php";
include "links.php";

$columnWidths = array(
    "serial"=>25,
    "type"=>15,
    "model"=>50,
    "qty"=>5,
    "price"=>10

);
$date = DATE('D, d M, Y');
$i=1;
$getData = mysql_query("SELECT * FROM facturas");

$dataCount = mysql_num_rows($getData);


$handle = printer_open("Generic / Text Only");
printer_set_option($handle, PRINTER_MODE, "RAW");
printer_set_option($handle, PRINTER_TEXT_ALIGN, PRINTER_TA_RIGHT);
printer_write($handle, $business_name . "        " . $date);
printer_write($handle, "\n");
printer_write($handle, $street . ", " . $city);
printer_write($handle, "\n");
printer_write($handle, $phone);
printer_write($handle, "\n");
printer_write($handle, "-----------------------------------");
printer_write($handle, "\n");
printer_write($handle, "\n");

    while($showData = mysql_fetch_array($getData)){
        foreach($showData as $k=>$v){
            $showData[$k] = str_pad(substr($showData[$k],0, $columnWidths[$k]), $columnWidths[$k], " ", STR_PAD_RIGHT);
        }
        $serial = $showData['serial'];
        $type = $showData['type'];
        $model = $showData['model'];
        $qty = $showData['qty'];
        $price = $showData['price'];

        printer_write($handle, $qty . " x " . $type . " " . $model . "....." . $price . "\n");
    }
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");
printer_write($handle, "\n");

printer_close($handle);