codeigniter-如何在表列中添加值 [英] codeigniter - how to add up values in table column
问题描述
试图从我的表(non_clinical_total_tests)中获取在列(af_am_msm)中输入的值的总和。我想在html表中显示总计。
Trying to get the sum of the values entered within a column (af_am_msm) from my table (non_clinical_total_tests). I want to display that total within an html table.
我的错误消息是:
遇到PHP错误-严重性:注意-消息:数组到字符串的转换
A PHP Error was encountered - Severity: Notice - Message: Array to string conversion
我的模型:
public function af_am_sum()
{
$this->db->select_sum('af_am_msm');
$query = $this->db->get('non_clinical_total_tests');
return $query->result();
}
我的控制器:
public function index()
{
$data['af_am_total'] = $this->Page_model->af_am_sum();
$data['pages'] = $this->Page_model->get_list();
$this->template->load('admin', 'default', 'district1', $data);
}
我的观点:
<td><?php echo $af_am_total; ?></td>
推荐答案
Codeigniter的 result()
函数始终返回对象数组。在您的视图中,您将其作为字符串回显,因此出现错误。为避免产生结果行,请在数组中循环,或:
Codeigniter's result()
function always returns an array of objects. In your view you echo it out as a string, hence the error. In order to avoid that you either produce result rows, looping through the array, or:
如您的示例所示,因为您期望只有一行(一列的总和),所以您有可能获得该结果行(而不用需要遍历它)与 row()-> the_name_of_your_column
。为了简化输出,我们可以为mysql sum创建一个别名,将其命名为第二个参数:
like in your example, as you are expecting only one row (the sum of a column) you have the possibility to get this result row (without the need to loop through it) with row()->the_name_of_your_column
. To simplify the output we can create an alias for the mysql sum, naming it as second parameter:
$this->db->select_sum('af_am_msm','my_sum');
$query = $this->db->get('non_clinical_total_tests');
return $query->row()->my_sum;
这会在您的视图中以假装的形式出现
which echos in your view as pretended
这篇关于codeigniter-如何在表列中添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!