picqer / php-barcode-generator

Barcode generator in PHP that is easy to use, non-bloated and framework independent.
GNU Lesser General Public License v3.0
1.67k stars 402 forks source link

variable barcode type #85

Closed Mr-TOA closed 4 years ago

Mr-TOA commented 4 years ago

Hello there, I'm trying to use variable instead of const, but gives me some errors

$generator = new Picqer\Barcode\BarcodeGeneratorSVG();
$generated = $generator->getBarcode('1234567890', $generator:: $barcode_type);

error:

PHP Fatal error: Uncaught Error: Access to undeclared static property: Picqer\Barcode\BarcodeGeneratorSVG::$barcode_type

thanks

joeybab3 commented 4 years ago

You have not declared $barcode_type in the php above, and you do not need to do $generator::$barcode_type when you have already declared $generator to be the generator type you want. Remove the ::$barcode_type part from the second line

Mr-TOA commented 4 years ago

Hi Joey, you are right but I want to select the barcode type from select option HTML tag and return the value just like above, is there anyway that I can do that ? thanks :)

joeybab3 commented 4 years ago

Are you able to post more of the code? You could do an if statement for each select value and have it instantiate a different generator?

Mr-TOA commented 4 years ago

not more posts of the code just one at a time but through a variable; e.g.

<?php
if(isset($_POST['barcode_type'])){
     $barcode_type = $_POST['barcode_type'];
     $generator = new Picqer\Barcode\BarcodeGeneratorSVG();
     $generated = $generator->getBarcode('1234567890', $generator::$barcode_type);
}
?>
casperbakker commented 4 years ago

It is probably not supported by PHP, the thing you try. Or the string you have in $barcode_type is not an exact match for one of the values used as static variables in the class.

What did you gave as an option in $barcode_type?

slokhorst commented 4 years ago

This should work:

<select name="barcode_type">
    <option>C39</option>
    <option>C39+</option>
    <option>C39E</option>
    <option>C39E+</option>
    <option>C93</option>
    <option>S25</option>
    <option>S25+</option>
    <option>I25</option>
    <option>I25+</option>
    <option>C128</option>
    <option>C128A</option>
    <option>C128B</option>
    <option>C128C</option>
    <option>EAN2</option>
    <option>EAN5</option>
    <option>EAN8</option>
    <option>EAN13</option>
    <option>UPCA</option>
    <option>UPCE</option>
    <option>MSI</option>
    <option>MSI+</option>
    <option>POSTNET</option>
    <option>PLANET</option>
    <option>RMS4CC</option>
    <option>KIX</option>
    <option>IMB</option>
    <option>CODABAR</option>
    <option>CODE11</option>
    <option>PHARMA</option>
    <option>PHARMA2T</option>
</select>

Note to use the string values from BarcodeGenerator.php, not the const variable names.

<?php
if(isset($_POST['barcode_type'])){
     $barcode_type = $_POST['barcode_type'];
     $generator = new Picqer\Barcode\BarcodeGeneratorSVG();
     $generated = $generator->getBarcode('1234567890', $barcode_type);
}
?>

Note to just use $barcode_type in the last line.

casperbakker commented 4 years ago

Thanks @slokhorst, that is indeed a way it should just work. This is not something related to this package itself, so I will close it now.