从codeigniter中的两个不同模型中检索数据并在一个视图中显示它

时间:2015-01-29 07:47:16

标签: php codeigniter

我是CI的新手,我想知道如何从不同的模型中检索数据但是相同的视图。

我有2个模型,一个用于评估费用的细分,另一个是费用评估细目的总价格。

    function result_getAssessment($studentid)
            {

                $sql=
                "
                select g.studentid, sb.subjectcode, s.price
                FROM grades g
                INNER JOIN subjectblocking sb ON g.blockcode=sb.blockcode
                INNER JOIN subjects s ON sb.subjectcode=s.subjectcode
                WHERE studentid='$studentid'
                ";

                $result = $this->db->query($sql);
                $result = $result->result();
                return $result; 

            }

function result_totalAssessment($studentid)
        {
            $sql=
            "
            SELECT sum(s.numofunit) total_units, sum(s.price) total_tuition FROM subjects s
            INNER JOIN subjectblocking sb ON s.subjectcode=sb.subjectcode
            INNER JOIN grades g ON sb.blockcode=g.blockcode
            WHERE g.studentid='$studentid'
            ";

            $result = $this->db->query($sql);
            $result = $result->result();
            return $result; 
        }

我的控制器:

 function getAssessment() 
{
   $this->load->view('header');
   $this->load->view('navbar');
   $this->load->view('sidebar');
   $this->load->view('footer');
   $studentid = $this->session->userdata('studentid');
   $data['studentid'] = $studentid;
   $data['query'] = $this->m_login->result_getAssessment($studentid);
   $this->load->view('v_assessment',$data);
   $this->gettotalAssessment();
}

  function gettotalAssessment() 
{
   $studentid = $this->session->userdata('studentid');
   $data['studentid'] = $studentid;
   $data['query'] = $this->m_login->result_totalAssessment($studentid);
   $this->load->view('v_assessment',$data);
}

我的观点:

<?php foreach ($query as $row){ ?>

              <tr>
              <td> <?php echo $row->subjectcode;?> <br></td>
              <td> <?php echo $row->price;?><br></td>
              <td> <?php echo $row->total_tuition;?><br></td>
              </tr>
<?php } ?>  

请帮助我,如何从两个模型中检索数据并从一个视图中显示它。提前谢谢!

2 个答案:

答案 0 :(得分:2)

codeigniter documentation中的这似乎微不足道。虽然我对codeigniter相对较新,但我的贡献可能很方便。在这里: 确保加载两个模型(比如在控制器类构造函数中)。 然后代码中有这两行导致问题

1. $data['query'] = $this->m_login->result_getAssessment($studentid); <<<found in getAssessment() method

2. $data['query'] = $this->m_login->result_totalAssessment($studentid); <<<found in  gettotalAssessment()  method

您必须为这些结果指定不同的数组键 因为你在某种程度上动态地构建数组。见:php manual 另请注意,在指定现有索引时向数组添加alement时,该索引处的值将被新值覆盖。希望这会给你的解决方案搜索带来更多的肉体。

答案 1 :(得分:1)

实际上,在控制器中加载$this->gettotalAssessment();中需要执行此操作的视图后,您正在调用getAssessment()函数

function getAssessment() 
{
   $this->load->view('header');
   $this->load->view('navbar');
   $this->load->view('sidebar');
   $this->load->view('footer');
   $studentid = $this->session->userdata('studentid');
   $data['studentid'] = $studentid;
   $data['query'] = $this->m_login->result_getAssessment($studentid);
   $data['totalAssessment'] = $this->m_login->result_totalAssessment($studentid);
   $this->load->view('v_assessment',$data);

}

在您的视图中,您可以通过

访问总评估
$totalAssessment