Tính năng quản lý sản phẩm CI

Tính năng quản lý sản phẩm CI

Làm sao có thể xây dựng 1 trang web mà không phải mất nhiều thời gian

Bã đã làm 1 trang web hoàn thiện trong vòng bao lâu. Thay vì thời gian code những code nhàm chán lặp đi lặp lại nhiều lần.

File Model.php

 

<?php

/**
*--Project: lavarel
--Design by: ATC PRO
-- Date time: 26/4/2022 12:30:12 AM
*/
class Products_model extends CI_Model
{
public function create_Products($data)
{
$this->db->insert('Products', $data);
$id=$this->db->insert_id();
if($id==0)
{
return base_url().'backend/Products';
}
return $this->db->get_where('Products', ['id' => $id])->row();
}

public function get_Products()
{
$this->db->select('Products'.'.*', FALSE);
$this->db->from('Products');
$query = $this->db->get();
return $query->result();

}
public function get_where_Products($id)
{
return $this->db->get_where('Products', ['id' => $id])->result();
}

public function get_Products_where( $id)
{
return $this->db->get_where('Products', ['id' => $id])->row();
}

public function update_Products( $data, $id)
{
return $this->db->where(['id' => $id])->update('Products', $data);
}

public function delete_Products( $id)
{

return $this->db->where(['id' => $id])->delete('Products');
}

}

 

 

File Controll.php

<?php
/**
 *--Project: lavarel
  --Design by: ATC PRO
  -- Date time: 26/4/2022 12:29:56 AM
 * This controller handles project management.
 */
class Products extends CI_Controller
{
    public $user_id;
    public $user_name;

    public function __construct(Type $foo = null)
    {

        parent::__construct();
        
        if (!$this->session->userdata('logged_in')) {
            $this->session->set_flashdata('flash_danger', 'Vui lòng đăng nhập để xem trang');
            header('location: ' .base_url().'backend/login');
        }
        else
        {
         $user_id = $this->session->userdata('user_id');
         $user_name = $this->session->userdata('user_name');
        
        }
        $this->load->model('products_model');
       
    }

    public function index()
    {
       
        $this->load->view('back_end_template/content', [
            'template' => 'backend/products/index','sl'=>1,
            'data' => $this->products_model->get_products()
             
        ]);
    }

    public function create()
    {
 $data = $this->products_model->get_where_products('0');
            $this->load->view('back_end_template/content', ['template' => 'backend/products/create','data' => $data  ]);
        
      }
    public function add()
    {
       
       
        
            $data = $this->products_model->create_products([
            'id' => $this->input->post('id'),
            'name' => $this->input->post('name'),
            'description' => $this->input->post('description'),
            'price' => $this->input->post('price'),
            'created_at' => $this->input->post('created_at'),
            'updated_at' => $this->input->post('updated_at'),
            'deleted_at' => $this->input->post('deleted_at'),

               
            ]);

            if ($data) {
                $this->session->set_flashdata('flash_success', 'Your products has been created');
                return header('location: ' .base_url().'backend/products/show/'.$data->id);
            }
        
    }

     public function search(){
        $data['template'] = 'backend/products/index';
        $keyword = $this->input->post('keyword');   
        $column = $this->input->post('column');   
        $list_col = array('id' => $keyword,'name' => $keyword,'description' => $keyword,'price' => $keyword,'created_at' => $keyword,'updated_at' => $keyword,'deleted_at' => $keyword,); 
        if($column!="")
        {
          $columns="products.$column";
          $list_col =array($columns => $keyword);
         }
        $config['$keyword'] = $keyword;
        $config['$columnsearch'] = $column;  
        $config['base_url'] = site_url('backend/products/search/');
        $config['total_rows'] = $this->get_model->atc_get_count_search('products',$list_col);
        $data['sl'] = $config['total_rows'];
        $config['per_page'] = 10;  
        $config['uri_segment'] = 3;
        $config['cur_tag_open'] = '<a class="current_page" href="backend/products/">';
        $config['cur_tag_close'] = '</a>';
        $this->pagination->initialize($config);
        
        $data['data'] = $this->get_model->atc_get_search('products',$list_col,$config['per_page'], $this->uri->segment($config['uri_segment'])); 
        $data['link'] = $this->pagination->create_links();
        
    
        
        $this->load->view('back_end_template/content', $data);
        
    }
    public function show($id)
    {
        $data = $this->products_model->get_where_products($id);
        if (!$data) {
            $this->denied_message();

           return header('location: ' .base_url().'backend/products');
        }
        $this->load->view('back_end_template/content', [
            'template' => 'backend/products/show',
            'data' => $data 
         
        ]);
    }

    public function edit($id)
    {
        $data = $this->products_model->get_where_products($id);
        if (!$data) {
            $this->denied_message();
         	return	header('location: ' .base_url().'backend/products');
           
        }
     
     
        
            $this->load->view('back_end_template/content', ['template' => 'backend/products/edit', 'data' => $data ]);
       
    }

    public function update()
    {
        
            $data = [
            'id' => $this->input->post('id'),
            'name' => $this->input->post('name'),
            'description' => $this->input->post('description'),
            'price' => $this->input->post('price'),
            'created_at' => $this->input->post('created_at'),
            'updated_at' => $this->input->post('updated_at'),
            'deleted_at' => $this->input->post('deleted_at'),

              
            ];
         	$ID=$this->input->post('id');
            $url=$this->input->post('urlre');
            if ($this->products_model->update_products( $data,$ID)) {
                $this->session->set_flashdata('flash_success', 'Your products has been updated');
                if($url=="save")
               {
                   return header('location: ' .base_url().'backend/products/');
               }
                return header('location: ' .base_url().'backend/products/show/'.$ID);
                }
           
        
    }
     public function deleteall()
    {
    
         $con = $this->input->post('id_check');
         $num_array = count($con);

            if($num_array>0){
                     for($i=1;$i<=$num_array;$i++)
                     {
                     $id = $con[$i-1];
                     $this->products_model->delete_products($id);
                     }
                      $this->session->set_flashdata('flash_success', 'Tất cả đã xóa thành công !');
              }
               return  header('location: ' .base_url().'backend/products');
    
    }
    public function delete($id)
    {
       if(isset($_POST['id']))
       {
          $id=$_POST['id'];
        }
        $data= $this->products_model->get_where_products($id);
        if (!$data) {
            $this->denied_message();

          return  header('location: ' .base_url().'backend/products');
        }
        if ($this->products_model->delete_products($id)) {
            $this->session->set_flashdata('flash_success', 'Your products has been deleted');
        } else {
            $this->denied_message();
        }
         return   header('location: ' .base_url().'backend/products');

    }

    private function denied_message()
    {
        $this->session->set_flashdata('flash_danger', 'That products does not exist or you do not have permission');
    }
}

Filde view .php

 
 <!-- Content Header  -->
<section class="content-header">
      <h1>
       <a href="/admin/products/index">
         <button type="button" class="btn btn-flat btn-success"><i class="fa fa-mail-reply-all"></i>&nbsp;Trở về</button>
       </a>
 products Index
        
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="#">products</a></li>
        <li class="active"> products Index</li>
      </ol>
</section>
 <!-- Main content -->
 <section class="content">
    
  <div class="row">
  @if(ViewBag.alert!=null) {
    <div class="alert [email protected] alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa [email protected]"></i> Thông báo!</h4>
  @ViewBag.alert
</div>
}
    <!--  column -->
     <div class="box">
        <div class="box-body">
            <div class="panel panel-primary">
                <div class="panel-heading">
                  <h3 class="panel-title"><i class="fa fa-plus"></i> products Index</h3>
                </div>
              <div class="box-body">
<form action='<?=base_url()?>backend/products/Add' method='post' id='form_products'>
<div class="box-body">
<input   id='urlre' value='' name='urlre'  type='hidden'  />    <div class="form-group">
    <label for="Input_id">id</label>
  <input  placeholder="id" class="form-control" id="id" name="id"   required='required' value='<?php if(isset($data[0]->id)) echo $data[0]->id;?>' type="text" >
  </div>
    <div class="form-group">
    <label for="Input_name">name</label>
  <input  placeholder="name" class="form-control" id="name" name="name"   required='required'   value='<?php if(isset($data[0]->name)) echo $data[0]->name;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_description">description</label>
  <textarea  class="form-control" id="description" name="description" placeholder=" required='required' " ><?php if(isset($data[0]->description)) echo $data[0]->description;?></textarea>
<script type='text/javascript'>
            	    CKEDITOR.replace( 'description',{
            	    height : '250'
            		
            	});		
            </script>  </div>
    <div class="form-group">
    <label for="Input_price">price</label>
  <input  placeholder="price" class="form-control" id="price" name="price"   required='required'   value='<?php if(isset($data[0]->price)) echo $data[0]->price;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_created_at">created_at</label>
  <input  placeholder="created_at" class="form-control" id="created_at" name="created_at"   required='required'   value='<?php if(isset($data[0]->created_at)) echo $data[0]->created_at;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_updated_at">updated_at</label>
  <input  placeholder="updated_at" class="form-control" id="updated_at" name="updated_at"   required='required'   value='<?php if(isset($data[0]->updated_at)) echo $data[0]->updated_at;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_deleted_at">deleted_at</label>
  <input  placeholder="deleted_at" class="form-control" id="deleted_at" name="deleted_at"   required='required'   value='<?php if(isset($data[0]->deleted_at)) echo $data[0]->deleted_at;?>'  type="text" >
  </div>

<div class="box-footer"> <button type="submit" class="btn btn-primary">Save changes</button></div>
</form>
              </div>
            </div>
        </div>
      </div>
 </section>

 

nguyen tran

Leave a Reply

Your email address will not be published. Required fields are marked *