【问题标题】:CodeIgniter: Passing information from the Model to the Controller using result_array()CodeIgniter:使用 result_array() 将信息从模型传递到控制器
【发布时间】:2012-06-12 08:18:30
【问题描述】:

我有print_r 显示Array ( [id] => 1 [cms_name] => Content Mangement System ) 的以下模型目前在我的控制器中我有$data['contentMangement'] = $this->model->function 但我不确定如何将上面的数组带入其中。

function systemOptions($options)
    {   
        $this->db->select($options);

        $query = $this->db->get('options');

        if($query->num_rows() > 0)
        {
            $row = $query->row_array();

            $row['cms_name'];
        }
                print_r($row);

        return $query->result_array();
    }

【问题讨论】:

  • 返回 $row 而不是 $query->result_array()

标签: php sql codeigniter model controller


【解决方案1】:

如果您想要所有选项,您可以这样做:

function systemOptions($options)
{   
    $this->db->select($options);

    return $this->db->get('options')->result_array();// will return empty array if there are no results

}

或者,如果您只想要第一行(从您的问题看就是这样),您可以这样做:

function systemOptions($options)
{   
    $this->db->select($options);

    $result = $this->db->get('options')->result_array();

    if(!empty($result))
    {
      return $result[0];
    }else{
      return null; //or false or array(), or whatever you want
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 2012-12-12
    • 1970-01-01
    相关资源
    最近更新 更多