文本打印机中的表格-PHP


Tables in Text Printer - PHP

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

?>

这个代码会打印得很好,但WHILE循环中的printer_qire代码会在它的每一行上打印所有内容,工作正常,但如果有,我可以把这些数据放在表中吗?

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

感谢

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

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