横幅图像未正确重新插入数据库

时间:2018-04-11 01:19:48

标签: codeigniter codeigniter-3

当我更新我的banner_image时,应删除数据库中的那些然后重新插入已设置的那些。

目前它的作用是删除横幅图像,然后重新插入空白banner_image

但是应该删除那个enitre行。

  

问题如何确保我何时更新它将删除不再选择的图片行而不重新插入

enter image description here

public function update($bid = NULL, $data = array()) {

$banner_update = array('banner_title' => $data['banner_title'], 'banner_status' => $data['banner_status']);

$this->db->where('bid', $bid);
$this->db->update('banner', $banner_update);

$this->db->where('bid', $bid)->delete('banner_image');

    if (isset($data['banner_image'])) {

        $banner_images = array();

        $i = 0;

        foreach ($data['banner_image'] as $image) {
            $banner_images[$i] = array(
                'bid' => $bid,
                'banner_image' => $image['image'],
            );

            $i++;
        }

        $this->db->insert_batch('banner_image', $banner_images);

    }

}

查看

<?php echo form_open($action);?>
<div class="container">

    <div class="card mt-3">

    <div class="card-body">

    <?php if (validation_errors()) {?> 

    <?php unset($_POST);?>

    <div class="bg-error-warning">
    <ul>
    <?php echo validation_errors('<li>', '</li>');?>
    </ul>
    </div>

    <?php }?>

    <div class="row">
    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <p>Banner Title</p>
        <div class="form-group">
            <input type="text" name="banner_title" value="<?php echo $banner_title;?>" placeholder="Enter Banner Title" class="form-control">
        </div>
    </div>
    </div>

    <div class="row">
    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
    <table id="images" class="table table-striped table-bordered">
        <tbody>
            <tr>
            <td>
            <button type="button" onclick="addImage();" data-toggle="tooltip" title="Add Banner!" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button>
            </td>
            </tr>

            <?php $image_row = 0; ?>
            <?php foreach ($banner_images as $banner_image) { ?>
            <tr id="image-row<?php echo $image_row; ?>">
                <td>
                <a href="" id="thumb-image<?php echo $image_row; ?>" data-toggle="image">
                    <img src="<?php echo $banner_image['thumb']; ?>" alt="" title="" data-placeholder="<?php echo $placeholder; ?>" class="img-thumbnail"/></a>
                    <input type="hidden" name="banner_image[<?php echo $image_row; ?>][image]" value="<?php echo $banner_image['image']; ?>" id="input-image<?php echo $image_row; ?>" />
              </td>
            </tr>

            <?php $image_row++; ?>
            <?php } ?>
        </tbody>

    </table>

    </div>
    </div>

    <div class="row">
    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <p>Banner Status</p>
        <div class="form-group">

            <?php $options = array('0' => 'Disabled', '1' => 'Enabled'); echo form_dropdown('banner_status', $options, $banner_status, array('class' => 'form-control'));?>
        </div>
    </div>
    </div>

    <div class="row">
    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <div class="form-group">
            <button type="submit" class="btn btn-block btn-dark">Create Banner</button>
        </div>
    </div>
    </div>
    </div>
    </div>
</div>
<?php echo form_close();?>
<script type="text/javascript">
var image_row = <?php echo $image_row; ?>;  

function addImage() {
html  = '<tr id="image-row' + image_row + '">';
html += '<td class="text-left">';
html += '<a href="" id="thumb-image' + image_row + '" data-toggle="image" >';
html += '<img src="<?php echo $placeholder; ?>" width="100" height="100" class="img-thumbnail" data-placeholder="<?php echo $placeholder; ?>"/>';
html += '<input type="hidden" name="banner_image[' + image_row + '][image]" value="" id="input-image' + image_row + '" />'
html += '</a>';
html += '</td>';

html += '<td class="text-left">';
html += '<button type="button" onclick="$(\'#image-row' + image_row  + '\').remove();" data-toggle="tooltip" title="Remove Banner!" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button>'
html += '</td>';

html += '<script>';
html += '$(document).ready(function(){';
html += '$(\'[data-toggle="tooltip"]\').tooltip();';
html += '});';
html += '<\/script>';

$('#images tbody').append(html);

image_row++;

}

</script>

1 个答案:

答案 0 :(得分:0)

让它工作只需要在foreach循环中放空

public function update($bid = NULL, $data = array()) {

    $banner_update = array('banner_title' => $data['banner_title'], 'banner_status' => $data['banner_status']);

    // Updates the main banner table
    $this->db->where('bid', $bid);
    $this->db->update('banner', $banner_update);

    // Removes banners so can have clean insert of new banners selected
    $this->db->where('bid', $bid);
    $this->db->delete('banner_image');

    // Inserts new banner images & saved ones.
    if ($this->input->post('banner_image')) {
        foreach ($this->input->post('banner_image') as $image) {
            if (!empty($image['image'])) {
                $this->db->insert('banner_image', array('bid' => $bid, 'banner_image' => $image['image']));
            }
        }
    }
}