Senin, 03 Agustus 2020

Membuat Qrcode dengan library Ciqrcode

Assalamualaikum Sobat Semua, 


Postingan kali ini akan membuat qrcode dengan mengunakkan framework Codeigniter dan library Ciqrcode langsung saja ya sobat semua

Peralatan yang di butuhkan

  1. Download Framework Codeigniter versi 3 Download Disini
  2. Download Library Ciqrcode Download Disini
  3. Sudah Terinstall xampp Pengguna Windows

Implementasi Code

  1. extract folder framework codeigniter, kemudian rename menjadi qrcode, taruh di dalam folder xampp/htdocs
  2. Buat File Controller Qrcode di dalam folder controller
  3. Buat File Model qrcode_model didalam folder model
  4. Buat File Qrcode_view didalam folder view
  5. Buat Database misal mahasiswa
Catatan untuk folder controller, model dan view sudah di sediakan oleh framework codeigniter yang sudah sobat download

File Qrcode Controller, Copy Code dibawah

<?php 
class Qrcode extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('qrcode_model'); //pemanggilan model mahasiswa
	}

	function index(){
		$data['data']=$this->qrcode_model->get_all_mahasiswa();
		$this->load->view('qrcode_view',$data);
	}

	function simpan()
	{
		$nim	= $this->input->post('nim');
		$nama	= $this->input->post('nama');
		$prodi	= $this->input->post('prodi');

		$this->load->library('ciqrcode'); //pemanggilan library QR CODE

		$config['cacheable']	= true; //boolean, the default is true
		$config['cachedir']		= './assets/'; //string, the default is application/cache/
		$config['errorlog']		= './assets/'; //string, the default is application/logs/
		$config['imagedir']		= './assets/images/'; //direktori penyimpanan qr code
		$config['quality']		= true; //boolean, the default is true
		$config['size']			= '1024'; //interger, the default is 1024
		$config['black']		= array(224,255,255); // array, default is array(255,255,255)
		$config['white']		= array(70,130,180); // array, default is array(0,0,0)
		$this->ciqrcode->initialize($config);

		$image_name=$nim.'.png'; //buat name dari qr code sesuai dengan nim

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

		$this->qrcode_model->simpan_mahasiswa($nim,$nama,$prodi,$image_name); //simpan ke database
		redirect('mahasiswa'); //redirect ke mahasiswa usai simpan data
	}
}


File Qrcode_model, Copy Code dibawah


<?php
class Qrcode_model extends CI_Model{

	function get_all_mahasiswa(){
		$hasil=$this->db->get('mahasiswa');
		return $hasil;
	}
	
	function simpan_mahasiswa($nim,$nama,$prodi,$image_name){
		$data = array(
			'nim' 		=> $nim,
			'nama' 		=> $nama,
			'prodi' 	=> $prodi, 
			'qr_code' 	=> $image_name
		);
		$this->db->insert('mahasiswa',$data);
	}

}

File Qrcode_view, Copy Code dibawah


<!DOCTYPE html>
<html>
<head>
	<title>Data Mahasiswa</title>
	<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.css'?>">
</head>
<body>
	<div class="container">
		<div class="row">
			<h2>Data <small>Mahasiswa</small></h2>
			<button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal">Add New</button>
			<table class="table table-striped">
				<thead>
					<tr>
						<th>NIM</th>
						<th>NAMA</th>
						<th>PRODI</th>
						<th>QR CODE</th>
					</tr>
				</thead>
				<tbody>
					<?php foreach($data->result() as $row):?>
					<tr>
						<td style="vertical-align: middle;"><?php echo $row->nim;?></td>
						<td style="vertical-align: middle;"><?php echo $row->nama;?></td>
						<td style="vertical-align: middle;"><?php echo $row->prodi;?></td>
						<td>
							<a href="<?= base_url().'index.php/qrcode/edit/'.$row->qr_code; ?>">
							<img style="width: 100px;" src="<?php echo base_url().'assets/images/'.$row->qr_code;?>"></a>
						</td>
					</tr>
					<?php endforeach;?>
				</tbody>
			</table>
		</div>
	</div>

	<!-- Modal add new mahasiswa-->
	<form action="<?php echo base_url().'index.php/mahasiswa/simpan'?>" method="post">
		<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
		  <div class="modal-dialog" role="document">
		    <div class="modal-content">
		      <div class="modal-header">
		        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
		        <h4 class="modal-title" id="myModalLabel">Add New Mahasiswa</h4>
		      </div>
		      <div class="modal-body">
		    
		          <div class="form-group">
		            <label for="nim" class="control-label">NIM:</label>
		            <input type="text" name="nim" class="form-control" id="nim">
		          </div>
		          <div class="form-group">
		            <label for="nama" class="control-label">NAMA:</label>
		            <input type="text" name="nama" class="form-control" id="nama">
		          </div>
	       		  <div class="form-group">
		            <label for="prodi" class="control-label">PRODI:</label>
		            <select name="prodi" class="form-control" id="prodi">
		            	<option>Sistem Informasi</option>
		            	<option>Sistem Komputer</option>
		            	<option>Manajemen Informatika</option>
		            </select>
		          </div>
	        
		      </div>
		      <div class="modal-footer">
		        <button type="button" class="btn btn-default" data-dismiss="modal">Tutup</button>
		        <button type="submit" class="btn btn-primary">Simpan</button>
		      </div>
		    </div>
		  </div>
		</div>
	</form>

	<script type="text/javascript" src="<?php echo base_url().'assets/js/jquery-2.1.4.min.js'?>"></script>
	<script type="text/javascript" src="<?php echo base_url().'assets/js/bootstrap.js'?>"></script>
</body>
</html>

Buat Database db_qrku, buat tabel mahasiswa
Code Tabel mahasiswa


CREATE TABLE IF NOT EXISTS `mahasiswa` (
  `nim` varchar(15) NOT NULL,
  `nama` varchar(50) DEFAULT NULL,
  `prodi` varchar(40) DEFAULT NULL,
  `qr_code` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`nim`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `mahasiswa` (`nim`, `nama`, `prodi`, `qr_code`) VALUES
('1210158', 'Sigit', 'Sistem Informasi', '1210158.png'),
('1210159', 'Joko', 'Manajemen Informatika', '1210159.png'),
('1210160', 'Andi', 'Sistem Komputer', '1210160.png');


Jika Sudah Buka Browser dan ketik url sesuai dengan nama folder sobat, contoh: localhost/qrcode

Kurang Lebih Hasilnya akan seperti ini


Mungkin Cukup Itu dulu dan semoga bermanfaat. Download Full Source Code

dipostingan selanjutnya nanti kita belajar edit dan hapus data

Tidak ada komentar:
Tulis komentar

Kategori