Codeigniter单击更改图像,但在第二次编辑时不起作用?

时间:2018-09-12 08:38:29

标签: php codeigniter-3

我有一个基于CI3的简单博客,我在管理面板中有该表格可以添加文章:

enter image description here

BLOG图片是默认图片,当我单击它时,出现上传表单,我可以从计算机中选择其他图片,然后单击“保存”按钮,一切正常,但是当我再次编辑本文但又没有设置图片时, ,它显示的是我第一次设置的图像,但是在DB列中单击“保存”按钮后,图像为空,并且图像消失了,这是我的代码:

控制器/编辑():

public function edit($id = '')
{
    $data['header_title'] = 'Блог - Добави';
    $this->load->model('admin/Blog_Model');

    if (!empty($this->input->post())) {

        $config['upload_path'] = './assets/img/blog/'; //Use relative or absolute path
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width'] = '5000';
        $config['max_height'] = '5000';
        $config['overwrite'] = FALSE;
        $this->upload->initialize($config);

        $this->upload->do_upload('image');
        $uploadData = $this->upload->data();
        $image = $uploadData['file_name'];

        $user = $this->Blog_Model->editPost($this->input->post(), $image, $id);
        redirect(site_url('admin/blog/showAll'));
    }

    $this->getForm($id);
}

Controller / getForm():

public function getForm($id = '')
{
    $data['header_title'] = 'Блог';

    if (!empty($id)) {
        $post = $this->Blog_Model->getPost($id);
        $data['action'] = 'admin/blog/edit/'.$id;
    } else {
        $data['action'] = 'admin/blog/add';
    }

    $data['formTitle'] = array(
        'name'          => 'title',
        'id'            => 'content-title',
        'value'         => isset($post['title']) ? $post['title'] : '',
        'placeholder'   => 'Заглавие',
        'class'         => 'form-control'
    );

    $data['formContent'] = array(
        'name'          => 'content',
        'id'            => 'content-blog',
        'value'         => isset($post['content']) ? $post['content'] : '',
        'placeholder'   => 'Съдържание',
    );

    $data['formImage'] = !empty($post['image']) ? $post['image'] : 'blog.png';


    $data['formButton'] = array(
        'type' => 'submit',
        'content'=> 'Изпрати',
        'class'=> 'btn btn-primary btn-block btn-flat'
    );

    $data['head'] = $this->load->view('admin/common/head', NULL, TRUE);
    $data['left_column'] = $this->load->view('admin/common/left_column', NULL, TRUE);

    $this->load->view('admin/common/header', $data);
    $this->load->view('admin/blog/blog_form', $data);
    $this->load->view('admin/common/footer');
}

查看/博客表单:

<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<?php echo $head; ?>
<?php echo $left_column; ?>


  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        БЛОГ
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="#">Tables</a></li>
        <li class="active">Data tables</li>
      </ol>
    </section>
    <!-- Main content -->
    <section class="content">
      <div class="row">
        <div class="col-md-12">
          <div class="box box-info">
            <div class="box-header">
              <h3 class="box-title"><b>Добави новина:</b></h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body pad">
                <?php echo form_open_multipart($action); ?>

                <div class="box-body">
                <div class="form-group">
                  <label for="content-title">Заглавие:</label>
                   <?php echo form_input($formTitle); ?>
                </div>
                <div class="form-group">
                    <label for="content_blog">Съдържание:</label>
                    <?php echo form_textarea($formContent); ?>
                </div>
                <div class="col-xs-12 col-md-3 pull-right">
                    <?php echo form_button($formButton); ?>
                </div>
                <div class="image-upload">
                    <label for="image-input">
                        <img src="<?php echo base_url('assets/img/blog/' . $formImage); ?>" alt="" class="img-thumbnail" id="image-blog"/>
                    </label>
                    <input type="file" id="image-input" name="image" onchange="readPath(this);"/>
                </div>

              <?php echo form_close(); ?>
            </div>
          </div>
          <!-- /.box -->

        </div>
        <!-- /.col-->
      </div>
      <!-- ./row -->
    </section>
    <!-- /.content -->
  </div>
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->

<script>
        function readPath(input) {
        console.log('blog');
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#image-blog').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#image-input").change(function(){
        readPath(this);
    });
</script>

以及模型/ getPost(),editPost()方法:

public function getPost($id)
{
    $this->db->select()->from('blog')->where('id', $id);
    $query = $this->db->get();
    return $query->row_array();
}

public function editPost($inputData, $image, $id)
{
    $data = array(
        'title' => $inputData['title'],
        'content' => $inputData['content'],
        'image' => $image
    );

    $this->db->where('id', $id);
    $this->db->update('blog', $data);
}

任何想法都会有所帮助,在视图文件的底部,您可以看到js脚本来替换图像。

1 个答案:

答案 0 :(得分:0)

嗯..我取得了一些进步

在控制器中,我更改此行:

$data['formImage'] = !empty($post['image']) ? $post['image'] : 'blog.png';

具有以下行:

if (!empty($this->input->post('image'))) {
    $data['image'] = $this->input->post('image');
} elseif (!empty($post)) {
    $data['image'] = $post['image'];
} else {
    $data['image'] = '';
}

if (!empty($this->input->post('image'))) {
   $data['formImage'] = $post['image'];
} elseif (!empty($post)) {
    $data['formImage'] = $post['image'];
} else {
    $data['formImage'] = 'blog.png';
}

然后在该行下方的查看文件中:

<label for="image-input">
  <img src="<?php echo base_url('assets/img/blog/' . $formImage); ?>" alt="" class="img-thumbnail" id="image-blog"/>
</label>

添加了该行:

<input type="hidden" name="image" value="<?php echo $image; ?>" id="input-image" />

在模型中,将编辑方法更改为此:

public function editPost($inputData, $id)
{
    $data = array(
        'title' => $inputData['title'],
        'content' => $inputData['content'],
        'image' => $inputData['image']
    );

    $this->db->where('id', $id);
    $this->db->update('blog', $data);
}

现在,我可以在不丢失图像的情况下进行每次编辑,但是每次我保存并打开同一图像时,它都只能以视觉方式更改图像。

编辑:

  

好,最后我做到了,像这样:

    $image = !empty($uploadData['file_name']) ? $uploadData['file_name'] : $this->input->post('image');

    $user = $this->Blog_Model->editPost($this->input->post(), $image, $id);

但是我认为,这不是很好的方法,如果有人可以提出更好的方法,请!