简体   繁体   中英

PHP CodeIgniter Error: Undefined Property [Generate Qrcode]

Hi im new to using additional libraries on codeigniter (im using this library ) so these error are really overwhelming me, the goal im trying is to make a form input with option tag (the options from my db) then when i click the button, it will automatically generate the qrcode. at first im following some tutorial from the internet and adjust some parts in order to reach the actual goal.

It doesnt show any explicit error on the html page so i checked the console of the page the error as follows;

jquery-3.4.1.js:9837 POST http://localhost/ikanku/Make_qr/save 500 (Internal Server Error)
send @ jquery-3.4.1.js:9837
ajax @ jquery-3.4.1.js:9434
(anonymous) @ make_qr:421
dispatch @ jquery-3.4.1.min.js:2
v.handle @ jquery-3.4.1.min.js:2

so im trying to open the network tab to see if there anything that i could fix anything, but it confuse me bcs the error show as following;

A PHP Error was encountered
Severity: Notice

Message: Undefined property: Make_qr::$Ciqrcode

Filename: core/Model.php

Line Number: 73

Backtrace:

File: C:\xampp\xampp\htdocs\ikanku\application\models\Make_qr_model.php
Line: 35
Function: __get

File: C:\xampp\xampp\htdocs\ikanku\application\controllers\Make_qr.php
Line: 24
Function: save

File: C:\xampp\xampp\htdocs\ikanku\index.php
Line: 315
Function: require_once

ive been load the model name, controllers name globally through the autoload and internally through parent::__construct(); but it still error as above, i really need anyone's suggestion/advice for my problem because im still learning on how to make a qrcode feature in my application. Right now im using CodeIgniter 3.1.11

heres my controller - Make_qr.php

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

class Make_qr extends CI_Controller {
    public function __construct()
    {
      parent::__construct();
      $this->load->model('Make_qr_model');
    }

    public function index()
    {
        $data['vessel'] = $this->db->query('select * from vessel order by vessel_name')->result_array();
        $data['port'] = $this->db->query('select * from port order by port_name')->result_array();
        $this->load->view('template/header');
        $this->load->view('template/sidebar');
        $this->load->view('qrcode/make_qr',$data);
        $this->load->view('template/footer');

    }

    public function save()
    {
        $this->Make_qr_model->save($this->input->post());
    }
}

heres my model - Make_qr_model.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Make_qr_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
        $this->load->library('Ciqrcode');
    }

    function random_strings($length_of_string){ 

        $str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //abcdefghijklmnopqrstuvwxyz
        return substr(str_shuffle($str_result),0, $length_of_string); 
    }
//save data function setelah qrcode di generate
    public function save($data){
        $data = array_replace($data,
            array_fill_keys(
                array_keys($data, ""),
                NULL
            )
        );
        if(isset($data['quantity']) && count($data['quantity'])>0){
            for($i=1;$i<=$data['quantity'];$i++){
                $r = $this->random_strings(6);

                $this->load->library('Ciqrcode');

                $config['cacheable']    = true;
                $config['cachedir']     = './assets/';
                $config['errorlog']     = './assets/';
                $config['imagedir']     = './assets/images/';
                $config['quality']      = true;
                $config['size']         = '1024';
                $config['black']        = array(224,255,255);
                $config['white']        = array(70,130,180);
                $this->Ciqrcode->initialize($config);

                $image_name=$r.'.png'; //buat name dari qr code sesuai dengan random string

                $params['data'] = $r; //data yang akan di jadikan QR CODE
                $params['level'] = 'H'; //H=High
                $params['size'] = 1024;
                $params['savename'] = FCPATH.$config['imagedir'].$image_name; //simpan image QR CODE ke folder assets/images/
                $this->Ciqrcode->generate($params); // fungsi untuk generate QR CODE

                $img = file_get_contents(FCPATH.$config['imagedir'].$image_name, "r");


                $base64 = 'data:image/png;base64,'.base64_encode($img);

                $data_insert = array();
                $data_insert['vessel_id'] = $data['vessel_id'];
                $data_insert['port_id'] = $data['port_id'];
                $data_insert['key'] = $r;
                $data_insert['qr'] = $base64;
                $this->db->insert('qr_vessel', $data_insert);

                $vessel = $this->db->query('select * from vessel where id='.$data['vessel_id'])->row_array();

                echo "<table class='table table-striped m-table'>
<tbody>
    <tr>
      <td width='30%' style='background-color:black'>
        <img src=".$base64.">
      </td>
      <td width='70%' style='vertical-align:top'>
        Vessel Name : ".$vessel['vessel_name']."<br>
        Vessel Company : ".$vessel['company']."<br>
        Fishing Gear : ".$vessel['fishing_gear']."<br>
      </td>
    </tr>
    <tr>
      <td align='center'>
        ".$r."
      </td>
      <td align='center'></td>
    </tr>
</body>
</table>";
            }
            $notif['x'] = 'ok';
        }else{
            $notif['x'] = 'error';
            die('error');
        }

        return;

    }
}
?>

and heres my view - make_qr.php

<section class="content">
<form role="form" name="frm" action="<?=site_url('Make_qr/save')?>" method="post" id="frm">
<div id="container">
    <h1>Generate Fishing QR</h1>

    <div id="body">
        <table class="table table-striped m-table">
          <tbody>
            <tr>
              <td>Vessel</td>
              <td>
                <select id='vessel_id' name='vessel_id' style="width:250px" >
                    <option value="">Pilih</option>
                    <?php
                    foreach($vessel as $row){
                    ?>
                      <option value="<?=$row['id_vessel']?>"><?=$row['vessel_name']?> - <?=$row['company']?></option>
                    <?php
                    }
                    ?>
                </select>
              </td>
            </tr>

            <tr>
              <td>Port Departure</td>
              <td>
                <select id='port_id' name='port_id' style="width:250px" >
                    <option value="">Pilih</option>
                    <?php
                    foreach($port as $row){
                    ?>
                      <option value="<?=$row['id_port']?>"><?=$row['port_name']?></option>
                    <?php
                    }
                    ?>
                </select>
              </td>
            </tr>

            <tr>
              <td>QR Quantity</td>
              <td>
                <select id='quantity' name='quantity'>
                    <option value="">Pilih</option>
                    <?php
                    for($i=1;$i<=20;$i++){
                    ?>
                      <option value="<?=$i?>"><?=$i?></option>
                    <?php
                    }
                    ?>
                </select>
              </td>
            </tr>
          </tbody>
        </table>
        <div class="col-lg-3 m--margin-bottom-10-tablet-and-mobile">
            <span id='loading' style="display:none"><img src="<?php echo base_url(); ?>Assets/assets/images/ajax-loader_dark.gif"></span>
            <button type="button" class="btn btn-info" id="save">Generate</button>
        </div>
        <br>
        <div id='qrs'></div>
    </div>
</section>

i apologize for the long description of my errors it would be mean so much to me if i can get some advice & suggestion from here,

thankyou

Change model name Make_qr_model to Makeqr_model

Load library $this->load->model('Makeqr_model');

Load CodeIgniter-PHP-QR-Code library properly; clone git repo and paste folder inside application/libraries

Make sure GD2 PHP extension installed

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



class QRGenerator extends CI_Controller {
    function __construct(){
       Parent::__construct();
    }


    public function generate(){
        $this->load->library('ciqrcode');

        $params['data'] = 'This is a text to encode become QR Code';
        $params['level'] = 'H';
        $params['size'] = 10;
        $params['savename'] = FCPATH.'tes.png';
        $this->ciqrcode->generate($params);

        echo '<img src="'.base_url().'test.png" />';
    }

}    


在此处输入图像描述

Codeigniter model reference click

Hope it will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM