我正在尝试从数据库在同一页面上动态填充不同的表!当我在每个表的末尾对项目数量求和时,问题就来了!第一个表的数量总和是正确的,但将前一个数量添加到下一个表的总数量中!请在这方面帮助我,我如何在foreach循环的每个重复div中实现不同的查询,或者我还能做些什么! 有我的代码:
<?php foreach ($items as $row): ?>
<!-- Tab Contents Starts -->
<div class="tab-content">
<div id="list" class="tab-pane fade in active">
<!-- NEW TABLE -->
<div class="row">
<div class="col-xs-12">
<div class="clearfix">
<div class="pull-right tableTools-container"></div>
</div>
<div class="table-header" style="background:<?php echo $color; ?>!important;">
<td><?php echo $row->name; ?> </td>
<button class="green ace-icon fa fa-caret-down bigger-120" data-toggle="collapse" data-target="#<?php echo $row->name; ?>" style="float: right;"></button>
</div>
<!-- div.table-responsive -->
<!-- div.dataTables_borderWrap -->
<div>
<table id="dynamic-table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Material Name</th>
<th>Available Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row->name; ?> </td>
<td><?php echo $row->quantity; ?> </td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div id="<?php echo $row->name; ?>" class="collapse">
<div class="tab-content">
<div id="list" class="tab-pane fade in active">
<!-- NEW TABLE -->
<div class="row">
<div class="col-xs-12">
<div class="clearfix">
<div class="pull-right tableTools-container"></div>
</div>
<?php
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
?>
<div class="table-header" style="background:#66C22D!important;">
<?php echo $row->name; ?> Invoice List
</div>
<!-- div.table-responsive -->
<!-- div.dataTables_borderWrap -->
<div>
<table id="dynamic-table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="center">Invoice ID</th>
<th>Material Name</th>
<th>Quantity</th>
<th>Total Price</th>
<th>Vendor</th>
<th>Vendor Contact</th>
<th>Purchase Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $itemname = $row->name; ?>
<?php $groups = $this->db->get_where('raw' , array('material_name' => $itemname) )->result();
foreach ($groups as $row): ?>
<tr>
<td class="center">
<?php echo $row->invoice_id; ?>
</td>
<td><?php echo $row->material_name; ?> </td>
<td>
<?php echo $row->qty;
$totalqty =$totalqty+ $row->qty;
?>
</td>
<td>
<?php echo $row->price; ?>
</td>
<td>
<?php echo $row->vendor; ?>
</td>
<td>
<?php echo $row->phone; ?>
</td>
<td>
<?php echo $row->dop; ?>
</td>
<td>
<div class="hidden-sm hidden-xs action-buttons">
<!--
<a class="blue" href="#">
<i class="ace-icon fa fa-search-plus bigger-130"></i>
</a> -->
<a class="green" href="<?php echo base_url();?>index.php/admin/raw_crud/do_update/<?php echo $row->id;?>" >
<i class="ace-icon fa fa-pencil bigger-130"></i>
</a>
<a class="red" href="#" onclick="confirm_modal('<?php echo base_url();?>index.php/admin/raw_crud/delete/<?php echo $row->id;?>');">
<i class="ace-icon fa fa-trash-o bigger-130"></i>
</a>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th style="text-align:right">Total Quantity :</th>
<th style="text-align:right;"><?php echo $totalqty; ?></th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
<!-- Tab Contents Ends -->
</div>
<!-- Tab Contents Ends -->
</div>
<!-- Tab Contents Ends -->
</div>
<!-- /.col -->
<?php endforeach; ?>
请只替换表代码,即从表开始到表结束,您的问题将得到解决。
<table id="dynamic-table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="center">Invoice ID</th>
<th>Material Name</th>
<th>Quantity</th>
<th>Total Price</th>
<th>Vendor</th>
<th>Vendor Contact</th>
<th>Purchase Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $itemname = $row->name; ?>
<?php $groups = $this->db->get_where('raw' , array('material_name' => $itemname) )->result();
<?php $totalqty = 0; ?>
foreach ($groups as $row): ?>
<tr>
<td class="center">
<?php echo $row->invoice_id; ?>
</td>
<td><?php echo $row->material_name; ?> </td>
<td>
<?php echo $row->qty;
$totalqty += $row->qty;
?>
</td>
<td>
<?php echo $row->price; ?>
</td>
<td>
<?php echo $row->vendor; ?>
</td>
<td>
<?php echo $row->phone; ?>
</td>
<td>
<?php echo $row->dop; ?>
</td>
<td>
<div class="hidden-sm hidden-xs action-buttons">
<!--
<a class="blue" href="#">
<i class="ace-icon fa fa-search-plus bigger-130"></i>
</a> -->
<a class="green" href="<?php echo base_url();?>index.php/admin/raw_crud/do_update/<?php echo $row->id;?>" >
<i class="ace-icon fa fa-pencil bigger-130"></i>
</a>
<a class="red" href="#" onClick="confirm_modal('<?php echo base_url();?>index.php/admin/raw_crud/delete/<?php echo $row->id;?>');">
<i class="ace-icon fa fa-trash-o bigger-130"></i>
</a>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th style="text-align:right">Total Quantity :</th>
<th style="text-align:right;"><?php echo $totalqty; ?></th>
</tr>
</tfoot>
</table>