简体   繁体   English

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

[英]How to save dynamic generated table data to mysql database in php codeigniter

I had Created a Web App in PHP Code igniter which generates table containing calculation of employees earning, deduction. 我在PHP代码点火器中创建了一个Web应用程序,它生成包含员工收入,扣除计算的表。 Since I am newbie to CI. 因为我是CI的新手。 I am trying to save the dynamic generated table data to database, but failed. 我试图将动态生成的表数据保存到数据库,但失败了。 I am not getting that how to POST table data to Controller. 我没有得到如何将表数据POST到Controller。 Any help is welcome. 欢迎任何帮助。
This is My Controller To generate dynamic table 这是我的控制器生成动态表

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);

    }

This Is My View to echo table values 这是我的视图以回显表值

  <?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(); ?>

Its not too clear on how you are currently generating your table, but regardless, when you generate your table you have to place it in a form, and assign keys to inputs. 它不太清楚你当前如何生成表格,但无论如何,当你生成表格时,你必须将它放在一个表格中,并为输入分配键。 Try this approach: 试试这种方法:

HTML (these are the results you should get when you generate your table) 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>

Controller: 控制器:

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);
    }
}

Model: 模型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM