如何在php codeigniter中将动态生成的表数据保存到mysql数据库

时间:2015-02-27 12:55:17

标签: php mysql codeigniter

我在PHP Code igniter中创建了一个Web应用程序,它生成包含员工收入,扣除计算的表。 因为我是CI的新手。我试图将动态生成的表数据保存到数据库,但失败了。 我没有得到如何将表数据POST到Controller。 欢迎任何帮助 这是我的控制器生成动态表

function index()
{

    if($this->input->post("submit"))
    {
         $vals=$this->cpanel_model->esic();
     if($vals){
        $data['w_share']=$vals->w_share;
        $data['e_share']=$vals->e_share;
     }

    $data["wshare"]="";
        //echo $data['w_share'];
        $city=$_POST["city"];
        $v1= $data['w_share'];
        $v2= '100';
        $v3= $data['e_share'];
        $arr=$_POST["wid"];
        $arr1=$_POST["days"];
        $arr2=$_POST["payment"];
        $arr3=$_POST["wdays"];
        for ($i=0; $i <count($arr1) ; $i++)
         { 
            $net[$i]=($arr2[$i]/$arr3[$i])*$arr1[$i];

            $wshare[$i]=($net[$i]/$v2)*$v1;
            $eshare[$i]=($net[$i]/$v2)*$v3;
            $total[$i]=$eshare[$i]+$wshare[$i];
            $wdays[$i]=$arr3;
            $days[$i]=$arr1;
         }
         $data["net"]=$net;
         $data["wshare"]=$wshare;
         $data["total"]=$total;
         $data["eshare"]=$eshare;
         $data["wdays"]=$arr3;
         $data["days"]=$arr1;
         $data["list"]=$this->payment_model->search($city);
         $this->load->view("secure/php-version/esic",$data);

    }

这是我的视图以回显表值

  <?php echo form_open('esic/insert',array("role"=>"form")); ?>
             <div class="table-responsive">
               <table border="1" id="t01"  class="table table-striped table-bordered table-hover table-condensed">
<tr>
  <th>Worker_id</th>
  <th>Worker Name</th>
  <th>Worker City</th>
  <th>Designation</th>
  <th>Working Days</th>
   <th>Basic Payment</th>
   <th>Net Payment</th>
  <th>Present Days</th>
  <th>Worker Share (1.75%)</th>
  <th>Employee Share (4.75%)</th>
  <th>Total</th>
</tr>
<?php $i=0; $sum2=0; ?>
<?php  if(count($list)>0) { foreach ($list as $std) {?>
  <tr>
  <?php echo form_hidden("wid[]",$std["wid"]); ?>
  <?php echo form_hidden("wdays[]",$wdays); ?>
  <?php  echo form_hidden("payment[]",$std["payment"]); ?>
  <td><?php echo $std["wid"]; ?></td>
  <td><?php echo $std["wname"]; ?></td>
  <td><?php echo $std["city"]; ?></td>
   <td><?php echo $std["designation"]; ?></td>
   <td><?php echo $wdays[$i]; ?></td>
  <td><?php echo $std["payment"]; ?></td>
   <td><?php echo $net[$i]; ?></td>
<td><?php echo $days[$i]; ?></td>  
   <td><?php echo $wshare[$i];  ?></td>
  <td><?php echo $eshare[$i];  ?></td>
  <td><?php echo $total[$i];  ?></td>
  <?php $sum2 += $std["payment"]; ?>
    </tr>
<?php $i++;}}?>
<tr>
<?php
$sum=0;
$sum1=0;
$sum3=0;
$sum4=0;
$sum5=0;
$i=0;
foreach ($total as $value) {
  $sum = $sum + $value;
  $i++;
}
foreach ($days as $value) {
  $sum1 = $sum1 + $value;
  $i++;
}
foreach ($eshare as $value) {
  $sum3 = $sum3 + $value;
  $i++;
}
foreach ($wshare as $value) {
  $sum4 = $sum4 + $value;
  $i++;
}
foreach ($net as $value) {
  $sum5 = $sum5 + $value;
  $i++;
}
 ?>
<td>
  &nbsp;
</td>
<td>
  &nbsp;
</td>
<td>
  &nbsp;
</td>
<td>
  &nbsp;
</td>
<td>
  &nbsp;
</td>
<td style="font-weight:bold; font-size:10pt;">
  <?php echo $sum2; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
  <?php echo $sum5; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
 <?php echo $sum1; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum4; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum3; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
 <?php echo $sum; ?>
</td>
</tr>
</table>

</div>
         <?php echo form_submit(array("name"=>"submit","type"=>"submit","value"=>"Create ESIC")); ?>
<?php echo form_close(); ?>

1 个答案:

答案 0 :(得分:0)

关于当前如何生成表格并不是很清楚,但无论如何,当您生成表格时,必须将其放在表格中,并为输入分配键。试试这种方法:

HTML(这些是生成表格时应该得到的结果)

<form action="<?= base_url() ?>table_data" method="post">
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Earnings</th>
                <th>Deduction</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="hidden" name="table[0][name]" value="name1">name1</td>
                <td><input type="hidden" name="table[0][earning]" value="earning1">earning1</td>
                <td><input type="hidden" name="table[0][deduc]" value="deduc1">deduc1</td>
            </tr>
            <tr>
                <td><input type="hidden" name="table[1][name]" value="name1">name1</td>
                <td><input type="hidden" name="table[1][earning]" value="earning1">earning1</td>
                <td><input type="hidden" name="table[1][deduc]" value="deduc1">deduc1</td>
            </tr>
        </tbody>
    </table>
    <input type="submit" value="Submit this table">
</form>

控制器:

function table_data() {
    if ($this->input->post()) {
        $table_data = $this->input->post('table');
        $this->load->model('model_name');
        $this->model_name->insert_table_data($table_data);
    }
}

型号:

function insert_table_data($table_data){
    $this->db->insert_batch('mytable', $table_data); 
}