Search This Blog

07 January 2019

Codeigniter Using CRUD

Codeigniter Using CRUD


Change The Configuration in Config File:

Open root folder/Application/Config-> Autoload.php 

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

Open root folder/Application/Config-> database.php


'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'Your DB Name',
'dbdriver' => 'mysqli',

Create  One  Controller File :

<?php
Class Crud extends CI_Controller
{
}  ?>

Create One Model File:

<?php
Class Crud extends CI_Model
{
}  ?>

Controller:

<?php
/**
 * 
 */
Class Crud extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('Crud_model');
$this->load->helper('url');
}
public function select(){
$result['data']=$this->Crud_model->select();
$this->load->view('select',$result);
}
public function insert(){
$this->load->view('index');
if($this->input->post('s')){
$name=$this->input->post('nme');
$gender=$this->input->post('gen');
$res=$this->input->post('res');
$config['upload_path']="./images";
$config['allowed_types']="png|jpg|gif";
$this->load->library('upload',$config);
$file="Image";
if($this->upload->do_upload($file)){
             $img=$this->upload->data('file_name');
$this->db->query("Insert into crud values(NULL,'$name','$gender','$res','$img')");
}else{
echo $this->upload->display_errors();
}
}
}


public function delete(){
$del=$this->input->get('del');
$this->Crud_model->delete($del);
redirect('/Crud/select');
}
public function show(){
$id=$this->input->get('up');
$data['result']=$this->Crud_model->show($id);
$this->load->view('show',$data);
if($this->input->post('sub')){
$name=$this->input->post('nme');
$gender=$this->input->post('gen');
$res=$this->input->post('res');
$config['upload_path']="images";
$config['allowed_types']="jpg|png|gif";
$this->load->library('upload'config);
$file="image";
if($this->upload->do_upload($file)){
$img=$this->upload->data('file_name');
$sql="update crud set name='$name',gender='$gender',result='$res',image='$img' where id='$id'";
$this->db->query($sql);
redirect('/Crud/select');
}
}
}



}



?>

Model File:

index.php

   <?php

   Class Crud_model extends CI_Model{

    public function select()
      {
$sql="select * from crud";
$query=$this->db->query($sql);
return $query->result();
    }

    public function delete($del){
   $sql="delete from crud where id='$del'";
$query=$this->db->query($sql);
   }

  public function show($id){
   $sql="select * from crud where id='$id'";
   $query=$this->db->query($sql);
   return $query->result();
  }

  }
  ?>

View File:

<!DOCTYPE html>
<html>
<head>
<title>Crud</title>
</head>
<body>
<table>

<form method="post" enctype="multipart/form-data">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();  ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
<tr>
<td>Name</td><td><input type="text" name="nme" autocomplete="off" ></td>
</tr>
<tr>
<td>gender</td><td><input type="radio" name="gen" value="Male" autocomplete="off" >Male
        <input type="radio" name="gen" value="Female" autocomplete="off" >Female
</td>

</tr>
<tr>
<td>Result</td><td><select name="res"><option>Select Result</option><option>Pass</option><option>Fail</option></select></td>
</tr>
<tr>
<td>Image</td><td><input type="file" name="Image" ></td>
</tr>
</table>
<input type="submit" name="s" value="Insert">
</form>

</body>
</html>

Select.php

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr><th>Name</th><th>gender</th><th>Result</th><th>Image</th></tr>
<?php foreach ($data as $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->gender; ?></td>
<td><?php echo $row->result; ?></td>
<td><img width="100px" height="100px" src="<?php echo base_url()."images/".$row->image; ?>"></td>
    <td><a href='<?php echo "show?up=".$row->id; ?>'>Update</a></td>
<td><a href='<?php echo "delete?del=".$row->id;  ?>'>delete</a></td>
</tr>
<?php } ?>
</table>
</body>
</html>

Update.php:

<!DOCTYPE html>
<html>
<head>
<title>Crud</title>
</head>
<body>
<table>

<form method="post" enctype="multipart/form-data">
<?php foreach ($result as $row) {
?>
<input type="hidden" value="<?php  echo $row->id; ?>">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
<tr>
<td>Name</td><td><input value="<?php echo $row->name ?>" type="text" name="nme" autocomplete="off" ></td>
</tr>
<tr>
<?php if($row->gender=="Male") {?>
<td>gender</td><td><input checked="true" type="radio" name="gen" value="Male" autocomplete="off" >Male
        <input type="radio" name="gen" value="Female" autocomplete="off" >Female
</td>
<?php }else{?>
<td>gender</td><td><input  type="radio" name="gen" value="Male" autocomplete="off" >Male
        <input type="radio" checked="true" name="gen" value="Female" autocomplete="off" >Female
</td>
<?php }?>
</tr><?php if($row->result=="Pass"){?>
<tr>
<td>Result</td><td><select name="res"><option>Select Result</option><option selected="true">Pass</option><option>Fail</option></select></td>
</tr><?php } else{ ?>
<tr>
<td>Result</td><td><select name="res"><option>Select Result</option><option>Pass</option><option selected="true">Fail</option></select></td>
<?php } ?>
<tr>
<td>Image</td><td><img width="200px" height="200px" src="<?php echo base_url().'images/'.$row->image;?>"></td>
</tr>
<tr>
<td>Select Image</td><td><input type="file" name="image"></td>
</tr>
</table>
<?php } ?>
<input type="submit" name="sub" value="Update">
</form>

</body>
</html>
---------------------------------------------------
Simple Steps:

$this->db->query($sql)->row //single record

// IF you Want to get More than Single Row use  (row)

single rows count 

$q=$this->db->query($sql)->row
count($q)

-----------------------------------------------------------------------

$this->db->query($sql)->rows // multiple records

// IF you Want to get More than Multiple Row use  (rows)

multiple rows Count

$q=$this->db->query($sql)->rows
$cnt=$q->num_rows();

Outputs:






Jquery or Javascript Start Exam Time

 <script> function startTimer() {      var date = "<?php echo $date ?>"; // dynamic date      var time = "<?...