Reputation: 125
I'm new to CI and I wanted to know how to retrieve data from different models but the same views.
I have 2 models, one for the breakdown of assessment of fees and the other one is the total price of the breakdown of assessment of fees.
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;
}
My Controllers:
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);
}
My view:
<?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 } ?>
Please help me, how can I retrieve data from two models and displaying it from one view. Thanks in advance!
Upvotes: 1
Views: 1345
Reputation: 2316
Actually you are calling $this->gettotalAssessment();
function after loading view in getAssessment()
you need to do like this in your controller
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);
}
In your view then your can access total Assessment by
$totalAssessment
Upvotes: 1
Reputation: 31
This seems trivial in the ,codeigniter documentation. Although am relatively new to codeigniter my contribution may be handy. Here it goes:
make sure you load the two models (say in your controller class constructor).
Then there are these two lines in your code that are causing the problem
1. $data['query'] = $this->m_login->result_getAssessment($studentid); <<<found in getAssessment() method
and
2. $data['query'] = $this->m_login->result_totalAssessment($studentid); <<<found in gettotalAssessment() method
You have to specify different array keys for these results
since you are building the array somewhat dynamically. see: php manual
Note also that when you add an alement to an array while specifying an existing index, the value at that index becomes overridden by the new value. hope this throws more flesh to you solution search.
Upvotes: 2