CodeIgniter Basic With CRUD Operation

As we discuessed in installationa and configure post, how to installation with secure encryption key in config file. Now we are readdy for make any application in codegniter. In this post, i am explain how to create first application in codegniter.

The basic steps are given below:

Make Default controller name. by default it is welcome. You can change it in Rout file file which location is application/config/route.php.

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

You also needs to change base url in config file.

$config['base_url'] = '';
$config['index_page'] = 'index.php';
$config['uri_protocol']	= 'REQUEST_URI';

You can define base_url as like http://localhost/iGD/. As you need to remove index.php then you should black in the feild value. And url_protocol value have auto,QUERY_STRING,REQUEST_URI,PATH_INFO.

You can also define allowed and access denied for any url in the .htaccess file.

First to make database and table which also needs to define in database file.First step is to make model file which must include databse. Then make all the function about get, edit, update and delete function for database operations.Please check below code of model:


class demo_model extends CI_Model
{
	public function __construct()
        {
                // Call the CI_Model constructor
                parent::__construct();
		$this->load->database();
        }
	public function list_records()
	{
		$query=$this->db->get('demo');
		return $query->result();
	}
	public function record($id)
	{
		$this->db->select('*');
		$this->db->from('demo');
		$this->db->where('id',$id);
		$query=$this->db->get();
		return $query->result();
	}
	public function delete($id)
	{
		$this->db->where('id',$id);
		$this->db->delete('demo');
		$query=$this->db->trans_complete();
		return $query->result();
	}
}

Then include model file to the controller file. First to make index controller for display record. It also needs to call retrived functions of model class, which is given below:

class Welcome extends CI_Controller {

	public function __construct()
        {
                // Call the CI_Model constructor
                parent::__construct();
                $this->load->model('demo_model');
        }
	
	public function index()
	{
		$data['list_records']=$this->demo_model->list_records();
		//print_r($data);
		 
		$this->load->view('welcome_message',$data);
	}
}

Then make view file which name is call in the controller function. Our example it is welcome_message.php. We also needs to be echo or display all the message or data from controller should be displayed.

public function edit()
	{
		$id=$this->input->get('id');
		//echo $id;
		$data['list_records']=$this->demo_model->record($id);
		$this->load->view('edit_form',$data);
		
	}
 public function update()
        {
             $id = $this->input->post('id');
              $alink = trim($this->input->post('alink'));
        
           //print_r($_FILES);
           $this->db->select('image');
            $this->db->from('demo');
            $this->db->where_in('id', $id);
            $query = $this->db->get();
            foreach ($query->result() as $user) {
                $old_thumb = $user->image;
                $old_puzzle_file = $user->image;
            }
          $thumb_img = $_FILES['img']['name'];
            $thumb_img_type = $_FILES['img']['type'];
            $thumb_img_size = $_FILES['img']['size'];
            $thumb_img_name = $_FILES['img']['tmp_name'];
          //  echo $thumb_img;
            $base="localhost/chirag/";
            if ($thumb_img != "") {
               // echo "hi";
                $thumb_img_imgSize = @getImageSize($thumb_img_name);
                $thumb_img_width = $thumb_img_imgSize[0];
                $thumb_img_height = $thumb_img_imgSize[1];
                if ( (($thumb_img_type == "image/x-png") || ($thumb_img_type == "image/png")||($thumb_img_type == "image/jpeg"))) {
                   //echo $old_thumb;
                    $file_name=rand();
                   // $file_name.=
                    $new_file_name = str_replace($base . 'image/', '', $thumb_img);
                    $thumb_path = "" . $new_file_name;
                        echo $thumb_path;

                    if (copy($_FILES['img']['tmp_name'], $base."public/image/" . $thumb_path)) {
                         echo "hi";
                        //date_default_timezone_set("" . SERVER_DEFAULT_TIMEZONE . "");
                        $time = $dateNow = date('Y-m-d H:i:s', time());
                        $thumb_path = $base . $thumb_path;
                        $data['thumb_image_url'] = $thumb_path;
                        $data['time'] = $time;
                         $this->db->where('id', $id);
                             $data = array(
                                 'link' => $alink,
                                  'image'=>$new_file_name

                                );
                         $this->db->update('demo', $data);
                        echo "image upload successfully";
                         $response = array("MESSAGE" => "REQUIRED_FIELD_MISSING", "STATUS" => FALSE);
                                     header("Location:../../");
//redirect("www.google.com");
echo "Update successfully";
                                   //   redirect($uri="index", $method="auto", $code=NULL);
                        //redirect('welcome' );
//$this->load->view('welcome_message');
            return $this->output->set_output(json_encode(array("JSON_ROOT_OBJECT" => $response)));
                        //redirect('welcome');
                    } else {
                        //echo "2";
						 $this->db->where('id', $id);
                             $data = array(
                                 'link' => $alink,
                                  'image'=>'/chirag/public/image/'.$new_file_name

                                );
                         $this->db->update('demo', $data);
                   //     $_SESSION['not_change'] = "
Then we also make delte function in the controller file. And it must be call from list view. So it delete record from database. We also call the delete model function from controller.
public function delete()
	{
		$id=$this->input->get('id');
		//echo $id;
		$data['list_records']=$this->demo_model->delete($id);
		//$this->load->view('edit_form',$data);
		 header("Location:../../");
		
	}

After that step we call the url as call base_url with controller name. So it displayed. Thus our first application has beed make.

Let's Think together, Say Something !