用于生成code128的php文件在codeigniter中不起作用

时间:2019-05-14 16:01:12

标签: php codeigniter

我尝试从库中运行,并且错误地打印了条形图,但是所有问题出在脚本中,由于某种原因与CI发生冲突……我想将来我必须使代码适应CI。


<div id="cke_1_contents" class="cke_contents cke_reset" role="presentation" style="height: 200px;">
<span id="cke_52" class="cke_voice_label">Press ALT 0 for help</span>
<iframe src="" style="width: 100%; height: 100%;" class="cke_wysiwyg_frame cke_reset" title="Rich Text Editor, Details_0__Content" aria-describedby="cke_52" tabindex="0" allowtransparency="true" frameborder="0"></iframe></div>

<body class="cke_editable cke_editable_themed cke_contents_ltr cke_show_borders" spellcheck="false" contenteditable="true">
<h1>Test</h1>
<p>Test<br></p>
<p>Test<br></p>
<p><br></p>
</body>

首先,我对Codeigniter相当了解。

我发现此php代码生成了code128,我需要从数据库打印多个标签,然后将其发送到Zebra打印机。

在纯php上效果很好,但是使用库集成到codeigniter时,这些条无法打印。除了横条,一切都在打印。

在注释中的代码上,您可以看到我的所有尝试。

首先,我在相同的视图“ include”“ require_once”中进行了尝试,但随后发现它不适用于CI或类似的东西,因此我创建了一个新库:


<?php

function bar128($text) { // Part 1, make list of widths
 $char128asc=' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'; 
$char128wid = array(
 '212222','222122','222221','121223','121322','131222','122213','122312','132212','221213', // 0-9 
 '221312','231212','112232','122132','122231','113222','123122','123221','223211','221132', // 10-19 
 '221231','213212','223112','312131','311222','321122','321221','312212','322112','322211', // 20-29 
 '212123','212321','232121','111323','131123','131321','112313','132113','132311','211313', // 30-39 
 '231113','231311','112133','112331','132131','113123','113321','133121','313121','211331', // 40-49 
 '231131','213113','213311','213131','311123','311321','331121','312113','312311','332111', // 50-59 
 '314111','221411','431111','111224','111422','121124','121421','141122','141221','112214', // 60-69 
 '112412','122114','122411','142112','142211','241211','221114','413111','241112','134111', // 70-79 
 '111242','121142','121241','114212','124112','124211','411212','421112','421211','212141', // 80-89 
 '214121','412121','111143','111341','131141','114113','114311','411113','411311','113141', // 90-99
 '114131','311141','411131','211412','211214','211232','23311120' ); // 100-106
 $w = $char128wid[$sum = 104]; // START symbol
 $onChar=1;
 for($x=0;$x<strlen($text);$x++) // GO THRU TEXT GET LETTERS
 if (!( ($pos = strpos($char128asc,$text[$x])) === false )){ // SKIP NOT FOUND CHARS
 $w.= $char128wid[$pos];
 $sum += $onChar++ * $pos;
 } 
 $w.= $char128wid[ $sum % 103 ].$char128wid[106]; //Check Code, then END
 //Part 2, Write rows
 $html="<table cellpadding=0 cellspacing=0><tr>"; 
 for($x=0;$x<strlen($w);$x+=2) // code 128 widths: black border, then white space
 $html .= "<td><div class=\"b128\" style=\"border-left-width:{$w[$x]};width:{$w[$x+1]}\"></div></td>"; 
 return "$html<tr><td colspan=".strlen($w)." align=center><font family=arial size=1.5>$text</td></tr></table>"; 
}

//include 'barcode128.php';
$product    = $_POST['product'];
$product_id = $_POST['product_id'];
$rate       = $_POST['rate'];

for ($i = 1; $i <= $_POST['print_qty']; $i++) {
    echo "<p class='inline'><span ><b>$product</b></span>" . bar128(stripcslashes($_POST['product_id'])) . "<span ><b>Precio: " . $rate . " </b><span></p>&nbsp&nbsp&nbsp&nbsp";
}

?>


//////The lib//////

<?php

global $char128asc,$char128charWidth;
$char128asc=' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'; 
$char128wid = array(
 '212222','222122','222221','121223','121322','131222','122213','122312','132212','221213', // 0-9 
 '221312','231212','112232','122132','122231','113222','123122','123221','223211','221132', // 10-19 
 '221231','213212','223112','312131','311222','321122','321221','312212','322112','322211', // 20-29 
 '212123','212321','232121','111323','131123','131321','112313','132113','132311','211313', // 30-39 
 '231113','231311','112133','112331','132131','113123','113321','133121','313121','211331', // 40-49 
 '231131','213113','213311','213131','311123','311321','331121','312113','312311','332111', // 50-59 
 '314111','221411','431111','111224','111422','121124','121421','141122','141221','112214', // 60-69 
 '112412','122114','122411','142112','142211','241211','221114','413111','241112','134111', // 70-79 
 '111242','121142','121241','114212','124112','124211','411212','421112','421211','212141', // 80-89 
 '214121','412121','111143','111341','131141','114113','114311','411113','411311','113141', // 90-99
 '114131','311141','411131','211412','211214','211232','23311120' ); // 100-106




class Bar128 
{

    function __construct()
    {



function Barra128($cod) { // Part 1, make list of widths
 global $char128asc,$char128wid; 
 $w = $char128wid[$sum = 104]; // START symbol
 $onChar=1;
 for($x=0;$x<strlen($cod);$x++) // GO THRU TEXT GET LETTERS
 if (!( ($pos = strpos($char128asc,$cod[$x])) === false )){ // SKIP NOT FOUND CHARS
 $w.= $char128wid[$pos];
 $sum += $onChar++ * $pos;
 } 
 $w.= $char128wid[ $sum % 103 ].$char128wid[106]; //Check Code, then END
 //Part 2, Write rows
 $html="<table cellpadding=0 cellspacing=0><tr>"; 
 for($x=0;$x<strlen($w);$x+=2) // code 128 widths: black border, then white space
 $html .= "<td><div class=\"b128\" style=\"border-left-width:{$w[$x]};width:{$w[$x+1]}\"></div></td>"; 
 return "$html<tr><td colspan=".strlen($w)." align=center><font family=arial size=1.5>$cod</td></tr></table>"; 
}
}

?>

//////////// CONTROLLER ///////////

    public function Barcodegenerator()
    {

        //load library
        $this->load->library('Myzend');
        //$this->zend->load('Zend/barcode128');
        //require ('barcode128.php');
        //$this->load->helper('barcode128');
        //require_once APPPATH. 'barcode128.php';
        //require 'barcode128.php';
        //include APPPATH. 'barcode128.php';
        //include('assets/barcode128.php');
        $this->load->view('barcode',$data);

    }
////////////////// My Lib //////////


<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

require('Zend/barcode128.php');

class MyZend extends Bar128 {

    public function __construct()
    {
        parent::__construct();
        $CI =& get_instance();
}

}

2 个答案:

答案 0 :(得分:0)

一般建议(在代码点火器中可能是一个问题):

此路径是相对于您的库(或third_party)目录中的lib文件的。

require('Zend/barcode128.php');

如果您希望从其他地方开始使用它,最好使用预定义的相对路径(BASEPATH,APPPATH,FCPATH等)。例如

require(APPPATH . 'libraries/zend/barcode128.php');

将查找/application/libraries/zend/barcode128.php

还尝试仅将第一个大写名称用作库名称,例如将其更改

class MyZend extends Bar128 {

class Myzend extends Bar128 {

有时(code)Codeigniter在库,助手等名称形成方面不能很好地发挥作用。

创建库主文件的一种安全方法是用大写的首字母命名,其余的小写,例如,案例文件名应为“ Myzend.php”。

关于在codeigniter中启用日志: 在config.php中:

$config['log_threshold'] = 4;

将日志设置为包含INFO,可能有助于查找错误

答案 1 :(得分:0)

您实际上并不需要Myzend库或任何文件。您只需要修改bar128类文件即可遵循自定义CI库的命名约定。 一种快速的开始方法是使用配置文件中定义的前缀重命名文件。然后,在其构造器中,初始化库中所有其他方法都应使用的CI实例。     父母:: __ construct();     $ CI =&get_instance();

然后,您可以在该类中用$ CI替换每个$ this。