Case Study : Data Export Mysql Database into a PDF file in Code Igniter.
Requirements:
1. Webserver package, already installed.
2. Code Igniter Bundle.
3. Cezpdf library –> download here

Sometimes, when we make an application, we will need to exported data into a pdf file (apart from the excel file), for example to create letters or reports. With a library cezpdf (by Wayne Munro), make a report as a pdf not be too difficult.

A. Preparing Database
1. Create a database with the name db_tutorial.
2. Create a table named tb_member, with the structure of the table below,

3. Insert a few sample data in tb_member, such as the data below,

4. Okay, done with the database.

B. Prepare files and directory structure
1. Install CodeIgniter, by extracting the bundle CI and place it in your document root.
2. Set the database settings (system-config-application-database.php) by setting the database to be used (db name: db_tutorial)
3. Set the url base config (system-config-application-config.php)
4. Put the files cezpdf.php and class.pdf.php (the downloaded library Cezpdf above) in a folder system-application-libraries
5. Put the files in the folder pdf_helper.pdf-helper-application system.
6. Ok: D

C. Make a model
1. Type the following script,

<?php
  class Member_model extends Model {
	function Member_Model() {
    	parent::Model();
    }
	function alldata()
	{
		$this->db->select('*');
		$this->db->from('tb_member');
		$this->db->order_by('no','ASC');
		$getData = $this->db->get();
		if($getData->num_rows() > 0)
		return $getData->result_array();
		else return null;
	}
}
?>

2. Save with the name of the folder system member_model.php-models-application

D. Make Controller
1. Type the following script,

<?php
class Member_con extends Controller {
	public function __construct() {
		parent::__construct();
		$this->load->model('member_model');
		$this->load->helper('url');
	}
	public function index() {
		$data['member'] = $this->member_model->alldata(); 
		$this->load->view('member_view', $data);
	}
	
	function topdf () {
		$this->load->library('cezpdf');
		$this->load->helper('pdf');
		prep_pdf();
		$data['member']= $this->member_model->alldata(); 
		$titlecolumn = array(
							'no' => 'no',
							'name' => 'name',
							'address' => 'address'
		);
		$this->cezpdf->ezTable($data['member'], $titlecolumn,'Member Data');
		$this->cezpdf->ezStream();
	}
}
?>

2. Save with the name of the folder system member_model.php-models-application

E. Create View
1. Type the following script,

<h4>Member Data</h4>
<?php if(count($member) > 0) { ?>
<table border="1">
<tr>
<th>No</th>
<th>Name</th>
<th>Address</th>
</tr>						
<?php
foreach($member as $rows) {
echo "<tr>";
echo "
<td>".  $rows['no']."</td>
<td>". $rows['name'] ."</td>
<td>". $rows['address'] ."</td>
"; } ?>
</table>
<?php } ?>
<br> <br>
<a href='<?php echo base_url(); ?>index.php/member_con/topdf'><span 

style='color:green;'>Export to Pdf</span></a>

2. Save with the name of the folder system member_model.php-models-application

F. Testing code
1. Open http://localhost/ci_tutor/index.php/member_con. You will see the data that exists in tb_member like the picture below,

2. Click the link export to pdf to export data into pdf file.
3. So automatically your browser (or download manager) will download the pdf file.
4. Open the pdf files, you will see data from tb_member displayed in pdf file:) See the picture below.

Happy coding: D
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di itx.web.id boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.


Related Articles:

Artikel terkait:

: PHP

About the author

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she lo

8 Comments

Tinggalkan Balasan

Alamat surel Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: