logo
logo
Sign in

CRUD operation in CodeIgniter | PHP Gurukul

avatar
PHPGurukul
CRUD operation in CodeIgniter | PHP Gurukul

In previous tutorial we learned how to insert data into data in CodeIgniter. In this tutorial we will learn how to fetch data from database in CodeIgniter.

1.Create a model(Read_data_Model.php) inside application/mode

 


<?php

Class Read_data_Model extends CI_Model{

public function readdata(){

$query=$this->db->select('fullName,mobileNumber,emailId,gender,address,termsCondition,postingDate')

              ->get('tblusers');

        return $query->result();

}

}

2.Create a controller(Read_data.php) inside application/controller


<?php

Class Read_data extends CI_Controller{

public function index(){

//load model

$this->load->model('Read_data_Model');

//load model function

$results=$this->Read_data_Model->readdata();

//loading view with passing result array

$this->load->view('read_data',['result'=>$results]);

}

}

Click Here : https://phpgurukul.com/how-to-fetch-data-in-codeigniter/

3. Create a view (read_data.php) inside application/view

 


<table class="table table-responsive">

<thead>

<tr>

<th>#</th>

<th>Name</th>

<th>Email id</th>

<th> Mobile Number</th>

<th> Gender</th>

<th> Address</th>

<th> Terms and Conditions</th>

<th>Posting date</th>

</tr>

</thead>

<tbody>

<?php

if(count($result)) {

$cnt=1;

foreach ($result as $row){

?>

<tr>

<td><?php echo htmlentities($cnt);?></td>

<td><?php echo htmlentities($row->fullName)?></td>

<td><?php echo htmlentities($row->emailId)?></td>

<td><?php echo htmlentities($row->mobileNumber)?></td>

<td><?php echo htmlentities($row->gender)?></td>

<td><?php echo htmlentities($row->address)?></td>

<td><?php echo htmlentities($row->address)?></td>

<td><?php echo htmlentities($row->postingDate)?></td>

</tr>

<?php

$cnt++;

} // end foreach

} else { ?>

<tr>

<td colspan="7">No Record found</td>

</tr>

<?php

}

?>

</tbody>

</table>

View—————————————

 

View Demo

How to run this script

  1. Download the zip file .
  2. Extract the zip and copy registrationci folder
  3. Paste in root directory(For xampp htdocs and for wamp www)
  4. Open your browser put http://localhost/phpmyadmin
  5. Create a database cidb
  6. Import sql file tblusers.sql
  7. Run your script http://localhost/registrationci


PHP Gurukul

Welcome to PHPGurukul. We are a web development team striving our best to provide you with an unusual experience with PHP. Some technologies never fade, and PHP is one of them. From the time it has been introduced, the demand for PHP Projects and PHP developers is growing since 1994. We are here to make your PHP journey more exciting and useful.


Website : https://phpgurukul.com

collect
0
avatar
PHPGurukul
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more