blog-details

Học lập trinh web PHP CodeIgniter Framework sử dụng phần mêm ATCSMART gồm các bài sau.

0. Điều kiện cần cài đặt XAMPP (Appache + MySQL)  xem tại  Hưỡng dẫn cài Xampp

1. Tạo chức năng các bảng dữ liệu

CREATE DATABASE BlogATC;

USE BlogATC; 
CREATE TABLE IF NOT EXISTS `roile` (
`id` int unsigned NOT NULL  AUTO_INCREMENT , 
`name` varchar (100) NULL, 
`status` tinyint(1) NULL, 
`access` varchar(500) NULL 
, INDEX(id), PRIMARY KEY(id))
ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `menu` (
`id` int unsigned NOT NULL  AUTO_INCREMENT , 
`name` varchar (150) NULL, 
`parent` int NULL, 
`sort` int NULL, 
`link` varchar (255) NULL, 
`status` tinyint(1) NULL 
, INDEX(id), PRIMARY KEY(id))
ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `category` (
`id` int unsigned NOT NULL  AUTO_INCREMENT , 
`name` varchar (255) NULL, 
`sort` int NULL, 
`parrent` int NULL, 
`status` boolean NULL 
, INDEX(id), PRIMARY KEY(id))
ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `blog` (
`id` int unsigned NOT NULL  AUTO_INCREMENT , 
`name` varchar (255) NULL, 
`sort` int NULL, 
`images` varchar (255) NULL, 
`categoryid` int NULL, 
`description` varchar(500) NULL, 
`status` boolean NULL, 
`content` varchar(500) NULL, 
`datepost` date NULL 
, INDEX(id), PRIMARY KEY(id))
ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `user` (
`username` int NOT NULL, 
`name` varchar (50) NULL, 
`password` varchar (50) NULL, 
`roileid` int NULL 
, PRIMARY KEY(username))
ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `contact` (
`id` int unsigned NOT NULL  AUTO_INCREMENT , 
`name` varchar (255) NULL, 
`email` varchar (100) NULL, 
`phone` varchar (20) NULL, 
`message` varchar(500) NULL, 
`status` boolean NULL, 
`datesend` date NULL 
, INDEX(id), PRIMARY KEY(id))
ENGINE=InnoDB;

ALTER TABLE User
ADD FOREIGN KEY(roileid) REFERENCES Roile(id);
ALTER TABLE Blog
ADD FOREIGN KEY(categoryid) REFERENCES Category(id);

2. Tạo và backup database  lên MYSQL Hoặc MariaDB

3. Bulding code  và chọn mẫu và xuất ra thu mục.

 1. File  CI_Controller.php

<?php
/**
 *--Project: BlogATC
  --Design by: ATC PRO
  -- Date time: 8/15/2020 12:34:05 AM
 * This controller handles project management.
 */
class Contact 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('contact_model');
       
    }

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

	public function create()
    {
 $data = $this->contact_model->get_where_contact('0');
            $this->load->view('back_end_template/content', ['template' => 'backend/contact/create','data' => $data  ]);
        
      }
    public function add()
    {
       
       
		
            $data = $this->contact_model->create_contact([
            'id' => $this->input->post('id'),
            'name' => $this->input->post('name'),
            'email' => $this->input->post('email'),
            'phone' => $this->input->post('phone'),
            'message' => $this->input->post('message'),
            'status' => $this->input->post('status'),
            'datesend' => $this->input->post('datesend'),

               
            ]);

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

	 public function search(){
        $data['template'] = 'backend/contact/index';
        $keyword = $this->input->post('keyword');   
	    $column = $this->input->post('column');   
        $list_col = array('id' => $keyword,'name' => $keyword,'email' => $keyword,'phone' => $keyword,'message' => $keyword,'status' => $keyword,'datesend' => $keyword,); 
	    if($column!="")
		{
		  $columns="contact.$column";
		  $list_col =array($columns => $keyword);
         }
        $config['$keyword'] = $keyword;
	    $config['$columnsearch'] = $column;  
        $config['base_url'] = site_url('backend/contact/search/');
        $config['total_rows'] = $this->get_model->atc_get_count_search('contact',$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/contact/">';
        $config['cur_tag_close'] = '</a>';
        $this->pagination->initialize($config);
        
        $data['data'] = $this->get_model->atc_get_search('contact',$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->contact_model->get_where_contact($id);
        if (!$data) {
            $this->denied_message();

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

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

	public function update()
    {
        
            $data = [
            'id' => $this->input->post('id'),
            'name' => $this->input->post('name'),
            'email' => $this->input->post('email'),
            'phone' => $this->input->post('phone'),
            'message' => $this->input->post('message'),
            'status' => $this->input->post('status'),
            'datesend' => $this->input->post('datesend'),

              
            ];
		 	$ID=$this->input->post('id');
		    $url=$this->input->post('urlre');
            if ($this->contact_model->update_contact( $data,$ID)) {
                $this->session->set_flashdata('flash_success', 'Your contact has been updated');
                if($url=="save")
			   {
				   return header('location: ' .base_url().'backend/contact/');
			   }
				return header('location: ' .base_url().'backend/contact/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->contact_model->delete_contact($id);
					 }
					  $this->session->set_flashdata('flash_success', 'Tất cả đã xóa thành công !');
	          }
			   return  header('location: ' .base_url().'backend/contact');
	
	}
    public function delete($id)
    {
	   if(isset($_POST['id']))
	   {
		  $id=$_POST['id'];
	    }
        $data= $this->contact_model->get_where_contact($id);
        if (!$data) {
            $this->denied_message();

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

    }

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

2.File Model Blog.php

<?php

/**
 *--Project: BlogATC
  --Design by: ATC PRO
  -- Date time: 8/15/2020 12:34:09 AM
 */
class Contact_model extends CI_Model
{
    public function create_Contact($data)
    {
        $this->db->insert('Contact', $data);
		$id=$this->db->insert_id();
		if($id==0)
		{
		    return base_url().'backend/Contact';
		}
        return $this->db->get_where('Contact', ['id' => $id])->row();
    }

    public function get_Contact()
    {
	    $this->db->select('Contact'.'.*', FALSE);
        $this->db->from('Contact');
	    $query = $this->db->get();  
        return $query->result();
        
    }
	public function get_where_Contact($id)
    {
        return $this->db->get_where('Contact', ['id' => $id])->result();
    }

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

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

    public function delete_Contact( $id)
    {
       
        return $this->db->where(['id' => $id])->delete('Contact');
    }

    
}

3. File  View indexp.php

4. Xuất code và cấu hình thư mục cho đúng

5. Chạy demo và triển khai đăng nhập quản lý

6. Cập nhật chỉnh sửa theo ý 1 số mục cần thiết.

7. Hoàn thiện lần cuối.

8. Triển khai đưa sản phẩm lên hosting domain

 

----Nội dung đang được cập nhật----

Đánh giá bài viết