0

I have a add row function to add the multiple input field rows for uploading multiple file in a form. Here i try to change the input name while adding a row except the default first row of input field. but it is changing the first row name too. How to solve it ? Thanks for your Help !! (I using PHP Codeigniter and Mysql).

//my add row function below

function addRow(tableID) {
    /* here i used the Datatable in a form and i get the count of existing rows */
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length; //this to get the count of row
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;

    /* Here I change the input names using id */
    $("#uploadBtn").attr('name',"image"+rowCount+"[]"); 

    for(var i=0; i<colCount; i++) {

        var newcell = row.insertCell(i);
        newcell.innerHTML = table.rows[0].cells[i].innerHTML;
        switch(newcell.childNodes[0].type) {
            case "text":
                    newcell.childNodes[0].value = "";
                    break;
            case "checkbox":
                    newcell.childNodes[0].checked = false;
                    break;
            case "select-one":
                    newcell.childNodes[0].selectedIndex = 0;
                    break;
        }
    }
}

// my model function to upload a file

function file_upload() {

    foreach($val as $row) { //Here i getting different names of input fields in $row

        if(!empty($_FILES[$row])){
            log_message('debug',$row);
            $valid_formats = array("jpg","jpeg", "png", "gif","bmp");
            $max_file_size = 1024*1024*1024*1024; //500 kb
            $path = "uploads/image/"; // Upload directory
            $count = 0;
            // Loop $_FILES to execute all files
            foreach ($_FILES[$row]['name'] as $f => $name) {
                if ($_FILES[$row]['error'][$f] == 4) {
                    continue; // Skip file if any error found
                }
                if ($_FILES[$row]['error'][$f] == 0) {
                    if ($_FILES[$row]['size'][$f] > $max_file_size) {
                        $message[] = "$name is too large!.";
                        continue; // Skip large files
                    }
                    elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                        $message[] = "$name is not a valid format";
                        continue; // Skip invalid file formats
                    }
                    else{ // No error found! Move uploaded files
                        if(move_uploaded_file($_FILES[$row]["tmp_name"][$f], $path.$_FILES['name'])) {
                            $image_names[] ="prop".$prop_count."_".$prop_id."_img".$count;
                        }

                       $count++; // Number of successfully uploaded file
                    }
                }
            }
        }

        else{

            $img="";
        }
        $img = implode(',',$image_names);
    }
}
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103

1 Answers1

0

try reading this site: http://www.ahowto.net/php/easily-integrateload-phpexcel-into-codeigniter-framework

it says there, you can set value for each sell.