没有从模型查询中获得预期的输出我应该使用什么查询?

sdnqo3pr  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(263)

我用的是codeigniter,
我有一个表,我将数据插入数据库,数据插入两次取决于一年六个月。
所以我的表数据是

------------------------------------------------
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

现在我必须从数据库中获取数据,我希望我的输出像这样

--------------------------------------------
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

所以我试图在视图页上显示数组,所以 print_r($row) 我得到的结果是

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  )

但是在列表中,我得到了输出。我没有加金额,因为我拿错了金额。这不是例外的输出。我的模型查询中有一些问题。

--------------------------------------------
id  |m_id | primary        | secondary    
    |     |12m  |6m        |12m   |6m 
--------------------------------------------
 1  |1    |     |          |      | 
 2  |1    |     |          |      | 
 3  |2    |     |          |      | 
 4  |2    |     |          |      |
 5  |3    |     |          |      | 
 6  |3    |     |          |      |

我的视图代码是

<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>

控制器

public function activityFees()
  {
        $data['activityfeeslist'] = $this->Fees_model->activityFessList();
        $this->load->view('fees/activityFees',$data);
  }

模型

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;    
        }
        }

你能帮我解决这个问题吗?

aor9mmx1

aor9mmx11#

如果您在视图中得到循环中的行。。。不仅仅是改变观点
你的activityfees控制器是正确的。。如果您从其他构造函数加载了模型

public function activityFees()
  {
        $this->load->model('Fees_model');
        $data['activityfeeslist'] = $this->Fees_model->activityFessList();
        $this->load->view('fees/activityFees',$data);
  }

在模型中,你不必按照标准去做。。所以写下来,并提到加入也。。。

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;    
            }
            }

在视野中
你的情况造成了错误。而不是写条件。。。写为。。

<?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 } } ?>
c8ib6hqw

c8ib6hqw2#

是的,在视图中的模型查询中有错误。在视图中,您正在对12m和6m进行迭代和运算,而不是在视图中进行,您应该使用如下查询:

$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();

为了直接从查询中得到结果,我已经连接了同一个表两次。我可能有点错,但我希望你能理解。

相关问题