Codeigniter : Insert product and upload image
Controllers prd.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class prd extends CI_Controller { public function __construct() { parent::__construct(); $this->load->library('pagination'); //$this->load->model('Imgtype_model'); $this->load->model('prd_model'); } public function index() { $config = array(); $config['base_url'] = base_url('imgtype/index'); $config['total_rows'] = $this->prd_model->record_count($this->input->get('keyword')); $config['per_page'] = $this->input->get('keyword') == NULL ? 14 : 999; $config['uri_segment'] = 3; $choice = $config['total_rows'] / $config['per_page']; $config['num_links'] = round($choice); $this->pagination->initialize($config); $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; $data['results'] = $this->prd_model->fetch_prd($config['per_page'], $page, $this->input->get('keyword')); $data['link'] = $this->pagination->create_links(); $data['total_rows'] = $config['total_rows']; $this->load->view('template/backheader'); $this->load->view('prd/mainpage', $data); $this->load->view('template/backfooter'); } //show select list imgtype for from add public function newdata() { $data['results'] = $this->Imgtype_model->fetch_imgtype(0,0,''); $this->load->view('template/backheader'); $this->load->view('prd/newdata',$data); $this->load->view('template/backfooter'); } public function adding($value='') { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '2000'; $config['max_width'] = '2000'; $config['max_height'] = '2000'; $this->load->library('upload', $config); if ( ! $this->upload->do_upload('img')) { //$error = array('error' => $this->upload->display_errors()); echo $this->upload->display_errors(); //$this->load->view('upload_form', $error); } else { $data = $this->upload->data(); //print_r($data); //$this->load->view('upload_success', $data); $filename = $data['file_name']; //$imgtype_name = $data['imgtype_name']; $arr=array( 'img_name'=> $this->input->post('img_name'), 'img_detail'=> $this->input->post('img_detail'), 'img_price'=> $this->input->post('img_price'), 'imgtype_id'=> $this->input->post('imgtype_id'), "img"=>$filename ); $this->db->insert('img',$arr); $this->session->set_flashdata( array( 'msginfo'=>'<div class="pad margin no-print"><div style="margin-bottom: 0!important;" class="callout callout-info"><h4><i class="fa fa-info"></i> ข้อความจากระบบ</h4>ทำรายการสำเร็จ</div></div>' ) ); redirect('prd', 'refresh'); } } public function update($value='') { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '2000'; $config['max_width'] = '2000'; $config['max_height'] = '2000'; $this->load->library('upload', $config); //print_r($_POST); //exit(); if ( ! $this->upload->do_upload('img')) { //$error = array('error' => $this->upload->display_errors()); //echo $this->upload->display_errors(); //$this->load->view('upload_form', $error); $arr1=array( 'img_name'=> $this->input->post('img_name'), 'img_detail'=> $this->input->post('img_detail'), 'img_price'=> $this->input->post('img_price'), 'imgtype_id'=> $this->input->post('imgtype_id'), //'imgtype_id'=> $this->input->post('imgtype_id'), "img"=>$this->input->post('img2') ); $this->db->where('img_id', $this->input->post('img_id')); $this->db->update('img', $arr1); $this->session->set_flashdata( array( 'msginfo'=>'<div class="pad margin no-print"><div style="margin-bottom: 0!important;" class="callout callout-success"><h4><i class="fa fa-info"></i> ข้อความจากระบบ</h4>ทำรายการสำเร็จ [ไม่เปลี่ยนภาพ]</div></div>' ) ); redirect('prd', 'refresh'); } else { $data = $this->upload->data(); // print_r($data); //$this->load->view('upload_success', $data); $filename = $data['file_name']; //$imgtype_name = $data['imgtype_name']; /* if($filename!=''){ echo "has"; }else{ echo "no"; } */ //exit(); $arr=array( 'img_name'=> $this->input->post('img_name'), 'img_detail'=> $this->input->post('img_detail'), 'img_price'=> $this->input->post('img_price'), 'imgtype_id'=> $this->input->post('imgtype_id'), //'imgtype_id'=> $this->input->post('imgtype_id'), "img"=>$filename ); $this->db->where('img_id', $this->input->post('img_id')); $this->db->update('img', $arr); $this->session->set_flashdata( array( 'msginfo'=>'<div class="pad margin no-print"><div style="margin-bottom: 0!important;" class="callout callout-info"><h4><i class="fa fa-info"></i> ข้อความจากระบบ</h4>ทำรายการสำเร็จ</div></div>' ) ); redirect('prd', 'refresh'); } } public function edit($img_id) { $data['results'] = $this->Imgtype_model->fetch_imgtype(0,0,''); $data['doc'] = $this->prd_model->read_prd($img_id); $this->load->view('template/backheader'); $this->load->view('prd/edit',$data); $this->load->view('template/backfooter'); } public function confrm($img_id) { $data = array ( 'backlink' => 'prd', 'deletelink'=> 'prd/remove/' . $img_id ); $this->load->view('template/backheader'); $this->load->view('confrm',$data); $this->load->view('template/backfooter'); } public function remove($img_id) { $this->prd_model->remove_prd($img_id); redirect('prd','refresh'); } } |
models prd_model.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
<?php class Prd_model extends CI_Model { public $img_detail; //public $description; public function __construct() { parent::__construct(); } public function record_count($keyword) { $this->db->like('img_detail',$keyword); $this->db->from('img'); return $this->db->count_all_results(); } public function fetch_prd($limit, $start, $keryword) { $this->db->select('img.*,imgtype.imgtype_name'); $this->db->from('img'); $this->db->join('imgtype','imgtype.imgtype_id=img.imgtype_id'); $this->db->like('img_detail', $keryword); $this->db->limit($limit, $start); $query = $this->db->get(); if($query->num_rows() > 0) { foreach($query->result() as $row) { $data[] = $row; } return $data; } return FALSE; } public function fetch_prd2($limit, $start) { $this->db->select('img.*,imgtype.imgtype_name'); $this->db->from('img'); $this->db->join('imgtype','imgtype.imgtype_id=img.imgtype_id'); $this->db->like('img_detail'); $this->db->limit($limit, $start); $query = $this->db->get(); if($query->num_rows() > 0) { foreach($query->result() as $row) { $data[] = $row; } return $data; } return FALSE; } public function entry_prd($img_id) { $this->img_detail = $this->input->post('img_detail'); //$this->description = $this->input->post('description'); if($img_id == NULL) { $this->db->insert('img', $this); } else { $this->db->update('img', $this, array('img_id'=> $img_id)); } } public function read_prd($img_id) { $this->db->select('img.*,imgtype.imgtype_name'); $this->db->from('img'); $this->db->join('imgtype','imgtype.imgtype_id=img.imgtype_id'); $this->db->where('img.img_id', $img_id); $query = $this->db->get(); if($query->num_rows() > 0) { $data = $query->row(); return $data; } return FALSE; } public function remove_prd($img_id){ $this->db->delete('img',array('img_id'=>$img_id)); } } |
views (form insert) newdata.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
<div class="content-wrapper"> <!-- Content Header (Page header) --> <section class="content-header"> <h1> จัดการสินค้า </h1> <ol class="breadcrumb"> <li> <a href="<?php echo site_url('prd'); ?>"> <i class="fa fa-dashboard"> </i>หน้าแรก </a> </li> <li> <a href="<?php echo site_url('prd'); ?>"> จัดการสินค้า </a> </li> <li class="active"> เพิ่มข้อมูลใหม่ </li> </ol> </section> <!-- Main content --> <section class="content"> <div class="container"> <div class="row"> <div class="col-sm-7"> <!-- Your Page Content Here --> <div class="box box-primary"> <div class="box-header with-border"> <h3 class="box-title"> เพิ่มข้อมูล </h3> </div><!-- /.box-header --> <!-- form start --> <form role="form" action="<?php echo base_url('prd/adding'); ?>" method="post" enctype="multipart/form-data"> <div class="box-body"> <div class="form-group"> <label for="exampleInputEmail1"> ประเภท </label> <?php echo $this->session->flashdata('err_imgtype_id'); ?> <select class="form-control" name="imgtype_id"> <option value=""> เลือกข้อมูล </option> <?php foreach($results as $result){ ?> <option value="<?php echo $result->imgtype_id; ?>"> <?php echo $result->imgtype_name; ?> </option> <?php } ?> </select> </div> <div class="form-group"> <label for="exampleInputEmail1"> ชื่อภาพ </label> <?php echo $this->session->flashdata('err_img_name'); ?> <input type="text" id="img_name" class="form-control" name="img_name" value="<?php echo $this->session->flashdata('img_name'); ?>" required> </div> <div class="form-group"> <label> รายละเอียด </label> <textarea rows="3" class="form-control" id="img_detail" name="img_detail" required><?php echo $this->session->flashdata('img_detail'); ?></textarea> </div> <div class="form-group"> <label for="exampleInputEmail1"> ราคา </label> <?php echo $this->session->flashdata('err_img_price'); ?> <input type="number" id="topic" class="form-control" name="img_price" value="<?php echo $this->session->flashdata('img_price'); ?>" required> </div> <div class="form-group"> <label for="exampleInputEmail1"> อัพโหลดไฟล์ภาพ </label> <?php echo $this->session->flashdata('err_img'); ?> <input type="file" name="img" id="img" > </div> </div><!-- /.box-body --> <div class="box-footer"> <button class="btn btn-primary" type="submit"> <i class="fa fa-fw fa-save"> </i>บันทึกข้อมูล </button> <a class="btn btn-danger" href="<?php echo site_url('prd'); ?>" role="button"> <i class="fa fa-fw fa-close"> </i>ยกเลิก </a> </div> </form> </div> </div> </div> </div> </section><!-- /.content --> </div><!-- /.content-wrapper --> |
views (List) mainpage.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
<div class="content-wrapper"> <!-- Content Header (Page header) --> <section class="content-header"> <h1> จัดการสินค้า </h1> <ol class="breadcrumb"> <li><a href="<?php echo base_url('prd'); ?>"><i class="fa fa-dashboard"></i> หน้าแรก</a></li> <li class="active">จัดการสินค้า</li> </ol> </section> <!-- Top menu --> <?php echo $this->session->flashdata('msginfo'); ?> <!-- Main content --> <section class="content"> <!-- Your Page Content Here --> <div class="box"> <div class="box-header"> <h3 class="box-title">ตารางข้อมูล</h3> </div><!-- /.box-header --> <div class="box-body"> <div id="example1_wrapper" class="dataTables_wrapper form-inline dt-bootstrap"> <div class="row"> <div class="col-sm-6"> <a class="btn btn-success" href="<?php echo base_url('prd/newdata'); ?>" role="button"><i class="fa fa-fw fa-plus-circle"></i> เพิ่มข้อมูล</a> <a class="btn btn-default" href="<?php echo base_url('prd'); ?>" role="button"><i class="fa fa-fw fa-refresh"></i> Refresh Data</a> </div> <div class="col-sm-6"> <div id="" class="dataTables_filter"> <form action="" method="GET" name="search"> <label>ค้นหา</label>:<input type="search" name="keyword" class="form-control input-sm" placeholder="ค้นหา"></label> </form> </div> </div> </div> <div class="row"> <div class="col-sm-12"> <table id="example1" class="table table-bordered table-striped dataTable" role="grid" aria-describedby="example1_info"> <thead> <tr role="row"> <th class="sorting" tabindex="0" rowspan="1" colspan="1" style="width: 20%;">ชือภาพ</th> <th class="sorting" tabindex="0" rowspan="1" colspan="1">รายละเอียดภาพ </th> <th class="sorting" tabindex="0" rowspan="1" colspan="1">ภาพ</th> <th class="sorting" tabindex="0" rowspan="1" colspan="1" style="width: 60px;"> </th> </tr> </thead> <tbody> <?php if(!empty($results)){ foreach ($results as $data) { ?> <tr role="row"> <td> <a href="<?php echo base_url('prd/edit/'.$data->img_id); ?>"> <?php echo $data->img_name; ?> </a> <br> </td> <td> <?php echo $data->img_detail; ?> [ ราคา : <?php echo $data->img_price; ?>] <br> ประเภท : <?php echo $data->imgtype_name; ?> </td> <td> <a target="_blank" href="<?php echo base_url('uploads/'.$data->img); ?>"> <img src="uploads/<?php echo $data->img; ?>" width="100px"> </a> </td> <td> <a class="btn btn-danger btn-xs" href="<?php echo base_url('prd/confrm/'.$data->img_id); ?>" role="button"><i class="fa fa-fw fa-trash"></i> ลบข้อมูล</a> </td> </tr> <?php } } ?> </tbody> </table> </div> </div> <div class="row"> <div class="col-sm-5"> <div class="dataTables_info" id="example1_info" role="status" aria-live="polite">Total <?php echo $total_rows; ?> entries</div> </div> <div class="col-sm-7"> <div id="example1_paginate" class="dataTables_paginate paging_simple_numbers"> <?php echo $link; ?> </div> </div> </div> </div> </div><!-- /.box-body --> </div> </section><!-- /.content --> </div><!-- /.content-wrapper --> |
views (form edit) edit.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
<div class="content-wrapper"> <!-- Content Header (Page header) --> <section class="content-header"> <h1> จัดการสินค้า </h1> <ol class="breadcrumb"> <li> <a href="<?php echo base_url(); ?>"> <i class="fa fa-dashboard"> </i>หน้าแรก </a> </li> <li> <a href="<?php echo base_url('prd'); ?>"> จัดการสินค้า </a> </li> <li class="active"> เพิ่มข้อมูลใหม่ </li> </ol> </section> <!-- Main content --> <section class="content"> <!-- Your Page Content Here --> <div class="box box-primary"> <div class="box-header with-border"> <h3 class="box-title"> เพิ่มข้อมูล </h3> </div><!-- /.box-header --> <!-- form start --> <form role="form" action="<?php echo base_url('prd/update'); ?>" method="post" enctype="multipart/form-data"> <input type="hidden" value="<?php echo $doc->img_id; ?>" name="img_id"> <input type="hidden" value="<?php echo $doc->img; ?>" name="img2"> <div class="box-body"> <div class="form-group"> <label for="exampleInputEmail1"> ประเภท </label> <?php echo $this->session->flashdata('err_imgtype_id'); ?> <select class="form-control" name="imgtype_id"> <option value=""> เลือกข้อมูล </option> <?php foreach($results as $result){ ?> <option value="<?php echo $result->imgtype_id; ?>" <?php if($result->imgtype_id==$doc->imgtype_id){echo "selected"; } ?>> <?php echo $result->imgtype_name; ?> </option> <?php } ?> </select> </div> <div class="form-group"> <label for="exampleInputEmail1"> ชื่อภาพ </label> <?php echo $this->session->flashdata('err_img_name'); ?> <input type="text" id="img_name" class="form-control" name="img_name" value="<?php echo $doc->img_name; ?>"> </div> <div class="form-group"> <label> รายละเอียด </label> <textarea rows="3" class="form-control" id="img_detail" name="img_detail"><?php echo $doc->img_detail; ?></textarea> </div> <div class="form-group"> <label for="exampleInputEmail1"> ราคา </label> <?php echo $this->session->flashdata('err_img_price'); ?> <input type="number" id="topic" class="form-control" name="img_price" value="<?php echo $doc->img_price; ?>"> </div> <div class="form-group"> <label for="exampleInputEmail1"> อัพโหลดไฟล์ภาพ (<a href="<?php echo base_url('uploads/' . $doc->img); ?>" target="_blank"> ไฟล์เก่า </a>) </label> <?php echo $this->session->flashdata('err_img'); ?> <input type="file" name="img" id="img" > </div> </div><!-- /.box-body --> <div class="box-footer"> <button class="btn btn-primary" type="submit"> <i class="fa fa-fw fa-save"> </i>บันทึกข้อมูล </button> <a class="btn btn-danger" href="<?php echo base_url('document'); ?>" role="button"> <i class="fa fa-fw fa-close"> </i>ยกเลิก </a> </div> </form> </div> </section><!-- /.content --> </div><!-- /.content-wrapper --> |
../views confirm to delete data confrm.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<div class="content-wrapper"> <!-- Content Header (Page header) --> <section class="content-header"> </section> <!-- Main content --> <section class="content"> <!-- Your Page Content Here --> <div class="example-modal"> <div class="modal" style="display: block;"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h4 class="modal-title"> ข้อความแจ้งเตือน </h4> </div> <div class="modal-body"> <p> คุณต้องการลบข้อมูลนี้หรือไม่ </p> </div> <div class="modal-footer"> <a class="btn btn-default pull-left" href="<?= base_url($backlink) ?>" role="button"> ไม่ใช่ </a> <a class="btn btn-primary" href="<?= base_url($deletelink) ?>" role="button"> ใช่ ยืนยันการลบ </a> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal --> </div> </section><!-- /.content --> </div><!-- /.content-wrapper --> |