我正在准备一个包含 Mission、Vision 和 Why_Us 部分的 CMS。我希望在编辑时我应该分别上传三个图像,并为每个部分提供不同的文本字段。整个条目和图像应反映在 SQL 数据库中。示例代码如下,请协助。
查看代码
<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>
控制器代码
// 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>";
}
}
}
型号代码
public function insert($data)
{
$this->db->insert('about_us',$data);
return TRUE;
}