Welcome Guest, Not a member yet? Register   Sign In
Poll: Is this post useful ???
You do not have permission to vote in this poll.
Yes
50.00%
1 50.00%
No
50.00%
1 50.00%
Total 2 vote(s) 100%
* You voted for this item. [Show Results]

Retrive Data from a database.(CRUD part 2)
#1
Thumbs Up 
(This post was last modified: 02-15-2015, 01:05 AM by Ujitha.)

This tutorial is about the retrive the pre-saved data into a table.

Step 01.

Frist create a database called School.
And a table called Student_details in side the School database.

Use the below mysql query--->

create table Student_details(ID varchar(20) primary key,Name varchar(20),B_day varchar(20),Mobile varchar(20) );

And add some data in to the table using below query.

INSERT INTO `student_details`(`ID`, `Name`, `B_day`, `Mobile`) VALUES ('001','Ujitha','1989 11 04','071-3546276')


Step 02.

Open database.php in Codeigniter/application/config directory.

Do the following changes to it and save.

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root'; //Enter your database username.
$db['default']['password'] = ''; //Enter your database passowrd.
$db['default']['database'] = 'school'; //Enter the database name.
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Step 03.

Open autoload.php and do the following modifications and save.

$autoload['libraries'] = array('database', 'session', 'xmlrpc','email');

$autoload['helper'] = array('url','form');

Step 04.

Open config.php do following midifications and save.

$config['base_url'] = 'http://localhost/Codeigniter/';

$config['encryption_key'] = '23456789';

Step 05.

Then do the bootstrp configarations as I told in previous tutorial.[This is not nessasary.]

Now we have to create the MVC architecture.

First create the controller and save it as studentData.php

<?php

class studentController extends CI_Controller
{

public function getStudentData()
{

$this->load->model('studentModel');
$data['info']=$this->studentModel->getSstudentData();
$this->load->view('StudentData',$data);

}

}


?>

Step 06 :

Now create the model as follows and save it as studentModel.php .

<?php

class studentModel extends CI_Model
{

public function getStudentData()
{

$student_details = $this->db->get('student_details');
return $student_details->result();

}


}

?>

Step 07 :

Now create a view as follows and save it as StudentData.php

<link href="<?php echo base_url('assets\css\bootstrp.css'); ?>" rel="stylesheet">
<link href="<?php echo base_url('assets\css\bootstrap.min.css'); ?>" rel="stylesheet">

<div class="container">
<div style="margin-left:20%;margin-top:20px">

<table class="table table-bordered">
<tr>
<td>ID</td>
<td>Name</td>
<td>Birth day</td>
<td>Mobile</td>
</tr>
<?php foreach($info as $details ):?>
<tr class="active">
<td><?php echo $details->ID ?></td>
<td><?php echo $details->Name ?></td>
<td><?php echo $details->B_day ?></td>
<td><?php echo $details->Mobile ?></td>
</tr>
<?php endforeach; ?>
</table>

</div>
</div>


Step 08 :

Now go to web browser and print below in address bar.

http://localhost/Codeigniter/index.php/s...tudentData

You will see a nice output.

See the attchment.
That is my style with Codeigniter Idea . Start with simple and ends in deep.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB