Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter:从模型中检索变量以在控制器中使用_Php_Codeigniter_Variables - Fatal编程技术网

Php Codeigniter:从模型中检索变量以在控制器中使用

Php Codeigniter:从模型中检索变量以在控制器中使用,php,codeigniter,variables,Php,Codeigniter,Variables,我打赌这很容易,但已经尝试了一段时间,似乎无法让它工作。基本上,我正在设置分页,在下面的模型中,我希望将$total_行传递给我的控制器,以便将其添加到配置中,如“$config['total_行]]=$total_行;” function get_timeline_filter($per_page, $offset, $source) { $this->db->where('source', $source); $this->db-&

我打赌这很容易,但已经尝试了一段时间,似乎无法让它工作。基本上,我正在设置分页,在下面的模型中,我希望将$total_行传递给我的控制器,以便将其添加到配置中,如“$config['total_行]]=$total_行;”

function get_timeline_filter($per_page, $offset, $source) 
    {
        $this->db->where('source', $source);
        $this->db->order_by("date", "desc");
        $q = $this->db->get('timeline', $per_page, $offset);
        $total_rows = $this->db->count_all_results();

        if($q->num_rows() >0) 
        {
            foreach ($q->result() as $row) 
            {
                $data[] = $row;                 
            }
            return $data;
        }

    }

我知道如何使用
$this->example\u model->example($something)将东西从控制器传递到模型但不确定如何从模型中获取变量?

我认为在运行时更改配置不是一个好主意,但我有3种方法可以在控制器中获取$total_rows值

ideia 1。您可以在会话中存储$total\u行的值

ideia 2。通过引用传递变量(使用函数定义中的&$total\u行,例如:

function get_timeline_filter($per_page, $offset, $source, &$total_rows) 
请阅读以下内容:

在控制器中,您可以调用:

$total_rows=0;
$result=$this->model_name->get_timeline_filter($per_page, $offset, $source, &$total_rows);
ideia 3.从函数ex返回值:

(……)

问候,

Pedro

将$total_行设置为模型中的类变量,并从控制器访问它,就像它是任何其他类变量一样

$total = $this->dbmodelname->total_rows;

嗨,佩德罗,我似乎无法实现代码。我对PHP和Codeigniter相当陌生。如果我使用idea 2,我是否要用
函数get_timeline_filter($per_page,$offset,$source)
替换
函数get_timeline_filter($per_page,$offset,$source,$total_行)
?我应该将什么作为代码放在控制器中,还是只需要使用
$config['total\u rows']=$total\u rows;
我已经尝试过了,但我刚刚得到一个错误。模型错误=
消息:Timeline\u Model::get\u Timeline\u filter()缺少参数4
Controller error=
Message:Undefined variable:total_rows
Hi Craig,我在ideia 2中添加了一个更好的描述。请看一看,如果不明白,请随时询问更多信息。我需要您的帮助。请解释以上内容
$total = $this->dbmodelname->total_rows;