Codeignier 3 blog application: update() post method needs improvement - Ajax30 - 05-02-2018
I am working on a basic blog application in Codeigniter 3.1.8.
There is a create post and an update post functionality.
When a post is created, there is the option to upload a post thumbnail, else a default image is displayed.
There is also a method for updating posts:
Code: public function update() {
// Form data validation rules
// irrelevant for the question suppressed
$id = $this->input->post('id');
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = $this->input->post('postimage');
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
if ($this->form_validation->run()) {
$this->Posts_model->update_post($id, $post_image);
redirect('posts/post/' . $id);
} else {
$this->edit($id);
}
}
The code above means: if no new image is uploaded, write the name of the existing one in the posts table again, instead of writing nothing. If instead a new photo is uploaded, write the name of the new photo.
In the Posts_model model:
Code: public function update_post($id, $post_image) {
$data = [
'title' => $this->input->post('title'),
'description' => $this->input->post('desc'),
'content' => $this->input->post('body'),
'post_image' => $post_image,
'cat_id' => $this->input->post('category')
];
$this->db->where('id', $id);
return $this->db->update('posts', $data);
}
The edit post view:
Code: <?php echo form_open_multipart(base_url('posts/update')); ?>
<input type="hidden" name="id" id="pid" value="<?php echo $post->id; ?>">
<div class="form-group <?php if(form_error('title')) echo 'has-error';?>">
<input type="text" name="title" id="title" class="form-control" placeholder="Title" value="<?php echo $post->title; ?>">
<?php if(form_error('title')) echo form_error('title'); ?>
</div>
<div class="form-group <?php if(form_error('desc')) echo 'has-error';?>">
<input type="text" name="desc" id="desc" class="form-control" placeholder="Short decription" value="<?php echo $post->description; ?>">
<?php if(form_error('desc')) echo form_error('desc'); ?>
</div>
<div class="form-group <?php if(form_error('body')) echo 'has-error';?>">
<textarea name="body" id="body" cols="30" rows="5" class="form-control" placeholder="Add post body"><?php echo $post->content; ?></textarea>
<?php if(form_error('body')) echo form_error('body'); ?>
</div>
<div class="form-group">
<select name="category" id="category" class="form-control">
<?php foreach ($categories as $category): ?>
<?php if ($category->id == $post->cat_id): ?>
<option value="<?php echo $category->id; ?>" selected><?php echo $category->name; ?></option>
<?php else: ?>
<option value="<?php echo $category->id; ?>"><?php echo $category->name; ?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</div>
<label for="postimage">Upload an image</label>
<div class="form-group">
<input type="file" name="userfile" id="postimage" size="20">
</div>
<input type="hidden" name="postimage" id="postimage" value="<?php echo $post->post_image; ?>">
<div class="form-group">
<input type="submit" value="Save" class="btn btn-block btn-md btn-success">
</div>
<?php echo form_close(); ?>
The problem with the upload part of the update() method seems... redundant. The image name is written if there is no image uploaded.
How can I improve the function?
RE: Codeignier 3 blog application: update() post method needs improvement - InsiteFX - 05-02-2018
Initialize the $post_image with the default image.
If the user uploads a new image then assign that new image to $post_image.
This way you are getting rid of an extra image check.
RE: Codeignier 3 blog application: update() post method needs improvement - Ajax30 - 05-02-2018
I don't want the default.jpg image to be uploaded if nothing else is loaded at update post. This already happens at create post. (Every post has either a relevant image or the default image. No post is without image.)
Code: public function create() {
$this -> form_validation -> set_rules('title', 'Title', 'required');
$this -> form_validation -> set_rules('desc', 'Short description', 'required');
$this -> form_validation -> set_rules('body', 'Body', 'required');
$this -> form_validation -> set_error_delimiters('<p class="error">', '</p>');
if ($this -> form_validation -> run() === FALSE) {
$this -> load -> view('partials/header', $data);
$this -> load -> view('create');
$this -> load -> view('partials/footer');
} else {
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '2048';
$this -> load -> library('upload', $config);
if (!$this -> upload -> do_upload()) {
$errors = array('error' => $this -> upload -> display_errors());
$post_image = 'default.jpg';
} else {
$data = array('upload_data' => $this -> upload -> data());
$post_image = $_FILES['userfile']['name'];
}
$this -> Posts_model -> create_post($post_image);
redirect('posts');
}
}
I want nothing to change unless a new image is uploaded. How would you do that?
|