我是 codeigniter 的新手,所以我对它的了解非常少。我需要从编辑页面编辑帖子。除了我上传的图片,我可以编辑所有内容。对于图片,我要么必须重新上传我想要的所有图片,要么图像字段将变为空。这是我当前的编辑代码:
<body>
<div class="container">
<strong>Edit Product</strong>
<?php echo validation_errors(); ?>
<?php echo form_open_multipart('/products/edit/'.$products['product_id']); ?>
<?php $error = form_error("product_name", "<p class='text-danger'>", '</p>'); ?>
<div class="form-group <?php echo $error ? 'has-error' : '' ?>">
<label for="product_name">Product_Name</label>
<input type="text" name="product_name" value="<?php echo $products['product_name'] ?>" id="product_name" class="form-control">
</div>
<div class="form-group">
<label>Picture</label>
<input class="form-control" type="file" name="picture[]" multiple />
</div>
<div class="container">
<?php
// explode images into a variable
$images=explode(',',$show['images']);
?>
<?php foreach($images as $key=>$val){ ?>
<?php //echo ($val);?>
<div class="row">
<div class="col-sm-6">
<img src="<?php echo base_url('uploads/images/').$val;?>" />
<input class="btn btn-danger" type="submit" value="Remove" />
</div>
</div>
<?php } ?>
</div>
<input type="submit" value="Create Product" class="btn btn-primary">
<?php echo form_close(); ?>
</div>
</body>
这是我在 Controller 上的编辑功能:
public function edit($id)
{
#code
$data['show']=$this->product_model->get_product_by_id($id);
if(empty($data['show']))
{
show_404();
}
$this->load->helper('form');
$this->load->library('form_validation');
$data['products']=$this->product_model->get_product_by_id($id);
$this->form_validation->set_rules('product_name','Product_Name','required');
$this->form_validation->set_rules('product_price','Product_Price','required');
$this->form_validation->set_rules('produce_description','Produce_Description','required');
if($this->form_validation->run() === FALSE)
{
$this->load->view('products/edit',$data);
}
else {
$this->product_model->set_product($id);
redirect('/');
}
}
这是我的 set_product 函数,用于将所有内容保存在数据库中:
public function set_product($id=0){
$picture=array();
$count=count($_FILES['picture']['name']);
//Check whether user upload picture
if(!empty($_FILES['picture']['name'])){
foreach($_FILES as $value){
for($s=0; $s<=$count-1; $s++){
$_FILES['picture']['name']=$value['name'][$s];
$_FILES['picture']['type'] = $value['type'][$s];
$_FILES['picture']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['picture']['error'] = $value['error'][$s];
$_FILES['picture']['size'] = $value['size'][$s];
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture[] = $uploadData['file_name'];
}
}
}
}//end of first if
$data=array('product_name'=>$this->input->post('product_name'));
if ($id==0){
$this->db->insert('products',$data);
$last_id = $this->db->insert_id();
if(!empty($picture)){
foreach($picture as $p_index=>$p_value) {
$this->db->insert('images', array('product_id'=>$last_id,'images'=>$p_value));
}
}
}
else {
$this->db->where('id',$id);
$this->db->update('products',$data);
if(!empty($picture)){
foreach($picture as $p_index=>$p_value) {
$this->db->update('images', array('product_id'=>$last_id,'images'=>$p_value) ); // --> this one?
}
}
}
}
这就是我的编辑页面现在的样子。 现在我想在这里发生的是,当我单击删除按钮时,只应删除该特定图片,我应该能够上传其他图片和剩余的图片,即未删除的图片。 谁能帮帮我。非常感谢任何形式的帮助。提前致谢。
最佳答案
逻辑
首先,了解在这种情况下实际应该发生什么的逻辑/概念。
- 点击删除链接
现在您有两个选择。 i) 发送到 URL 并返回此页面 ii) 使用 Ajax
- 从图片表中删除图片
通过直接链接或 Ajax,您将发送图像的 ID,您可以通过在模型中创建一个函数轻松删除它。
- 从上传文件夹中删除图片
有一个内置的 PHP 函数 unlink(path) 用于删除文件。您可以在将要编写的从图像表中删除图像的相同函数中执行此操作。
如何编码
在显示图像的 View 中,您可以链接到类似 URL 的 URL
<a href="<?php echo base_url().'products/delImage/'.$images['id']?>" class="btn btn-danger">Remove</a>
但这仅意味着如果您在 product_images 表中有图像。如果您的产品表中的图像列中有 csv 格式的图像名称。您需要将 product_id 和图像名称发送到模型函数,然后使用 str_replace()函数从列中删除图像名称。
完成删除图像后,您可以使用以下命令重定向回此页面
redirect($_SERVER['HTTP_REFERER']);
编辑
如果你有图像表。您可以像这样划分 getProducts 查询 (我的表是属性)
$data=$this->db->query('SELECT property.*, users.name as posted_by, property_types.name as property_type
from property
inner join users on users.id=property.posted_by
inner join property_types on property_types.id=property.type
order by property.posted_date desc
')->result_array();
for($i=0;$i<count($data);$i++)
{
$data[$i]['images']=$this->db->query('SELECT * from property_images WHERE property_images.property_id='.$data[$i]['id'])->result_array();
}
return $data;
关于php - 如何从 codeigniter 的编辑页面中删除(实际编辑)图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43245778/