2016-08-15 63 views
1

你好,我正在創建一個學校管理系統,並面臨一個小問題。 的問題是,我有一個表中如何使用代碼點火器在數據庫中提交多行表格形式數據?

enter image description here

包含以下字段的複雜形式I具有其中我要插入的數據的表的費用結構。

具有列

id | classID | sectionID | feetypeID | must | amount | active 
1  1   1   1  must  500  yes 
2  1   1   2  optional 500  no 

所以如何創建一個數組,使得它使用上述形式在表中插入數據。 基本上,我希望用戶選擇他們想要的類型並設置它們的數量,然後選擇他們想要應用它的類,以便在數據庫中爲每個部分和他們選擇的每個類型創建一個條目。

我不知道如何使控制器和模型,但我有鑑於這個代碼

查看

<form class="form-horizontal" role="form" method="post"> 
<div class="box-body"> 
<div class="row"> 

<div class="col-sm-12"> 

<h3>Fee Types</h3><br> 
<div id="hide-table"> 

<table id="example1" class="table table-striped table-bordered table-hover dataTable no-footer"> 
    <thead> 

     <tr> 
<th class="col-sm-2"><?=$this->lang->line('slno')?></th> 
<th class="col-sm-2"><?=$this->lang->line('feetype')?></th> 
<th class="col-sm-2"><?=$this->lang->line('amount')?></th> 
<th class="col-sm-2"><?=$this->lang->line('must')?></th> 
<th class="col-sm-2"><?=$this->lang->line('active')?></th> 
<th class="col-sm-2"><input type="checkbox"> Select All</th> 
</tr> 
</thead> 
<tbody> 
<?php if(count($feetypes)) {$i = 1; foreach($feetypes as $feetype) { ?> 
<tr> 
<td data-title="<?=$this->lang->line('slno')?>"> 
<?php echo $i; ?> 
</td> 
<td data-title="<?=$this->lang->line('feetype_name')?>" name="feetypeID" value="<?php echo $feetype->feetypeID;?>"> 
<?php echo $feetype->feetype; ?> 
</td> 

<td data-title="<?=$this->lang->line('amount')?>" name="amount"> 
<input type="text" class="form-control" value="<?php echo $feetype->defaultamount; ?>"> 
</td> 
<td data-title="<?=$this->lang->line('must')?>"> 
           <select class="form-control" name="must"> 
           <option value="MUST">Must</option> 
           <option value="OPTIONAL">Optional</option> 
           </select> 

</td> 
<td data-title="<?=$this->lang->line('active')?>"> 
           <select class="form-control" name="active"> 
           <option value="1">Yes</option> 
           <option value="0">No</option> 
           </select> 

</td> 
           <td data-title="<?=$this->lang->line('select')?>"> 
           <input type="checkbox" name="selected"> 
</td> 

</tr> 
<?php $i++; }} ?> 
</tbody> 
</table> 
</div> 


      <div class="row"> 
      <div class="col-sm-8"> 
     <h3> Apply To</h3><br> 
     </div> 
     </div> 
     <div class="row"> 
      <div class="col-sm-12"> 
    <table class="table table-bordered table-striped"> 
<thead> 
    <th class="col-sm-3">Class</th> 
    <th class="col-sm-4">Section</th> 
</thead> 
<tbody> 
<?php 
foreach ($categories as $category) 
    { 
?> 
<tr> 
<td> <input type="checkbox"><span style="font-size:18px;margin-left:10px;"> <?php echo $category->classes; ?></span> 
</td> 

<td> 
<?php 
if(!empty($category->subs)) { 

    foreach ($category->subs as $sub) { 
     echo "<input type='checkbox'> ".$sub->section.'&nbsp;&nbsp;&nbsp;&nbsp;' ; 
    } 

} 
?> 
</td> 
</tr> 
<?php 
    } 
?> 
</tbody> 
    </table> 
    <br><br> 
          <div class="form-group"> 
<div class="col-sm-12"> 
          <input type="submit" class="btn btn-success" value="<?=$this->lang->line("add_feestructure")?>" > 
    </div> 
</div> 
</div> 




      </form> 

控制器

public function addfeestructure(){ 
    $usertype = $this->session->userdata("usertype"); 
    if($usertype == "Admin" || $usertype == "Accountant"){ 
     $this->data['categories'] = $this->feestructure_m->get_categories(); 

     $this->data['feetypes'] = $this->feestructure_m->get_feetypes(); 
     if($_POST){ 

     } 
     $this->data['subview'] = "feestructure/addfeestructure"; 
     $this->load->view('_layout_main', $this->data); 
    } 
} 
+0

這裏是你的代碼?你有什麼試過,怎麼會出錯? –

+0

@SJ Sethi向我們顯示您的代碼。你面臨的錯誤是什麼? – Lokesh

+0

我已經添加了一些代碼,請更正它謝謝。 –

回答

0

要提交您需要爲您的名稱屬性添加方括號這樣的多個相同名稱的字段; <input type="text" name="fieldName[]">

然後在您的控制器,您將使用foreach循環來處理形式:

public function submitForm() { 
    $fieldA = $this->input->post('fieldName'); // this will be an array 

    for ($i = 0; $i < sizeof($fieldA); $i++) { 
     $array = array('fieldA' => $fieldA[$i]); 

     $this->your_model->addRecordToDatabase($tableName, $array); 
    } 
} 
相關問題