I am preparing a CMS which has Mission, Vision, and Why_Us section. I want that while editing I should be uploading three images separately with different text fields provided against each section. The entire entry and the images should reflect in the SQL Database. The sample codes are mentioned below, please assist.
View code
<form method="post" action="<?php echo site_url('about_us/insertdata');?>" enctype="multipart/form-data">
<!-- ============================================================= -->
<!-- Section for traavnow vision starts here -->
<!-- Section for our vision text data starts here -->
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-form-label">Our Vision:</label>
<div class="col-sm-10">
<textarea class="form-control" id="vision" name="our_vision" rows="3"></textarea>
</div>
</div>
<!-- Section for our vision text data ends here -->
<!-- Section for Uploading Image File to Our Vision Section Starts here -->
<div class="form-group row">
<label for="staticEmail" class="col-sm-4 col-form-label" >Upload Image:</label>
<div class="col-sm-4">
<input type="file" class="form-control-file" id="vision_image" name="vision_image">
</div>
<div class="col-sm-4" style="margin-top: 24px;">
<img src="" class="img-fluid rounded table-bordered" alt="Vision Image" style="height: 110px; width: 110px;">
</div>
</div>
<!-- Section for Uploading Image File to Our Vision Section ends here -->
<!-- Section for traavnow vision ends here -->
<!-- ============================================================= -->
<!-- ============================================================= -->
<!-- Section for traavnow mission starts here -->
<!-- Section for our mission text data starts here -->
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-form-label">Our Mission:</label>
<div class="col-sm-10">
<textarea class="form-control" id="mission" name="our_mission" rows="3"></textarea>
</div>
</div>
<!-- Section for our mission text data ends here -->
<!-- Section for Uploading Image File to Our mission Section Starts here -->
<div class="form-group row">
<label for="staticEmail" class="col-sm-4 col-form-label">Upload Image:</label>
<div class="col-sm-4">
<input type="file" class="form-control-file" id="mission_image" name="mission_image">
</div>
<div class="col-sm-4" style="margin-top: 24px;">
<img src="" class="img-fluid rounded table-bordered" alt="Mission Image" style="height: 110px; width: 110px;">
</div>
</div>
<!-- Section for Uploading Image File to Our mission Section ends here -->
<!-- Section for traavnow mission ends here -->
<!-- ============================================================= -->
<!-- ============================================================= -->
<!-- Section for traavnow why_traavnow starts here -->
<!-- Section for our why_traavnow text data starts here -->
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-form-label">Why Traavnow:</label>
<div class="col-sm-10">
<textarea class="form-control" id="why_traavnow" name="why_traavnow" rows="3"></textarea>
</div>
</div>
<!-- Section for our why_traavnow text data ends here -->
<!-- Section for Uploading Image File to Our why_traavnow Section Starts here -->
<div class="form-group row">
<label for="staticEmail" class="col-sm-4 col-form-label">Upload Image:</label>
<div class="col-sm-4">
<input type="file" class="form-control-file" id="whyus_image" name="whyus_image">
</div>
<div class="col-sm-4" style="margin-top: 24px;">
<img src="" class="img-fluid rounded table-bordered" alt="Why Traavnow" style="height: 110px; width: 110px;">
</div>
</div>
<!-- Section for Uploading Image File to Our why_traavnow Section ends here -->
<!-- Section for traavnow why_traavnow ends here -->
<!-- ============================================================= -->
<!-- ============================================================= -->
<!-- Button responding to form submission starts here -->
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<button class="btn btn-primary form-control" name="update" type="submit">Update Details</button>
</div>
</div>
<!-- Button responding to form submission ends here -->
<!-- ============================================================= -->
<div style="height: 120px;"></div>
</form>
Controller Code
// Function to INSERT text and images against each text starts here
public function insertdata()
{
$vision = $this->input->post('our_vision');
$mission = $this->input->post('our_mission');
$why_traavnow = $this->input->post('why_traavnow');
// get foto
$config['upload_path'] = './assets/uploads/about_us/';
$config['allowed_types'] = 'jpg|png|jpeg|';
$config['max_size'] = '2048'; //2MB max
$config['max_width'] = '4480'; // pixel
$config['max_height'] = '4480'; // pixel
$config['file_name'] = $_FILES['vision_image']['name'];
$this->upload->initialize($config);
if (!empty($_FILES['vision_image']['name'] ))
{
if ( $this->upload->do_upload('vision_image') ) {
$photo = $this->upload->data();
$data = array(
'our_vision' => $vision,
'photo' => $photo['vision_image'],
'our_mission' => $mission,
'why_traavnow' => $why_traavnow,
);
if(isset($_FILES) && ){
$this->model("about_us_model/insert");
}
}else {
echo "<script>alert('Details Updated Successfully')</script>";
}
}
}
Model Code
public function insert($data)
{
$this->db->insert('about_us',$data);
return TRUE;
}