Postingan lainnya
Harusnya masuk ke if tapi malah masuk ke else di codeigniter
saya punya form type file
<input type="file" class="form-control" id="imagefile" name="image" value="okok">
lalu di controller saya cek
if($this->input->post('image') == "okok"){
//harusnya masuk kesini
}else{
//tapi pas dijalankan masuk kesini
}
saya pakai codeigniter. salahnya dimana yah kak ?
1 Jawaban:
script viewnya
<pre> <form role="form" action="<?php echo base_url(); ?>admin/article/edit/<?php echo $article['idar']; ?>" method="post" enctype="multipart/form-data"> <div class="box-body"> <?php echo $this->session->flashdata('error'); ?> <div class="form-group"> <label for="title">Title</label> <input type="text" class="form-control" id="title" placeholder="Title" name="title" value="<?php echo $article['artitle']; ?>"> </div> <small class="form-text text-danger pl-1"><?php echo form_error('title'); ?></small> <div class="form-group"> <label for="image">Image</label><br> <img src="<?php echo base_url(); ?>assets/img/article/<?php echo $article['imgar']; ?>" style="width:100px"> <small>Old picture</small> <br><br> <input type="file" class="form-control" id="imagefile" onchange='changeEventHandler(event);' name="image" value="no"> </div> <small class="form-text text-danger pl-1"><?php echo form_error('image'); ?></small> <div class="form-group"> <label for="category">Category</label> <select class="form-control" name="category"> <option value="<?php echo $article['category']; ?>"><?php echo $article['titlecat']; ?></option> <?php foreach($allCategory->result_array() as $data){ ?> <option value="<?php echo $data['id']; ?>"><?php echo $data['title']; ?></option> <?php } ?> </select> </div> <div class="form-group"> <label for="desc">Description</label> <textarea name="description"><?php echo set_value('description'); ?><?php echo form_error('description'); ?><?php echo $article['descar']; ?></textarea> </div> <small class="form-text text-danger pl-1"><?php echo form_error('category'); ?></small> <button type="submit" class="btn btn-primary">Edit Article</button> </div> </form> </pre>
script controller
<pre> public function editarticle($id) { $query = $this->db->get_where('article', ['id' => $id])->row_array(); if($query['posted_by'] != $this->session->userdata('id')){ redirect(base_url() . 'admin/myarticles'); } $this->form_validation->set_rules('title', 'Title', 'required|min_length[10]'); $this->form_validation->set_rules('description', 'Description', 'required|min_length[20]'); if($this->form_validation->run() == false){ $data['title'] = 'Edit Article - Admin Panel'; $data['css'] = ''; $data['dataUser'] = $this->User_model->getDataUser(); $data['allCategory'] = $this->Categories_model->getAllCat(); $data['article'] = $this->Article_model->getArticleById($id); $this->load->view('admin/edit_article', $data); }else{ if($this->input->post('image') == "okok"){ $data = array(); $upload = $this->Article_model->uploadgambar(); if($upload['result'] == "success"){ $this->Article_model->updateArticle($upload,$id); $this->session->set_flashdata('upload', "<script> swal({ text: 'Article updated successfully!', icon: 'success' }); </script>"); redirect(base_url() . 'admin/myarticles'); }else{ $this->session->set_flashdata('upload', "<script> swal({ text: 'Article not uploaded. Make sure the image you upload is smaller than 2mb and must be in jpg, jpeg, or png format.', icon: 'error' }); </script>"); redirect(base_url() . 'admin/article/edit/' . $id); } }else{ $upload = ""; $this->Article_model->updateArticle($upload,$id); $this->session->set_flashdata('upload', "<script> swal({ text: 'Article updated successfully!', icon: 'success' }); </script>"); redirect(base_url() . 'admin/myarticles'); } } } </pre>
modelnya
<pre> public function uploadgambar() { $config['upload_path'] = './assets/img/article/'; $config['allowed_types'] = 'jpg|png|jpeg'; $config['max_size'] = '2048'; $config['file_name'] = round(microtime(true)*1000);
$this-&gt;load-&gt;library('upload', $config);
if($this-&gt;upload-&gt;do_upload('image')){
$return = array('result' =&gt; 'success', 'file' =&gt; $this-&gt;upload-&gt;data(), 'error' =&gt; '');
return $return;
}else{
$return = array('result' =&gt; 'failed', 'file' =&gt; '', 'error' =&gt; $this-&gt;upload-&gt;display_errors());
return $return;
}
}
</pre>
<pre> public function updateArticle($upload,$id) { $title = htmlspecialchars($this->input->post('title')); if($this->input->post('image') == "okok"){ $this->db->set('image', $upload['file']['file_name']); } $this->db->set('title', $title); $this->db->set('description', $this->input->post('description')); $this->db->set('category', htmlspecialchars($this->input->post('category'))); $this->db->where('id', $id); $this->db->update('article'); } </pre>