eternityhq
eternityhq

Reputation: 115

PHP Query Results with Barcode Image

I am trying to create a barcode for each of the results I get from my SQL Query. Basically I have the results showing in an HTML table, and in the last column I want so show a barcode = to the value of $row=[1]. I am at a complete loss on how to do this.

<?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>

To create the barcode I know I need to have

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

Where I would like to see that barcode is

<td>$barcode</td>

I just have no clue on how to achieve it.

Upvotes: 0

Views: 1520

Answers (1)

Muc
Muc

Reputation: 1526

It doesn't seem so straightforward as you approached it. I never used the library you're showing, I guess you're using http://www.barcodephp.com/en/manual/BCGcode39.

By the examples shown in that website, I figure the output from the draw() method is a bytestream corresponding to the image to be generated. There is a number of ways to accomplish what you want. One of them would be to have a process (some other file probably) which would receive the string and output the according barcode, and then call it inside your html table.

Something along the lines of:

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>

That should do it. Another way (very similar though), would be to generate the barcodes into files on your server, and then reference them as the source of the img tag.

Upvotes: 1

Related Questions