0

我正在尝试使用 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);

?>

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

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

谢谢

4

1 回答 1

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);
于 2013-07-12T18:25:35.233 回答