0

I would like to know if possible in my php codeigniter modelto be able to insert my file names where it matches up with the link insert post. The link post are in array i.e. <input type="text" name="link[]" />

Not sure whats best the name of the file inserts but over does not insert correct name. The input for user file is <input type="file" name="userfile+row_id" the row id produces auto generated number.

Model

public function add_banner_image($banner_id = 0) {
        foreach ($this->input->post('link') as $link) {

        $data = array(
            'banner_id' => $banner_id,
            'link' => $link,
        );

        foreach($_FILES as $field => $file) {
            if (!empty($file)) {

                if ($this->upload->do_upload($field)) {

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

                    $data['image'] = $file_data['file_name'];

                }
            }
        }

        $this->db->insert_id();
        $this->db->insert('banner_image', $data);

        }

    }

**View **

<div class="panel-body">
<?php echo $this->load->view('flashdata/flashdata.tpl');?>
<?php if ($error_warning) { ?>
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<?php echo $error_warning; ?>
</div>
<?php } ?>
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-banner" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-banner-name">Banner Name</label>
<div class="col-sm-10">
<input type="text" name="name" value="" placeholder="Banner Name" id="input-banner-name" class="form-control" />
<?php if ($error_name) { ?>
<div class="text-danger"><?php echo $error_name; ?></div>
 <?php } ?>
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label" for="input-status">Banner Status</label>
<div class="col-sm-10">
<select name="status" id="input-status" class="form-control">
<option value="1">Enable</option>
<option value="0">Disabled</option>
</select>
</div>
</div>
<table name="images" class="table table-bordered">
</table>
</form>

</div>
<script>
var row_id = <?php echo $row_id;?>; // Int from controller.

jQuery(document).ready(function() {
  $('button[name="new-title"]').on('click', function(){
    var table = $('table[name="images"]');
    var col_1 = $('<input />', {'name': 'title[]', 'class' : 'form-control', 'type': 'text', 'placeholder': 'Title'});
    var col_2 = $('<input />', {'name': 'link[]', 'class' : 'form-control', 'type': 'text', 'placeholder': 'URL'});
    var col_3 = $('<img src="" id="previewHolder'+ row_id +'"/>');
    var col_4 = $('<input />', {'type': 'file', 'id': 'file'+ row_id, 'name': 'userfile'+ row_id, 'size': '20'});
    create_table_row(table,[col_1,col_2,col_3,col_4]);
    row_id++;
  });

});

function create_table_row($table,cols){
    $row = $('<tr/>');
    for(i=0; i<cols.length; i++){
        $col = $('<td/>');
        $col.append(cols[i]);
        $row.append($col);
    }
    $table.append($row);
}
</script>

Controller

protected function validateForm() {
        if ((utf8_strlen($this->input->post('name')) < 3) || (utf8_strlen($this->input->post('name')) > 64)) {
            $this->error['name'] = 'Name Required!';
        }

        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|png|jpg';
        $config['max_size']    = '0';
        $config['max_width'] = '*';
        $config['max_height'] = '*';
        $config['overwrite'] = TRUE;

        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        foreach($_FILES['userfile']['name'] as $field => $file) {

            if (!empty($file)) {

                if ($this->upload->do_upload($field)) {

                } else {

                    $this->error['warning'] = $this->upload->display_errors();

                }

            }

        }

        return !$this->error;
    }
2
Contributors
1
Reply
16
Views
2 Years
Discussion Span
Last Post by mtho
0

I know this isn't the answer you want but I would suggest you save yourself a lot of time (and headaches) by switching to Laravel as soon as you can. CI was nice 4-5 years ago but it's time you let go.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.