首页 > 解决方案 > 我如何对模型页面中创建的表进行计算

问题描述

public function fetch_item($item)
{
   $this->db->where("pgroup",$item);
   $this->db->select('*');
  $this->db->from('itemmaster');
   $this->db- 
 >join('pgroup','pgroup.pgroupid 
     = itemmaster.catcode','left 
 outer');
     $query_result = $this->db->get()- 
    >result();

        //pass query result as html

        $output = '<table class="table 
      table- 
     striped table-bordered table- 
        hover">
        <thead>
        <tr>
          <th>Product Name</th>
            <th>Rate</th>
           <th>Qty</th>
        <th>amount</th>
      </tr>
     </thead>
  <tbody>';

      if($query_result !='false'){

       foreach ($query_result as $key 
         => 
         $value) {

       $output .='<tr>
          <td>'.$value- 
             >product_name.'</td>
         <td><input style="width:100px"  
         name="rate" type="text" 
    class="form- 
     control input-xs"   value=""></td>
     <td><input style="width:100px"  
      name="qty" type="text" 
       class="form- 
       control input-xs"    value=""> 
    </td>
      <td><input style="width:100px"  
         name="amount" type="text" 
        class="form- 
       control input-xs"    value=""> 
        </td>
      </tr>';
       }
}

       $output .="</tbody>
  </table>";

 echo $output;

  }

这是用于获取数据并以表格格式创建的模型代码..

我的问题是我如何完成 qty * 值之类的计算并在值文本框中显示。在模型代码中我已经在该表中创建了表如何计算.......在模型页面中创建。

标签: phpcodeigniter

解决方案


从下面的代码更新你的 foreach 循环代码

$i=0;
foreach ($query_result as $key => $value) {

    $output .='<tr>
          <td>'.$value->product_name.'</td>
         <td><input style="width:100px" name="rate" type="text" class="form-control input-xs" value="" id="rate_'.$i.'" onchange="calculate('.$i.')"></td>
         <td><input style="width:100px" name="qty" type="text" class="form-control input-xs" value="" id="qty_'.$i.'" onchange="calculate('.$i.')"> </td>
         <td><input style="width:100px" name="amount" type="text" class="form-control input-xs" value="" id="amount_'.$i.'"> 
         </td></tr>';
   $i++;
}

将以下代码添加到您的视图页面中,确保 jquery min lode 在此脚本之前

<script type="text/javascript">
  function calculate(id){
    var rate=$("#rate_"+id).val();
    var qty=$("#qty_"+id).val();
    if(qty=="")
        qty=0;
    if(rate=="")
        rate=0;
    var total=rate*qty;
    $("#amount_"+id).val(total);

  }
</script>

推荐阅读