Build Deep high-quality website with Core Pro 5.1.8 and Theme 1.0.6
Viết chức năng phần quản lý thư viện ảnh dùng codeinighter
Viết chức năng phần quản lý thư viện ảnh bằng codeinighter
Phần xử lý controller
<?php
/**
*--Project: funiture
--Design by: ATC PRO
-- Date time: 3/6/2022 12:21:03 AM
* This controller handles project management.
*/
class Photos 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('Photos_model');
}
public function index()
{
$this->load->view('back_end_template/content', [
'template' => 'backend/Photos/index','sl'=>1,
'data' => $this->Photos_model->get_Photos()
]);
}
public function create()
{
$data = $this->Photos_model->get_where_Photos('0');
$this->load->view('back_end_template/content', ['template' => 'backend/Photos/create','data' => $data ]);
}
public function add()
{
$data = $this->Photos_model->create_Photos([
'ID' => $this->input->post('ID'),
'Title' => $this->input->post('Title'),
'ImgUrl' => $this->input->post('ImgUrl'),
'Sort' => $this->input->post('Sort'),
'Parent' => $this->input->post('Parent'),
'Group' => $this->input->post('Group'),
'Status' => $this->input->post('Status'),
'Link' => $this->input->post('Link'),
'Note' => $this->input->post('Note'),
]);
if ($data) {
$this->session->set_flashdata('flash_success', 'Your Photos has been created');
return header('location: ' .base_url().'backend/Photos/show/'.$data->ID);
}
}
public function search(){
$data['template'] = 'backend/photos/index';
$keyword = $this->input->post('keyword');
$column = $this->input->post('column');
$list_col = array('ID' => $keyword,'Title' => $keyword,'ImgUrl' => $keyword,'Sort' => $keyword,'Parent' => $keyword,'Group' => $keyword,'Status' => $keyword,'Link' => $keyword,'Note' => $keyword,);
if($column!="")
{
$columns="Photos.$column";
$list_col =array($columns => $keyword);
}
$config['$keyword'] = $keyword;
$config['$columnsearch'] = $column;
$config['base_url'] = site_url('backend/photos/search/');
$config['total_rows'] = $this->get_model->atc_get_count_search('Photos',$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/photos/">';
$config['cur_tag_close'] = '</a>';
$this->pagination->initialize($config);
$data['data'] = $this->get_model->atc_get_search('Photos',$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->Photos_model->get_where_Photos($ID);
if (!$data) {
$this->denied_message();
return header('location: ' .base_url().'backend/photos');
}
$this->load->view('back_end_template/content', [
'template' => 'backend/photos/show',
'data' => $data
]);
}
public function edit($ID)
{
$data = $this->Photos_model->get_where_Photos($ID);
if (!$data) {
$this->denied_message();
return header('location: ' .base_url().'backend/photos');
}
$this->load->view('back_end_template/content', ['template' => 'backend/photos/edit', 'data' => $data ]);
}
public function update()
{
$data = [
'ID' => $this->input->post('ID'),
'Title' => $this->input->post('Title'),
'ImgUrl' => $this->input->post('ImgUrl'),
'Sort' => $this->input->post('Sort'),
'Parent' => $this->input->post('Parent'),
'Group' => $this->input->post('Group'),
'Status' => $this->input->post('Status'),
'Link' => $this->input->post('Link'),
'Note' => $this->input->post('Note'),
];
$ID=$this->input->post('ID');
$url=$this->input->post('urlre');
if ($this->Photos_model->update_Photos( $data,$ID)) {
$this->session->set_flashdata('flash_success', 'Your Photos has been updated');
if($url=="save")
{
return header('location: ' .base_url().'backend/photos/');
}
return header('location: ' .base_url().'backend/photos/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->Photos_model->delete_Photos($id);
}
$this->session->set_flashdata('flash_success', 'Tất cả đã xóa thành công !');
}
return header('location: ' .base_url().'backend/photos');
}
public function delete($ID)
{
if(isset($_POST['id']))
{
$ID=$_POST['id'];
}
$data= $this->Photos_model->get_where_Photos($ID);
if (!$data) {
$this->denied_message();
return header('location: ' .base_url().'backend/photos');
}
if ($this->Photos_model->delete_Photos($ID)) {
$this->session->set_flashdata('flash_success', 'Your Photos has been deleted');
} else {
$this->denied_message();
}
return header('location: ' .base_url().'backend/photos');
}
private function denied_message()
{
$this->session->set_flashdata('flash_danger', 'That Photos does not exist or you do not have permission');
}
}
Phần model thao tác dữ liệu
<?php
/**
*--Project: funiture
--Design by: ATC PRO
-- Date time: 3/6/2022 12:25:53 AM
*/
class Photos_model extends CI_Model
{
public function create_Photos($data)
{
$this->db->insert('Photos', $data);
$id=$this->db->insert_id();
if($id==0)
{
return base_url().'backend/Photos';
}
return $this->db->get_where('Photos', ['ID' => $id])->row();
}
public function get_Photos()
{
$this->db->select('Photos'.'.*', FALSE);
$this->db->from('Photos');
$query = $this->db->get();
return $query->result();
}
public function get_where_Photos($ID)
{
return $this->db->get_where('Photos', ['ID' => $ID])->result();
}
public function get_Photos_where( $ID)
{
return $this->db->get_where('Photos', ['ID' => $ID])->row();
}
public function update_Photos( $data, $ID)
{
return $this->db->where(['ID' => $ID])->update('Photos', $data);
}
public function delete_Photos( $ID)
{
return $this->db->where(['ID' => $ID])->delete('Photos');
}
}
Phần thêm view
<!-- Content Header -->
<section class="content-header">
<h1>
<a href="/admin/Photos/index">
<button type="button" class="btn btn-flat btn-success"><i class="fa fa-mail-reply-all"></i> Trở về</button>
</a>
Photos Index
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li><a href="#">Photos</a></li>
<li class="active"> Photos 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> Photos Index</h3>
</div>
<div class="box-body">
<form action='<?=base_url()?>backend/photos/Add' method='post' id='form_photos'>
<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_Title">Title</label>
<textarea class="form-control" id="Title" required='required' name="Title" cols="20" rows="2"><?php if(isset($data[0]->Title)) echo $data[0]->Title;?></textarea>
</div>
<div class="form-group">
<label for="Input_ImgUrl">ImgUrl</label>
<input id="ImgUrl" name="ImgUrl" value="<?php if(isset($data[0]->ImgUrl)) echo $data[0]->ImgUrl;?>" placeholder="ImgUrl" type="text" required="required" ><input onclick="BrowseServer('/','ImgUrl')" style="width: 70px;padding: 3px;" type="button" value="Chọn hình" readonly="readonly">
</div>
<div class="form-group">
<label for="Input_Sort">Sort</label>
<input placeholder="Sort" class="form-control" id="Sort" name="Sort" required='required' value='<?php if(isset($data[0]->Sort)) echo $data[0]->Sort;?>' type="text" >
</div>
<div class="form-group">
<label for="Input_Parent">Parent</label>
<textarea class="form-control" id="Parent" required='required' name="Parent" cols="20" rows="2"><?php if(isset($data[0]->Parent)) echo $data[0]->Parent;?></textarea>
</div>
<div class="form-group">
<label for="Input_Group">Group</label>
<textarea class="form-control" id="Group" required='required' name="Group" cols="20" rows="2"><?php if(isset($data[0]->Group)) echo $data[0]->Group;?></textarea>
</div>
<div class="form-group">
<label for="Input_Status">Status</label>
<textarea class="form-control" id="Status" required='required' name="Status" cols="20" rows="2"><?php if(isset($data[0]->Status)) echo $data[0]->Status;?></textarea>
</div>
<div class="form-group">
<label for="Input_Link">Link</label>
<textarea class="form-control" id="Link" required='required' name="Link" cols="20" rows="2"><?php if(isset($data[0]->Link)) echo $data[0]->Link;?></textarea>
</div>
<div class="form-group">
<label for="Input_Note">Note</label>
<textarea class="form-control" id="Note" required='required' name="Note" cols="20" rows="2"><?php if(isset($data[0]->Note)) echo $data[0]->Note;?></textarea>
</div>
<div class="box-footer"> <button type="submit" class="btn btn-primary">Save changes</button></div>
</form>
</div>
</div>
</div>
</div>
</section>
0 Nhận xét
Hãy trở thành người đầu tiên viết chia sẽ cảm nghĩ của mình bên dưới nhé!
Thêm Bình luận