CodeIgniter:用于获取包括图像在内的值的控制器

CodeIgniter: controller for getting values including images

本文关键字:控制器 图像 用于 获取 包括 CodeIgniter      更新时间:2023-09-26

我必须提交具有动态列的codeIgniter表单。如何使用循环从控制器获取它们。

这是我的浏览页面

<?php echo form_open_multipart('main_controller/do_insert');?>   
<div id="mainDiv">
 <div><input type="text" name="name0"/></div>
 <div><input type="file" name="img0"/></div>
</div>
<input type="button" value="add an entry" onClick="add('0')">
<input type="submit" value="save all"/>
<?php from_close();?>
<script>
function add(x)
{
 var count=x;count++;
 var str1="<div><input type='text' name='name"+count+"'/></div>"
 var str2="<div><input type='file' name='img"+count+"'/></div>"
 var str3="<input type='button' value='add an entry' onClick='add(`1`)'>";
 $("#mainDiv").append(str1+str2+str3);
}
</script>    

注意:"x"的值可能无法正常工作,但它在这里不相关,可以通过全局变量进行修复。

我的问题是,在提交表单时,我如何对控制器进行编码,以获取包括图像在内的所有值并将其保存到表中。

我的建议,使用文件名作为类似的数组字段。

<?php echo form_open_multipart('main_controller/do_insert');?>   
    <div id="mainDiv">
     <div class='group'>
      <div><input type="text" name="name[]"/></div>
      <div><input type="file" name="img[]"/></div>
     </div>
    </div>
    <input type="button" id="add an entry">
    <input type="submit" value="save all"/>
    <?php from_close();?>

脚本看起来像。。

<script>
 $('#add an entry').on('click', function() {
        var cloneRow = $('.group').clone();
        cloneRow.find("input").val("").end();
        cloneRow.appendTo('#mainDiv');
</script>

然后在控制器中:

foreach($this->input->post() as $post){
//do action on your data...here
}
foreach($_FILES['userfile'] as $key => $value)
{
 //images data
}
$i = 0;
foreach($this->input->post() as $post) {
            echo $post; //Or echo $this->input->post('name'.$i);
            print_r($_FILES['img'.$i]);
            $i++;
        }