【问题标题】:Codeigniter Select_Sum returns "array" not numerical value?Codeigniter Select_Sum 返回“数组”而不是数值?
【发布时间】:2011-09-14 08:38:33
【问题描述】:

我需要将所有行加起来以获得结果。使用 select_sum 如下

这是模型

    function Dues_Paid_Tot($date)
{
    $query = $this->db->select_sum('Dues_Paid', 'Dues_Paid_Tot');
    $query = $this->db->get('Membership');
    return $query->result();
}

这里是控制器

    function Fiscal2()
{
$date = $this->input->post('Select_Date');
    if($query = $this->report_model->fiscal_list($date))
    {
        $data['records'] = $query;
    }
$data['date'] = $this->input->post('Select_Date');
$data['Dues_Paid_Tot'] = $this->report_model->Dues_Paid_Tot($date);
$data['main_content'] = 'report_fiscal_view';
$this->load->view('includes/template', $data);
}

这是视图的相关代码

<?php echo $Dues_Paid_Tot; ?>

问题是,我没有显示 Dues_Paid 列中所有条目的总和,而是在我的视图中显示“数组”。

我哪里出错了?

谢谢!

【问题讨论】:

    标签: php mysql arrays codeigniter select


    【解决方案1】:

    您没有做错任何事,这正是 CodeIgniter 的工作原理。来自manual on result()

    此函数将查询结果作为对象数组返回,如果失败则返回空数组。

    因此,您要查找的信息包含在数组第一个元素的对象中。

    如果您执行var_dum($data['Dues_Paid_Tot']);,您将看到如何访问您的值,它可能类似于:

    $data['Dues_Paid_Tot'][0]->some_name
    

    【讨论】:

      【解决方案2】:

      它仍然是一个查询,所以你仍然需要处理结果,尝试将你的模型更改为这个......

      function Dues_Paid_Tot($date)
      {
          $query = $this->db->select_sum('Dues_Paid', 'Dues_Paid_Tot');
          $query = $this->db->get('Membership');
          $result = $query->result();
      
          return $result[0]->Dues_Paid_Tot;
      }
      

      【讨论】:

      • 按照您的建议,我认为结果应该是“消息:stdClass 类的对象无法转换为字符串”
      【解决方案3】:
      function Dues_Paid_Tot($date)
      {
          $query = $this->db->select_sum('Dues_Paid', 'Dues_Paid_Tot');
          $query = $this->db->get('Membership');
          return $query->result();
      }
      

      return $query-&gt;result();

      该函数返回查询结果 作为对象数组或空的 数组失败。

      更多信息请查看http://codeigniter.com/user_guide/database/results.html

      【讨论】:

        猜你喜欢
        • 2020-03-24
        • 2021-10-17
        • 2019-10-30
        • 1970-01-01
        • 1970-01-01
        • 2011-02-09
        • 2023-04-10
        • 2022-01-18
        • 2017-09-03
        相关资源
        最近更新 更多