【问题标题】:Count Field Content On Table [closed]计算表上的字段内容[关闭]
【发布时间】:2017-09-01 15:51:27
【问题描述】:

我有这个表,如何从 PHP 脚本的 Count 列 (30+15+20) 中计算字段内容?
如果有人有sn-p,源代码或教程,请告诉我:)

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>
}

【问题讨论】:

    标签: php count html-table


    【解决方案1】:

    使用值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()

    【讨论】:

    • @oyiym 你欢迎 :)
    • 你能帮我谈谈 substr 吗?如何将这 50000 美元格式化为 50.000 美元
    • 那不是正确的转换,分值使用number_formatPHP函数
    【解决方案2】:
    $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;
    

    【讨论】:

    • 谢谢,你的回答很相似 :)
    猜你喜欢
    • 2021-08-29
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多