Php 返回模型中的一行,将其传递给控制器和视图-Codeigniter

Php 返回模型中的一行,将其传递给控制器和视图-Codeigniter,php,codeigniter,Php,Codeigniter,很抱歉发布这样一个问题,但我在返回单张时遇到了困难 行并将其传递给模型。以下是我的模型中的方法: public function test($user_id) { $query = $this->db->query("SELECT COUNT(*) AS test FROM test WHERE user_id = '.$user_id.'"); return $query->first_row('array'); } class MY_Controller

很抱歉发布这样一个问题,但我在返回单张时遇到了困难 行并将其传递给模型。以下是我的模型中的方法:

public function test($user_id)
{
    $query = $this->db->query("SELECT COUNT(*) AS test FROM test WHERE user_id = '.$user_id.'");

    return $query->first_row('array');
}
class MY_Controller extends CI_Controller
{
    public $layout;
    public $id;
    public $data = array();

    public function __construct()
    {
        parent::__construct();

        $this->output->nocache();

        $this->load->model('subject_model');
        $this->load->model('user_model');
        $this->load->model('survey_model');
        $this->id = $this->session->userdata('user_id');

        $this->data['check_if_already_posted_it_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('it_survey', $this->id);
        $this->data['check_if_already_posted_lvis_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('lvis_survey', $this->id);
        $this->data['test']= $this->survey_model->test($this->id);


        $this->layout = 'layout/dashboard';
}
下面是我的控制器的一个示例,其中包含来自我的模型的一些其他返回值:

public function test($user_id)
{
    $query = $this->db->query("SELECT COUNT(*) AS test FROM test WHERE user_id = '.$user_id.'");

    return $query->first_row('array');
}
class MY_Controller extends CI_Controller
{
    public $layout;
    public $id;
    public $data = array();

    public function __construct()
    {
        parent::__construct();

        $this->output->nocache();

        $this->load->model('subject_model');
        $this->load->model('user_model');
        $this->load->model('survey_model');
        $this->id = $this->session->userdata('user_id');

        $this->data['check_if_already_posted_it_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('it_survey', $this->id);
        $this->data['check_if_already_posted_lvis_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('lvis_survey', $this->id);
        $this->data['test']= $this->survey_model->test($this->id);


        $this->layout = 'layout/dashboard';
}
我可以将该数据数组中的所有值传递给我的视图,但“test”除外。我基本上什么都试过了

CheckIfAlreadyPostedSurvey方法将返回 具有num_行的行数,我可以通过写入以下内容在视图中轻松打印其中的值:

<?=$check_if_already_posted_it_survey?>

在我看来,我应该如何打印出“测试”


提前感谢并道歉…

我可能问错了,但你试过了吗

echo $this->survey_model->test($this->id);
试试这个:

public function test($user_id)
{
    $query = $this->db->query("SELECT COUNT(*) AS test FROM test WHERE user_id = '".$user_id."'");

    return $query->row()->test;
}
这将返回一行作为对象,然后返回引用的行名称,在本例中为
test

以下内容也将起作用

public function test($user_id)
{
    $query = $this->db->query("SELECT * FROM test WHERE user_id = '".$user_id."'");

    return count($query->result());
}
您还弄乱了SQL语句中的引号