CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

28 September, 2018

Retrieve data from database using CodeIgniter framework

 Programing Coderfunda     September 28, 2018     codeigniter     No comments   

Retrieve data from database using CodeIgniter framework


In this example we will discuss about how to retrieve a record or data from MySQL database using CodeIgniter framework PHP.
For retrieve data from MySQL database using CodeIgniter framework first we have to create a table in data base.
After create a table in the MySQL database you need to insert record or data on it.If you want to know how to insert data in CodeIgniter framework please visit the link : Insert data in CodeIgniter.
The SELECT statement is used to retrieve data from one or more tables:
The SQL query for retrieve specific column.
SELECT column_name(s) FROM table_name
or we can use the * character to retrieve ALL columns from a table:
SELECT * FROM table_name
To learn more about SQL, please visit our SQL tutorial.


We use 3 file for retrieve students data.
  1. Crud.php Path: application\controllers\Crud.php
  2. Crud_model.php Path: application\models\Crud_model.php
  3. display_records.php Path: application\views\insert.php

Sql Table

CREATE TABLE crud (
`id` int(11) NOT NULL,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL,
PRIMARY KEY (id)
);

Crud.php

<?php 
class Crud extends CI_Controller 
{
public function __construct()
{
//call CodeIgniter's default Constructorparent::__construct();
//load database libray manually$this->load->database();
//load Model$this->load->model('Crud_model');
}
//Displaypublic function displaydata()
{
$result['data']=$this->Crud_model->display_records();
$this->load->view('display_records',$result);
}
}
?>

student_fetch.php

<?php
class Crud_model extends CI_Model 
{
//Viewfunction 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>
Now run the program on your browser with the below URL:
http://localhost/codeIgniter/index.php/Crud/displaydata
After fetch data the table look like this.
Idfirst namelast nameEmail Id
1DivyasundarSahudivyasundar@gmail.com
2HritikaSahuhritika@gmail.com
3MilanJenamilanjena@gmail.com


Next ❯
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Retrieve data from database using CodeIgniter framework

 Programing Funda     September 28, 2018     codeigniter     No comments   

Retrieve data from database using CodeIgniter framework


In this example we will discuss about how to retrieve a record or data from MySQL database using CodeIgniter framework PHP.
For retrieve data from MySQL database using CodeIgniter framework first we have to create a table in data base.
After create a table in the MySQL database you need to insert record or data on it.If you want to know how to insert data in CodeIgniter framework please visit the link : Insert data in CodeIgniter.
The SELECT statement is used to retrieve data from one or more tables:
The SQL query for retrieve specific column.
SELECT column_name(s) FROM table_name
or we can use the * character to retrieve ALL columns from a table:
SELECT * FROM table_name
To learn more about SQL, please visit our SQL tutorial.


We use 3 file for retrieve students data.
  1. Crud.php Path: application\controllers\Crud.php
  2. Crud_model.php Path: application\models\Crud_model.php
  3. display_records.php Path: application\views\insert.php

Sql Table

CREATE TABLE crud (
`id` int(11) NOT NULL,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL,
PRIMARY KEY (id)
);

Crud.php

<?php 
class Crud extends CI_Controller 
{
public function __construct()
{
//call CodeIgniter's default Constructorparent::__construct();
//load database libray manually$this->load->database();
//load Model$this->load->model('Crud_model');
}
//Displaypublic function displaydata()
{
$result['data']=$this->Crud_model->display_records();
$this->load->view('display_records',$result);
}
}
?>

student_fetch.php

<?php
class Crud_model extends CI_Model 
{
//Viewfunction 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>
Now run the program on your browser with the below URL:
http://localhost/codeIgniter/index.php/Crud/displaydata
After fetch data the table look like this.
Idfirst namelast nameEmail Id
1DivyasundarSahudivyasundar@gmail.com
2HritikaSahuhritika@gmail.com
3MilanJenamilanjena@gmail.com


Next ❯
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

How to insert data in database - CodeIgniter framework

 Programing Coderfunda     September 28, 2018     codeigniter     No comments   

In this example we are going to show you how to insert data in database using CodeIgniter framework PHP.
For insert data in MySQL using CodeIgniter first we have to create a table in data base.
The INSERT INTO statement is used to insert new data to a MySQL table:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
To learn more about SQL, please visit our SQL tutorial.
For creating table the SQL query is:

SQL Query

CREATE TABLE crud (
`id` int(11) NOT NULL,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL,
PRIMARY KEY (id)
);
Here we using 3 files for insert data in MySQL:
  • Crud.php Path: codeIgniter\application\controllers\Crud.php
  • Crud_model.php Path: codeIgniter\application\models\Crud_model.php
  • insert.php Path: codeIgniter\application\views\insert.php

Crud.php (Controller)

<?php 
class Crud extends CI_Controller 
{
public function __construct()
{
//call CodeIgniter's default Constructorparent::__construct();
//load database libray manually$this->load->database();
//load Model$this->load->model('Crud_model');
}
//Insertpublic function savedata()
{
//load registration view form$this->load->view('insert');
//Check submit button if($this->input->post('save'))
{
$first_name=$this->input->post('first_name');
$last_name=$this->input->post('last_name');
$email=$this->input->post('email');
$this->Crud_model->saverecords($first_name,$last_name,$email);
echo "Records Saved Successfully";
}
}
}
?>

Crud_model.php (Model)

<?php
class Crud_model extends CI_Model 
{
//Insertfunction saverecords($first_name,$last_name,$email)
{
$query="insert into crud values('','$first_name','$last_name','$email')";
$this->db->query($query);
}
}

insert.php (View)

<!DOCTYPE html> 
<html>
<head>
<title>Registration form</title>
</head>
<body>
<form method="post">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">First Name </td>
<td width="329"><input type="text" name="first_name"/></td>
</tr>
<tr>
<td>Last Name </td>
<td><input type="text" name="last_name"/></td>
</tr>
<tr>
<td>Email ID </td>
<td><input type="email" name="email"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="save" value="Save Data"/></td>
</tr>
</table>
</form>
</body>
</html>
Run the program on your browser with URL:
http://localhost/codeIgniter/index.php/Crud/savedata

Here codeIgniter is my folder name. Put your folder name instead of codeIgniter.Rest of things are same.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

How to insert data in database - CodeIgniter framework

 Programing Funda     September 28, 2018     codeigniter     No comments   

In this example we are going to show you how to insert data in database using CodeIgniter framework PHP.
For insert data in MySQL using CodeIgniter first we have to create a table in data base.
The INSERT INTO statement is used to insert new data to a MySQL table:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
To learn more about SQL, please visit our SQL tutorial.
For creating table the SQL query is:

SQL Query

CREATE TABLE crud (
`id` int(11) NOT NULL,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL,
PRIMARY KEY (id)
);
Here we using 3 files for insert data in MySQL:
  • Crud.php Path: codeIgniter\application\controllers\Crud.php
  • Crud_model.php Path: codeIgniter\application\models\Crud_model.php
  • insert.php Path: codeIgniter\application\views\insert.php

Crud.php (Controller)

<?php 
class Crud extends CI_Controller 
{
public function __construct()
{
//call CodeIgniter's default Constructorparent::__construct();
//load database libray manually$this->load->database();
//load Model$this->load->model('Crud_model');
}
//Insertpublic function savedata()
{
//load registration view form$this->load->view('insert');
//Check submit button if($this->input->post('save'))
{
$first_name=$this->input->post('first_name');
$last_name=$this->input->post('last_name');
$email=$this->input->post('email');
$this->Crud_model->saverecords($first_name,$last_name,$email);
echo "Records Saved Successfully";
}
}
}
?>

Crud_model.php (Model)

<?php
class Crud_model extends CI_Model 
{
//Insertfunction saverecords($first_name,$last_name,$email)
{
$query="insert into crud values('','$first_name','$last_name','$email')";
$this->db->query($query);
}
}

insert.php (View)

<!DOCTYPE html> 
<html>
<head>
<title>Registration form</title>
</head>
<body>
<form method="post">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">First Name </td>
<td width="329"><input type="text" name="first_name"/></td>
</tr>
<tr>
<td>Last Name </td>
<td><input type="text" name="last_name"/></td>
</tr>
<tr>
<td>Email ID </td>
<td><input type="email" name="email"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="save" value="Save Data"/></td>
</tr>
</table>
</form>
</body>
</html>
Run the program on your browser with URL:
http://localhost/codeIgniter/index.php/Crud/savedata

Here codeIgniter is my folder name. Put your folder name instead of codeIgniter.Rest of things are same.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Session with example CodeIgniter framework

 Programing Coderfunda     September 28, 2018     codeigniter     No comments   

Session with example CodeIgniter framework

In CodeIgniter or any other framework session is used to store information (in variables) and used it through out the application.

Initializing Session

To store data in session first of all we need to initialize the session.
In PHP we initialize the session by simply write the session_start(); function.
But in CodeIgniter We can do that by executing the following line in constructor.
$this->load->library(‘session');

Add data to session

In php we simply use $_SESSION super global variable to add value to a session.
$_SESSION[‘key’]=value;
Same thing can be done in CodeIgniter as shown below.
$this->session->set_userdata(‘session name', ‘any value’);
set_userdata() function takes two arguments. The first argument, session name, is the name of the session variable and here any value is the value assign to the session. We can use set_userdata() function to pass array to store values as shown below.
$newdata = array( 
'username' => 'johndoe', 
'email' => 'johndoe@some-site.com', 
'logged_in' => TRUE
); 
$this->session->set_userdata($newdata);

Remove Session Data

In PHP, we remove the data by using unset() function.
unset($_SESSION[‘some_name’]);
But in CodeIgniter we use unset_userdata() function that will remove data from session.
$this->session->unset_userdata(‘some_name');
If you want to remove more that one value and a array data then you can use the same function unset_userdata().
$this->session->unset_userdata($array_items);

Fetch Session Data

After set a data to a session we also retrieve it for our use. Userdata() function will be used for this purpose.
$name = $this->session->userdata(‘name');
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Session with example CodeIgniter framework

 Programing Funda     September 28, 2018     codeigniter     No comments   

Session with example CodeIgniter framework

In CodeIgniter or any other framework session is used to store information (in variables) and used it through out the application.

Initializing Session

To store data in session first of all we need to initialize the session.
In PHP we initialize the session by simply write the session_start(); function.
But in CodeIgniter We can do that by executing the following line in constructor.
$this->load->library(‘session');

Add data to session

In php we simply use $_SESSION super global variable to add value to a session.
$_SESSION[‘key’]=value;
Same thing can be done in CodeIgniter as shown below.
$this->session->set_userdata(‘session name', ‘any value’);
set_userdata() function takes two arguments. The first argument, session name, is the name of the session variable and here any value is the value assign to the session. We can use set_userdata() function to pass array to store values as shown below.
$newdata = array( 
'username' => 'johndoe', 
'email' => 'johndoe@some-site.com', 
'logged_in' => TRUE
); 
$this->session->set_userdata($newdata);

Remove Session Data

In PHP, we remove the data by using unset() function.
unset($_SESSION[‘some_name’]);
But in CodeIgniter we use unset_userdata() function that will remove data from session.
$this->session->unset_userdata(‘some_name');
If you want to remove more that one value and a array data then you can use the same function unset_userdata().
$this->session->unset_userdata($array_items);

Fetch Session Data

After set a data to a session we also retrieve it for our use. Userdata() function will be used for this purpose.
$name = $this->session->userdata(‘name');
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Hello world example - CodeIgniter framework

 Programing Coderfunda     September 28, 2018     No comments   

In this example we will discuss about Hello world example CodeIgniter framework PHP.
We use two file for hello world example.
  1. Hello.php (CodeIgniter\application\controllers\Hello.php )
  2. hello_world.php (CodeIgniter\application\views\hello_world.php)

Hello.php

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
class Hello extends CI_Controller { 
public function index() 
{ 
$this->load->view('hello_world'); 
} 
} 
?>

hello_world.php

<!DOCTYPE html>
<html>
<head>
<title>Hello World !</title>
</head>
</body>
<h1>Hello World !</h1>
</body>
</html>

Now run the program on your browser with the below URL:
http://localhost/CodeIgniter/index.php/Hello
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Hello world example - CodeIgniter framework

 Programing Funda     September 28, 2018     No comments   

In this example we will discuss about Hello world example CodeIgniter framework PHP.
We use two file for hello world example.
  1. Hello.php (CodeIgniter\application\controllers\Hello.php )
  2. hello_world.php (CodeIgniter\application\views\hello_world.php)

Hello.php

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
class Hello extends CI_Controller { 
public function index() 
{ 
$this->load->view('hello_world'); 
} 
} 
?>

hello_world.php

<!DOCTYPE html>
<html>
<head>
<title>Hello World !</title>
</head>
</body>
<h1>Hello World !</h1>
</body>
</html>

Now run the program on your browser with the below URL:
http://localhost/CodeIgniter/index.php/Hello
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Older Posts Home

Meta

Popular Posts

  • Write API Integrations in Laravel and PHP Projects with Saloon
    Write API Integrations in Laravel and PHP Projects with Saloon Saloon  is a Laravel/PHP package that allows you to write your API integratio...
  • Credit card validation in laravel
      Validation rules for credit card using laravel-validation-rules/credit-card package in laravel Install package laravel-validation-rules/cr...
  • iOS 17 Force Screen Rotation not working on iPAD only
    I have followed all the links on Google and StackOverFlow, unfortunately, I could not find any reliable solution Specifically for iPad devic...
  • C++ in Hindi Introduction
    C ++ का परिचय C ++ एक ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग लैंग्वेज है। C ++ को Bjarne Stroustrup द्वारा विकसित किया गया था। C ++ में आने से पह...
  • Python AttributeError: 'str' has no attribute glob
    I am trying to look for a folder in a directory but I am getting the error.AttributeError: 'str' has no attribute glob Here's ...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (2)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda