问题
I am using Codeigniter,
I have a table and I am inserting the data into the database and data are inserting two times depends on the year and six months.
So my table data is
------------------------------------------------
id |m_id | primary | secondary | Duration
------------------------------------------------
1 | 1 | 100 | 80 | 12m
------------------------------------------------
2 | 1 | 50 | 40 | 6m
------------------------------------------------
3 | 2 | 300 | 150 | 12m
------------------------------------------------
4 | 2 | 150 | 70 | 6m
------------------------------------------------
5 | 3 | 500 | 300 | 12m
------------------------------------------------
6 | 3 | 300 | 200 | 6m
Now I have to fetch data from the database and I want my output looks like this
--------------------------------------------
id |m_id | primary | secondary
| | 12m |6m |12m |6m
--------------------------------------------
1 |1 |100 |50 | 80 |40
--------------------------------------------
2 |2 |300 |150 | 150 |70
--------------------------------------------
3 |3 |500 |300 | 300 |200
So I tried to display the array on the view page so I print_r($row)
and I am getting the output like
stdClass Object ( [id] => 1 [m_id] => 1 [primary] => 100 [secondary] =>80 [Duration] => 12m )
stdClass Object ( [id] => 2 [m_id] => 1 [primary] => 50 [secondary] => 40 [Duration] => 6m)
stdClass Object ( [id] => 3 [m_id] => 2 [primary] => 300 [secondary] => 150 [Duration] => 12m)
stdClass Object ( [id] => 4 [m_id] => 2 [primary] => 150 [secondary] => 70 [Duration] => 6m)
stdClass Object ( [id] => 5 [m_id] => 3 [primary] => 500 [secondary] => 300 [Duration] => 12m)
stdClass Object ( [id] => 6 [m_id] => 3 [primary] => 300 [secondary] => 200 [Duration] => 6m )
but in the list, I am getting output. I haven't added amount because I am getting the wrong amount. This is not an excepted output. There is some issue in my model query.
--------------------------------------------
id |m_id | primary | secondary
| |12m |6m |12m |6m
--------------------------------------------
1 |1 | | | |
2 |1 | | | |
3 |2 | | | |
4 |2 | | | |
5 |3 | | | |
6 |3 | | | |
My view code is
<table id="list" border="1">
<thead>
<tr>
<th rowspan="2">id</th>
<th rowspan="2">m_id</th>
<th colspan="2">Primary</th>
<th colspan="2">Secondary</th>
</tr>
<tr>
<th>12m</th>
<th>6m</th>
<th>12m</th>
<th>6m</th>
</tr>
</thead>
<tbody>
<?php
if($activityfeeslist)
{
$n = 1;
foreach ($activityfeeslist as $rows)
//print_r($rows);
if($rows->Duration=='12m'){
$primary12=$rows->primary;
$secDep12=$rows->secondary;
}else{
$primary6=$rows->primary;
$secDep6=$rows->secondary;
}
{?>
<tr>
<td><?php echo $n++;?></td>
<td><?php echo $rows->m_id;?></td>
<td><?php echo $primary12;?></td>
<td><?php echo $primary6;?></td>
<td><?php echo $secDep12;?></td>
<td><?php echo $secDep6;?></td>
<?php
}}?>
</tbody>
</table>
Controller
public function activityFees()
{
$data['activityfeeslist'] = $this->Fees_model->activityFessList();
$this->load->view('fees/activityFees',$data);
}
Model
public function activityFessList(){
$getDetails = array('tbl_activityFees2.is_feesActive'=>1);
$result = $this->db->where($getDetails)
->select("*")
->from('tbl_activityFees2')
->join('activity_name','activity_name.m_id=tbl_activityFees2.m_id')
->get()
->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
Would you help me out with this issue?
回答1:
Yes, there is an error in your Model query as in your View. In view you are iterating and saperating 12m and 6m, instead of doing it in your view you should use a query something like below:
$getDetails = array('tbl_activityFees2.is_feesActive'=>1);
$result = $this->db->where($getDetails)
->where('Duration', '=', '12m')
->select('m_id', 'primary as primary12m', 'secondary as secondary12m')
->from('tbl_activityFees2')
->join('tbl_activityFees2 as t2','t2.m_id=tbl_activityFees2.m_id')->where('Duration', '=', '6m')->select('primary as primary6m', 'secondary as secondary6m')
->join('activity_name','activity_name.activity_name_id=tbl_activityFees2.activity_name_id')
->get()
->result();
I have joined same table two times to get the result directly from query. Systax me be little bit wrong but I hope you got the idea.
回答2:
if you are getting rows in your loop at view... than just change the view
Your activityFees controller is correct .. if you have loaded model from constructor else
public function activityFees()
{
$this->load->model('Fees_model');
$data['activityfeeslist'] = $this->Fees_model->activityFessList();
$this->load->view('fees/activityFees',$data);
}
at model you dont have to go as per standard .. so write it as and mention join also...
public function activityFessList(){
$getDetails = array('tbl_activityFees2.is_feesActive'=>1);
$result = $this->db->select("*")
->from('tbl_activityFees2')
->where($getDetails)
->join('activity_name','activity_name.activity_name_id=tbl_activityFees2.activity_name_id','left')
->get()
->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
at view
your condition is creating mistake.. . instead of writing condition ... write it as ..
<?php if($activityfeeslist)
{
$n = 1;
foreach ($activityfeeslist as $rows) { ?>
<tr>
<td><?php echo $n++;?></td>
<td><?php echo $rows->m_id;?></td>
<td><?php echo $rows->Duration;?></td>
<td><?php echo $rows->primary;?></td>
<td><?php echo $rows->Duration;?></td>
<td><?php echo $rows->secondary;?></td>
</tr>
<?php } } ?>
来源:https://stackoverflow.com/questions/54050168/not-getting-the-expected-output-from-the-model-query-what-query-should-i-use