条码图像的PHP查询结果

时间:2013-06-21 13:35:37

标签: php arrays header barcode

我正在尝试为从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>

我对如何实现它一无所知。

1 个答案:

答案 0 :(得分:1)

当你接近它时,它似乎并不那么简单。我从未使用过你正在展示的图书馆,我猜你正在使用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标记的来源引用。