2013-05-15 16 views
-3

count計數字段內容的表

我有這樣的表,怎麼算從計數列(30 + 15 + 20)在PHP腳本字段內容?
如果有人有片段,源代碼或教程,請告訴我:)

foreach($row->result_array() as $row) { 
    <td><div align="center"><?PHP echo $row['No']; ?></div></td> 
    <td><div align="center"><?PHP echo $row['Count']; ?></div></td> 
} 

回答

3

初始化變量與價值0 ...不是使用+=將值添加到您的$total

$total = 0; 

foreach($row->result_array() as $row) { 
    $total += $row['Count']; //Add this to your loop 
    /* What we are doing here is adding the value of $row['Count'] to $total 
     $total value will be available on each iteration and you keep on adding 
     values of $row['Count'] 
    */ 
?> 
    <td><div align="center"><?PHP echo $row['No']; ?></div></td> 
    <td><div align="center"><?PHP echo $row['Count']; ?></div></td> 
<?php 
} 

echo $total; 

另外最好的方法是在您的查詢中使用SUM()

+1

謝謝,它的工作:) – hurifajri

+0

@oyiym歡迎你:) –

+0

你能幫我介紹一下substr嗎?如何格式化這個$ 50000到$ 50.000 – hurifajri

3
$count = 0; 
foreach($row->result_array() as $row) { 
    echo('<td><div align="center">'.$row['No'].'</div></td> 
    <td><div align="center">'.$row['Count'].'</div></td>'); 
    $count += $row['Count'] 
} 
echo 'total count is '.$count; 
+0

謝謝,你的回答類似:) – hurifajri