Codeigniter Tutorials
- What is codeigniter?
- Application_Architecture
- MVC Architecture
- HMVC Architecture
- Codeigniter Configuration
- Remove index.php from url in codeigniter
- MVC Concept
- View
- Alternate PHP Syntax for View Files
- Routing
- Codeigniter URL
- Get Current URL
- Previous page URL get
- Seo Friendly URL
- Slug Create in codeigniter
- What is _remap() function
- Remove controller name from url in codeigniter
- Codeigniter Controller Class
- Class Constructor
- GET $ POST method in Codeigniter
- Models
- Basepath, Apppath, FCPATH
- URI Segment
- Page Redirect
- Helper class
- Custom Helper class
- Form Helper
- Common Helper Functions
- Common Function
- Array Problems
- Call controller in Helper
- Add active class to menu using Helper class
- Custom Library
- Custom Library Example
- when to use get_instance()
- Codeigniter Hook
- how to work inline css in codeigniter
- Custom 404 page
- 404 custom error page
- Create custom config file in codeigniter
- How to set and get config item value
- How to Speed Up CodeIgniter App?
- Codeigniter Functions
- Session
- cookies
- How to Set & Get Tempdata in Codeigniter
- flash messages in Codeigniter
- Flashdata
- Encryption and Decryption In CodeIgniter
- Codeigniter security
- csrf token form security
- Password Hashing
- Form Validation
- Custom Validation
- Registration Form with validation
- Server Side Form Validation
- Validate Select Option Field
- Date Format Validation
- Date Format change in codeigniter
- Date Functions
- DOB Validation
- CI CRUD
- User SignUp
- User Login
- User Logout
- Login Account
- Login form with RememberMe
- Login Form with session
- User change password
- Change Password with Callback Validation to Check Old Password
- Forgot password
- Reset password
- Insert data in database
- Fetch data from database
- Update data in database
- Delete data in database
- File Upload
- Image Upload with resize Image
- Upload Multiple file and images
- Upload Multiple images with CRUD
- File and image update
- Upload Image Using Ajax.
- Email Send
- Email Send Using Email library
- Email Send Using SMTP Gmail
- Notification send
- store data in json format in DB
- Json parse
- Fetch data Using Ajax with Json data
- How to Show data Using Ajax with Json parse
- Get JSON Data from PHP Script using jQuery Ajax
- Insert data Using Ajax
- Submit data Using Ajax with form validation
- How to show data Using Ajax in codeigniter
- Insert & Update Using Ajax
- Registration Form With Validation Using Ajax in codeigniter
- Delete data Using Ajax Confirmation
- Delete All data Using checkbox selection
- Ajax CSRF Token
- Ajax Post
- Ajax serverside form validation
- Contact form using AJAX with form validation
- DataTable Using Ajax dynamically
- DataTables pagination using AJAX with Custom filter
- DataTables AJAX Pagination with Search and Sort in codeigniter
- DataTables in Codeigniter using Ajax
- Ajax Custom Serarch
- Ajax Live Data Search using Jquery PHP MySql
- Ajax Custom Serarch and sorting in datatable
- Dynamic Search Using Ajax
- Autocomplete using jquery ajax
- Jquery Ajax Autocomplete Search using Typeahead
- Dynamic Dependent Dropdown Using Ajax
- Dynamic Dependent Dropdown list Using Ajax
- Dynamic Dependent Dropdown in codeigniter using Ajax
- ajax username/email availability check using JQuery
- Check Email Availability Using Ajax
- Data Load on mouse scroll
- Ajax CI Pagination
- Pagination in codeigniter
- Ajax Codeigniter Pagination
- email exists or not using ajax with json
- CRUD using AJAX With Modal popup in CI
- Add / Show Data on modal popup using Ajax
- Modal popup Validation using Ajax
- Data show on Modal popup Using Ajax
- Add / Remove text field dynamically using jquery ajax
- How to Add/Delete Multiple HTML Rows using JavaScript
- Delete Multiple Rows using Checkbox
- Multiple Checkbox value
- Form submit using jquery Example
- REST & SOAP API
- Multi-Language implementation in CodeIgniter
- How to pass multiple array in view
- Captcha
- create zip file and download
- PhpOffice PhpSpreadsheet Library (Export data in excel sheet)
- data export in excel sheet
- Excel File generate in Codeigniter using PHPExcel
- Dompdf library
- tcpdf library
- Html table to Excel & docs download
- CI Database Query
- Database Query
- SQL Injection Prevention
- Auth Model
- Join Mysql
- Tree View in dropdown option list
- OTP Integration in codeigniter
- curl post
- download file using curl
- Sweet Alert
- Sweet alert Delete & Success
- Log Message in Codeigniter
- Menu & Submenu show dynamically
- Set Default value in input box
- Cron Jobs
- Stored Procedure
- Display Loading Image when AJAX call is in Progress
- Send SMS
- IP Address
- Codeigniter Tutorialspoint
- Website Link
- How To Create Dynamic Xml Sitemap In Codeigniter
- Paypal Payment Integration
- Get Latitude and Longitude From Address in Codeigniter Using google map API
- How To Create Simple Bar Chart In Codeigniter Using AmCharts?
- dynamic Highcharts in Codeigniter
- Barcode in Codeigniter
- Codeigniter Interview Questions
- Project
Home » Codeigniter »
Fetch data from database?
Fetch Data :-
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth_model extends CI_Model {
function __construct()
{
//call model constructor
parent::__construct();
}
function fetchtable()
{
$query = $this->db->get('baby');
return $query->result();
}
}
?>
employee_info.php :-
<table border="1">
<thead>
<th>ID</th>
<th>NAME</th>
<th>MEANING</th>
<th>GENDER</th>
<th>RELIGION</th>
<th>ACTION</th>
</thead>
<tbody>
<?php
foreach($this->Auth_model->fetchtable() as $row)
{
//name has to be same as in the database.
echo "<tr>
<td>$row->id</td>
<td>$row->name</td>
<td>$row->meaning</td>
<td>$row->gender</td>
<td>$row->religion</td>
</tr>";
}
?>
</tbody>
</table>
Employeeshow.php Fetch Data :-
public function show_employee()
{
//call the model function to get the department data
$userresult = $this->Auth_model->user_getemployee();
$data['userlist'] = $userresult;
//load the department_view
$this->load->view('employeedetails',$data);
}
Auth_model.php :-
public function user_getemployee()
{
$this->db->select('*');
$this->db->from('employee_details');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
********** OR ***************
/* $sql = 'select var_dept_name, var_emp_name from tbl_dept, tbl_emp where tbl_dept.int_hod = tbl_emp.int_id';*/
$sql = 'select * from employee';
$query = $this->db->query($sql);
$result = $query->result();
return $result;
}
Employeeshow_view.php :-
<div class="panel-body">
<?php
for ($i = 0; $i < count($userlist); ++$i)
{
?>
<div class="col-md-3">
<a href="<?php echo base_url()?>Emp_Info/get_employee_id/<?php echo $userlist[$i]->employee_id;?>"> <img src="<?php echo base_url()?>assets/images/<?php echo $userlist[$i]->profile_pics; ?>"
class="user-image img-responsive"/> <center><b><h4><?php echo $userlist[$i]->first_name; ?></b></h4></center>
</a></div>
<?php
}
?>
</div>
************* OR ******************
<?php
foreach($userlist as $row){
?>
<tr>
<td><?php echo $row->user_id;?></td>
<td><?php echo $row->password;?></td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->mobile;?></td>
</tr>
<?php } ?>
Curd.php :-
<?php
class Crud extends CI_Controller
{
public function __construct()
{
//call CodeIgniter's default Constructor
parent::__construct();
//load database libray manually
$this->load->database();
//load Model
$this->load->model('Crud_model');
}
//Display
public function displaydata()
{
$result['data']=$this->Auth_model->display_records();
$this->load->view('display_records',$result);
}
}
?>
:-
<?php
class Auth_model extends CI_Model
{
//View
function display_records()
{
$query=$this->db->query("select * from crud");
return $query->result();
}
}
display_records.php :-
<html>
<head>
<title>Display records</title>
</head>
<body>
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr style="background:#CCC">
<th>Sr No</th>
<th>First_name</th>
<th>Last_name</th>
<th>Email Id</th>
<th>Delete</th>
<th>Update</th>
</tr>
<?php
$i=1;
foreach($data as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->first_name."</td>";
echo "<td>".$row->last_name."</td>";
echo "<td>".$row->email."</td>";
echo "</tr>";
$i++;
}
?>
</table>
Fetch data from database :-
Controller.php
public function fetchdata()
{
//load the database
$this->load->database();
//load the model
$this->load->model('Auth_model');
//load the method of model
$data['mydata'] = $this->Auth_model->selectdata();
//return the data in view
$this->load->view('showdata_view',$data);
}
Auth_model :-
Auth_model.php
public function selectdata()
{
//data is retrive from this query
$qry = $this->db->get('country_table');
return $qry;
}
View :-
showdata_view.php
<html>
<head>
</head>
<body>
<table>
<tbody>
<tr>
<td>country id</td>
<td>Country name</td>
</tr>
<?php foreach ($mydata->result() as $row)
{
?>
<tr>
<td><?php echo $row->country_id;?></td>
<td><?php echo $row->country_name;?></td>
</tr>
<?php }?>
</tbody>
</table>
</body>
</html>
Codeigniter display data from database :-
controller.php
<?php
class Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->model('Auth_Model');
}
public function savedata()
{
$this->load->view('registration');
if($this->input->post('save'))
{
$n=$this->input->post('name');
$e=$this->input->post('email');
$m=$this->input->post('mobile');
$this->Hello_Model->saverecords($n,$e,$m);
redirect("Hello/dispdata");
}
}
public function dispdata()
{
$result['data']=$this->Auth_Model->displayrecords();
$this->load->view('display_records',$result);
}
}
?>
Auth_model :-
<?php
class Auth_Model extends CI_Model
{
function saverecords($name,$email,$mobile)
{
$query="insert into users values('','$name','$email','$mobile')";
$this->db->query($query);
}
function displayrecords()
{
$query=$this->db->query("select * from users");
return $query->result();
}
}
view :-
<!DOCTYPE html>
<html>
<head>
<title>Display Records</title>
</head>
<body>
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr style="background:#CCC">
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
</tr>
<?php
foreach($data as $row)
{
echo "<tr>";
echo "<td>".$row->name."</td>";
echo "<td>".$row->email."</td>";
echo "<td>".$row->mobile."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>