php - 我如何对模型页面中创建的表进行计算
问题描述
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 * 值之类的计算并在值文本框中显示。在模型代码中我已经在该表中创建了表如何计算.......在模型页面中创建。
解决方案
从下面的代码更新你的 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>
推荐阅读
- php - 在 PHP 中解码 base64 与 html 混合
- mediawiki - 编辑 MediaWiki 元数据
- php - 如何使用php在mysql中插入动态输入值同时将一个表复制到另一个表
- linphone - 如何在 linphone android 应用程序中获取调试日志?
- mysql - heroku上的图片问题
- python - 从服务器自动运行带有 python 脚本的 bash 文件
- c++ - 写出正确的逻辑表达式
- xamarin.forms - 用于 UWP 的 Xamarin 表单集合视图 ItemsUpdatingScrollMode
- hibernate - 用于创建用户的参数化查询在 Spring Data JPA 中抛出丢失的用户或角色名称异常
- machine-learning - 如何自动选择时间序列模型进行预测?