我想知道你们所有经验丰富的编码员如何从我的数据库中获取一个值,该数据库存储为双数据
(变量监管和加班)
然后计算两列中每一列的总和。
foreach ($result as $row) {
echo '<tr><td>' . $row['username'] . '</td>
<td>' . $row['date'] . '</td>
<td>' . $row['jobnumber'] . '</td>
<td>' . $row['customer'] . '</td>
<td>' . $row['worksite'] . '</td>
<td>' . $row['duties'] . '</td>
<td>' . $row['regulartime'] . '</td>
<td>' . $row['overtime'] . '</td></tr>';
}
非常感谢你的帮助。
泰勒
EDIT 我想在两者的底部添加最后一行以显示总和
答案 0 :(得分:2)
function calculateSum(array $data, $field)
{
return array_reduce(
$data,
function ($result, $elem) use ($field) {
return $result + $elem[$field];
}, 0
);
}
calculateSum($result, 'regulartime');
calculateSum($result, 'overtime');
答案 1 :(得分:2)
由于您希望显示包含每行的表格,然后是包含总和的最后一行,您只需使用现有的<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
protected function button1_clickHandler(event:MouseEvent):void
{
for(var i:int = 0; i<provider.length; i++) {
Alert.show(provider.getItemAt(i).data);
}
}
]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" borderColor="#FFFFFF" cornerRadius="0">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.core.Application;
[Bindable]
private var provider:ArrayCollection = new ArrayCollection([
{data:'1'},
{data:'2'}]);
]]>
</mx:Script>
<!-- Here is the data grid , the provider is groupeCollection, ihave set editable to true-->
<mx:DataGrid id="myGrid" width="100%" height="80%"
dataProvider="{provider}" x="0" editable="true" >
<mx:columns>
<mx:DataGridColumn dataField="data" >
<mx:itemRenderer>
<mx:Component>
<mx:NumericStepper minimum="1" maximum="10000" />
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Canvas>
<mx:Button label="Check DP" click="button1_clickHandler(event)"/>
</mx:VBox>
</mx:Application>
循环来增加总计:
foreach
但是,如果您不需要首先显示$totalRegularTime = 0;
$totalOverTime = 0;
foreach ($result as $row) {
echo '<tr><td>' . $row['username'] . '</td>
<td>' . $row['date'] . '</td>
<td>' . $row['jobnumber'] . '</td>
<td>' . $row['customer'] . '</td>
<td>' . $row['worksite'] . '</td>
<td>' . $row['duties'] . '</td>
<td>' . $row['regulartime'] . '</td>
<td>' . $row['overtime'] . '</td></tr>';
$totalRegularTime += $row['regulartime'];
$totalOverTime += $row['overtime'];
}
echo '<tr><td colspan="6">TOTAL</td>
<td>' . $totalRegularTime . '</td>
<td>' . $totalOverTime . '</td></tr>';
中的所有行,那么最好的解决方案就是使用SQL获取总和。
如果您不能使用SQL,那么 Eternal1 的答案是一个不错的选择(例如,如果您的数据不是来自数据库)。它很干净。
答案 2 :(得分:1)
我会在SQL中而不是在PHP中执行此操作。
SELECT username, date, jobnumber, customer, worksite, duties, regulartime, overtime, (overtime + regulartime) as totaltime FROM table;