简体   繁体   中英

Stop last row from showing in list even though it has been removed from database

Currently on my project the user can upload attachments and delete them

When I comes to delete the last attachment even though removes from database and uploads folder it still shows on table list

Question: When the user delete's there last attachment how can I make sure it does not display it.

<script type="text/javascript">
$(document).ready(function() {
    $(document).on('click', '#delete_button', function () {
        $.ajax({
            url: "<?php echo base_url('extensions/attachments/delete');?>",
            type: 'post',       
            dataType: 'json',
            data: {
                attachment_id: $(this).attr("data-id"),
                posthash: $('#posthash').val()
            },
            success: function(json) {
                if (json['success'] == true) {

                    $.each(json['attachments'], function( key, value ) {
                        info = '<tr>';
                        info += '<td>';
                        info += value['orig_name'];
                        info += '</td>';
                        info += '<td>';
                        info += value['file_size'] + 'KB';
                        info += '</td>';
                        info += '<td class="text-center">';
                        info += '<button type="button" id="delete_button" class="btn btn-danger" data-id="' + value['attachment_id'] +'"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                        info += '</td>';
                        info += '</tr>';
                    }); 

                    $('#file-attachments tbody').html(info);
                }
            },
        });
    });


    $('#add_attachment').on('click', function() {

    $('#form-upload').remove();

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

    $('#form-upload input[name=\'file\']').trigger('click');

    $('#form-upload input[name=\'file\']').on('change', function() {

        var formData = new FormData($(this).parent()[0]);
        formData.append('posthash', $("#newreply #posthash").val());

        $.ajax({
            url: "<?php echo base_url('extensions/attachments/upload');?>",
            type: 'post',       
            dataType: 'json',
            data: formData,
            cache: false,
            contentType: false,
            processData: false,     
            success: function(json) {
                if (json['error']) {
                    alert(json['error']);
                }

                if (json['success']) {

                    $.each(json['attachments'], function( key, value ) {
                        info = '<tr>';
                        info += '<td>';
                        info += value['orig_name'];
                        info += '</td>';
                        info += '<td>';
                        info += value['file_size'] + 'KB';
                        info += '</td>';
                        info += '<td class="text-center">';
                        info += '<button type="button" id="delete_button" class="btn btn-danger" data-id="' + value['attachment_id'] +'"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                        info += '</td>';
                        info += '</tr>';
                    }); 

                    $('#file-attachments tbody').append(info);
                }
            },          

            });

        });
    }); 
});

</script>

Controller Function

public function delete()
{
    $data['attachments'] = array();

   $data = array('success' => false);

   if ($this->input->post('attachment_id'))
   {
        $data['success'] = true;

        $get = $this->attachment_model->getattachment($this->input->post('attachment_id'));

        if (unlink(FCPATH . 'uploads/' . $get['path']))
        {
            $this->attachment_model->deleteattachment($this->input->post('attachment_id'));
        }

        $attachments_results = $this->attachment_model->getattachmentsfornewreply($get['posthash']);

        foreach ($attachments_results as $attachment)
        {
            $data['attachments'][] = array(
                'attachment_id' => $attachment['attachment_id'],
                'post_id' => $attachment['post_id'],
                'posthash' => $attachment['posthash'],
                'file_name' => $attachment['file_name'],
                'orig_name' => $attachment['orig_name'],
                'file_size' => $attachment['file_size'],
                'path' => $attachment['path'] 
            );
        }
    }

   echo json_encode($data);
}

Solved Now

I have got it working now I had to create a javascript function

Which can just get the attachments and then on the other ajax I call this function return getattachments()

Of course now I have had to create a new php codeigniter controller function which just gets the attachments

$(document).ready(function() {

function getattachments() {
    $.ajax({
        url: "<?php echo base_url('extensions/attachments/getattachments');?>",
        type: 'post',       
        dataType: 'json',
        data: {
            posthash: $('#posthash').val()
        },
        success: function(json) {

            info = '';

            $.each(json['attachments'], function( key, value ) {
                info += '<tr>';
                info += '<td>';
                info += value['orig_name'];
                info += '</td>';
                info += '<td>';
                info += value['file_size'] + 'KB';
                info += '</td>';
                info += '<td class="text-center">';
                info += '<button type="button" id="delete_button" class="btn btn-danger" data-id="' + value['attachment_id'] +'"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                info += '</td>';
                info += '</tr>';
            }); 

            $('#file-attachments tbody').html(info);

        },
    });
}

$(document).on('click', '#delete_button', function (e) {
    e.preventDefault();

    $.ajax({
        url: "<?php echo base_url('extensions/attachments/delete');?>",
        type: 'post',       
        dataType: 'json',
        data: {
            attachment_id: $(this).attr("data-id"),
            posthash: $('#posthash').val()
        },
        success: function(json) {
            return getattachments();
        },
    });
});

$('#add_attachment').on('click', function() {

$('#form-upload').remove();

$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

$('#form-upload input[name=\'file\']').trigger('click');

$('#form-upload input[name=\'file\']').on('change', function() {

    var formData = new FormData($(this).parent()[0]);
    formData.append('posthash', $("#newreply #posthash").val());

    $.ajax({
        url: "<?php echo base_url('extensions/attachments/upload');?>",
        type: 'post',       
        dataType: 'json',
        data: formData,
        cache: false,
        contentType: false,
        processData: false,     
        success: function(json) {
            return getattachments();
        },          

        });

    });
});

});

Controller

<?php

class Attachments extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('extensions/attachment_model');
    }

    public function getattachments(){
        $data['attachments'] = array();

        $attachments_results = $this->attachment_model->getattachmentsfornewreply($this->input->post('posthash'));

        foreach ($attachments_results as $attachment)
        {
            $data['attachments'][] = array(
                'attachment_id' => $attachment['attachment_id'],
                'post_id' => $attachment['post_id'],
                'posthash' => $attachment['posthash'],
                'file_name' => $attachment['file_name'],
                'orig_name' => $attachment['orig_name'],
                'file_size' => $attachment['file_size'],
                'path' => $attachment['path'] 
            );
        }

        echo json_encode($data);
    }

    public function upload()
    {
        $this->load->library('upload');

        $data = array();

        $attachments_results = array();

        // The session userid
        $user_id = $this->session->userdata('user_id');

        // User Upload Folder
        $user_upload_folder = $this->user_model->getuseruploadfolder($user_id);

        $folder = date('Ym');

        if (!is_dir(FCPATH . 'uploads/' . $folder)) {
            mkdir(FCPATH . 'uploads/' . $folder, 0777, true);
        }

        $this->load->helper('string');

        $config['upload_path'] = './uploads/' . $folder . '/';
        $config['allowed_types'] = 'gif|jpg|png|php|JPEG|PNG';
        $config['max_size'] = 5000;
        $config['max_width'] = 0;
        $config['max_height'] = 0;
        $config['file_name'] = 'post_' . $user_id . '_' . time() . '_' . random_string('alpha', 16);

        $this->upload->initialize($config);

        if (!$this->upload->do_upload('file'))
        {
            $data['error'] = $this->upload->display_errors();

        } else {

            $upload_data = $this->upload->data();

            $attachment_insert = array(
                'post_id' => '0',
                'posthash' => $this->input->post('posthash'),
                'user_id' => $this->session->userdata('user_id'),
                'file_name' => $upload_data['file_name'],
                'orig_name' => $upload_data['client_name'],
                'file_size' => $upload_data['file_size'],
                'path' => $folder . '/' . $upload_data['file_name'],
                'datecreated' => time()
            );

            $this->attachment_model->insert($attachment_insert);

            $data['success'] = TRUE;
        }

        echo json_encode($data);
    }

    public function delete()
    {
        $data = array();

        if ($this->input->post('attachment_id'))
        {
            $data['success'] = true;

            $get = $this->attachment_model->getattachment($this->input->post('attachment_id'));

            if (unlink(FCPATH . 'uploads/' . $get['path']))
            {
                $this->attachment_model->deleteattachment($this->input->post('attachment_id'));
            }
        }

       echo json_encode($data);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM