Showing posts with label codeigniter. Show all posts
Showing posts with label codeigniter. Show all posts

How to delete data in a table using CodeIgniter?

This block of code of will explains about how to delete data from table using CodeIgniter.
Which involves mainly three steps.
  1. View
  2. Controller
  3. Model

View
demodata.php
<html>
<head>
    <style>
        .container {
width: 500px;
height: 100px;
padding: 200px;
        }
table, th, td {
border: 1px solid black;
width:500px;
        }
    </style>
</head>
<body class="container">
<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Number</th>
        <th>Update</th>
    </tr>

<?php
    foreach ($k->result() as $row) {
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>' . $row->number . '</td>';
echo "<td><a href='updatedemodata?id=$row->id'>Update</a>
        <a href='deletedemodata?id=$row->id'> / Delete</a></td>";
    }
?>

</table>
</body>
</html>

Controller
Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {
public function deletedemodata(){
if(isset($_GET['id'])) {
if (($_GET['id'] !== '')) {
$id = $_GET['id'];
$this->levels->deletedemodata($id);
            } else {
echo 'error while deleting';
            }
        }
    }
}
?>

Model
level.php
<?php
class Levels extends CI_Model
{
public function deletedemodata($id)
    {
$this->load->database();
$this->db->where('id', $id);
$this->db->delete('demo');
echo 'Data Deleted successfully';
$this->load->helper('url');
        redirect('/welcome/demodata');
    }
}
?>



How to update data in a table using CodeIgniter?

This code gives you step by step explanation on how to update data in a table with CodeIgniter framework.
Model
Controller
View

Model
level.php
<?php
class Levels extends CI_Model {
public function selectdemodata_ind($id)
    {
$this->load->database();
$query = $this->db->get_where('demo', array('id =' => $id));
return $query;
    }

public function updatedemodata($data, $id)
    {
$this->load->database();
$this->db->where('id', $id);
$this->db->update('demo', $data);
echo 'Data Updated successfully';
$this->load->helper('url');
        redirect('/welcome/demodata');
    }
}
?>

Controller
welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function updatedemodata(){
$id = $_GET['id'];
$data_one['k'] = $this->levels->selectdemodata_ind($id);
$this->load->view('updatedemodata', $data_one);

$this->load->model('levels');
$name = $this->input->post('name');
$number= $this->input->post('number');
$data = array(
'name' => $name,
'number' => $number
);

if(isset($_POST['update'])) {
if ((name !== '')) {
$id = $_GET['id'];
$this->levels->updatedemodata($data, $id);
            } else {
echo 'Please fill fields';
            }
        }
    }
}
?>

View
updatedemodata.php
<html>
<head>
    <style>
        .container {
width: 200px;
height: 100px;
padding: 200px;

        }
label {
padding: 10px;
        }

        .btn {
float: right;
padding: 5px;
        }
    </style>
</head>

<?php foreach ($k->result() as $row) { ?>
<body class="container">
    <form method='POST'>
        <label>Name</label>
        <input type="text" name="name" 
value="<?php echo $row->name; ?>" ><br><br>
        <label>Number</label>
        <input type="text" name="number" 
value="<?php echo $row->number; ?>" >
        <input type="submit" class="btn" 
name="update" value="update">
    </form>
    </body>
<?php } ?>

</html>










How to fetch data from a table in CodeIgniter?

This code explains how to fetch data from a table in CodeIgniter.
This process majorly contains three steps


  1. Model
  2. Controller
  3. View
Once you aware of setting up the database you can move ahead with this example.


Model
Fetching data from database table.
Levels.php
<?php
class Levels extends CI_Model {
public function selectdemodata()
    {
$this->load->database();
$query = $this->db->get('demo');
return $query;
    }
}
?>


Controller
Storing the data in an array.
Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller
{
public function demodata()
    {
$data_demo['k'] = $this->levels->selectdemodata();
$this->load->view('demodata', $data_demo);
    }
}
?>


View
Displaying data in a table.
demodata.php
<html>
<head>
<style>
        .container {
width: 500px;
height: 100px;
padding: 200px;
        }
table, th, td {
border: 1px solid black;
width:300px;
        }
</style>
</head>
<body class="container">
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Number</th>
</tr>

<?php
    foreach ($k->result() as $row) {
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>' . $row->number . '</td>';
    }
?>

</table>
</body>
</html>

Result:

How to insert data in a table with CodeIgniter

CodeIgniter

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.
This snippet gives you the simple and best explanation to insert data into a MySQL table.
This process contains three major steps.
  1. View
  2. Controller
  3. Model
1. View 

demo.php (place it in your view folder)

Which is the form where you will insert data
<html>
<head>
    <style>
        .container {
            width: 200px;
            height: 100px;
            padding: 200px;

        }
        label {
            padding: 10px;
        }

        .btn {
            float: right;
            padding: 5px;
        }
    </style>
</head>
<body class="container">
<form method='POST'>

    <label>Name</label>
    <input type="text" name="name"><br><br>

    <label>Number</label>
    <input type="text" name="number"><br><br>

 <input class="btn" type="submit" name="submitdemo" value="Submit">

</form>
</body>
</html>

2.Controller

Welcome.php(place it in your controller folder)
which will act as a bridge between your model and controller.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller
{
    public function demo()
    {
        $this->load->model('levels');
        $name = $this->input->post('name');

        $number = $this->input->post('number');

        $data = array(
            'name' => $name,
            'number' => $number,
        );

        if (isset($_POST['submitdemo'])) {
            if (($name !== '')) {
                $this->levels->demo($data);
            } else {
                echo 'Please fill fields';
            }

        }
        $this->load->view('demo.php', $data);

    }
}
?>
3. Model
Levels.php(place it in your model folder)
Model will insert the data into table colected from form(demo.php)
through controller(Welcome.php)
<?php
class Levels extends CI_Model
{
    public function demo($data)
    {
        $this->load->database();
        $this->db->insert('demo', $data);
        echo 'Data inserted successfully';
    }
}
?>
MySql:
Run this query to create table in your database.

CREATE TABLE `demo` (
`id` int(11) NOT NULL,
`name` varchar(24) NOT NULL,
`number` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Labels

php (35) javascript (31) phpjavascript (30) jquery (23) html (20) mysql (14) database (9) codeigniter (4) json (4) bar chart (2) calendar (2) column chart (2) framework (2) google maps (2) query (2) tables (2) url (2) dropdown (1)

Popular Posts