带有条形码图像的PHP查询结果


PHP Query Results with Barcode Image

我正试图为从SQL查询中获得的每个结果创建一个条形码。基本上,我在HTML表中显示了结果,在最后一列中,我希望显示一个值为$row=[1]的条形码。我完全不知道该怎么做。

<?php
//PHP Includes
include('inc/database.php');
require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');
require_once('class/Barcode39.php');
header('Content-Type: image/png');
// MSSQL Query
$sql = "SELECT warehouse, pick_order_number, order_status, pick_order_type, customer_order_number
        FROM pick_order_header
        WHERE warehouse = 'XDGM'
        AND order_status <> 'Complete'
        AND order_status <> 'Closed'
        AND pick_order_type <> 'Backorder'";
?>
<!DOCTYPE HTML>
<link rel="stylesheet" type="text/css" href="css/master.css">
<html>
<title>Current Orders</title>
<body>
<table>
<?php
// SQLSRV Query
$results = sqlsrv_query( $conn, $sql );
if( $results === false) {
    die( print_r( sqlsrv_errors(), true) );
}
    echo "
            <table border=1>
            <tr>
                <th>Order Number</th>
                <th>Order Status</th>
                <th>Order Type</th>
                <th>Customer Order</th>
                <th>Barcode</th>
            </tr>";
    while ($row = sqlsrv_fetch_array($results))
    {
        $odrnum = $row[1];
        $odrstatus = $row[2];
        $odrtype = $row[3];
        $custorder = $row[4];
        $barcode = $row[1];
    echo "
            <tr>
                <td>$odrnum</td>
                <td>$odrstatus</td>
                <td>$odrtype</td>
                <td>$custorder</td>
                <td>$barcode</td>
            </tr>";
    }
    echo "</table>";
?>
</table>
</body>
</html>

要创建条形码,我知道我需要

$code = new BCGcode39(); // Or another class name from the manual
$code->setScale(2); // Resolution
$code->setThickness(30); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0)
$code->parse('HELLO'); // Text
$drawing = new BCGDrawing('', $colorfg);
$drawing->setBarcode($code);
$drawing->draw();
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);

我想看到的条形码是

<td>$barcode</td>

我只是不知道如何实现它。

当你处理它时,它似乎并不那么简单。我从未使用过你显示的库,我想你正在使用http://www.barcodephp.com/en/manual/BCGcode39.

通过该网站中显示的示例,我认为draw()方法的输出是与要生成的图像相对应的字节流。有很多方法可以实现你想要的。其中之一是有一个进程(可能是其他文件),它将接收字符串并输出相应的条形码,然后在html表中调用它。

大致如下:

1-DrawBarcode.php

<?php
require_once('class/BCGDrawing.php');
header('Content-Type: image/png');
$drawing = new BCGDrawing('', $colorfg);
$drawing->setBarcode($_GET['code']);
$drawing->draw();

2-Table.php

<?php   
// MSSQL Query
$sql = "SELECT warehouse, pick_order_number, order_status, pick_order_type, customer_order_number
        FROM pick_order_header
        WHERE warehouse = 'XDGM'
        AND order_status <> 'Complete'
        AND order_status <> 'Closed'
        AND pick_order_type <> 'Backorder'";
?>
<!DOCTYPE HTML>
<link rel="stylesheet" type="text/css" href="css/master.css">
<html>
<title>Current Orders</title>
<body>
<table>
<?php
// SQLSRV Query
$results = sqlsrv_query( $conn, $sql );
if( $results === false) {
    die( print_r( sqlsrv_errors(), true) );
}
    echo "
            <table border=1>
            <tr>
                <th>Order Number</th>
                <th>Order Status</th>
                <th>Order Type</th>
                <th>Customer Order</th>
                <th>Barcode</th>
            </tr>";
    while ($row = sqlsrv_fetch_array($results))
    {
        $odrnum = $row[1];
        $odrstatus = $row[2];
        $odrtype = $row[3];
        $custorder = $row[4];            
    echo "
            <tr>
                <td>$odrnum</td>
                <td>$odrstatus</td>
                <td>$odrtype</td>
                <td>$custorder</td>
                <td><img src="DrawBarcode.php?code=<?php echo $row[1];?>"/></td>
            </tr>";
    }
    echo "</table>";
?>
</table>
</body>
</html>

另一种方法(非常类似)是将条形码生成到服务器上的文件中,然后将其作为img标记的源进行引用。