dongtuo7364 2016-11-29 14:11
浏览 51
已采纳

模型文件codeigniter中的多个查询

I have a site based on the PHP framework, CodeIgniter. I am facing a problem while fetching data from the database table travels_detail on a page.

The controller method:

function search_travel(){
    $departure= $this->input->post('departure');
    $destination= $this->input->post('destination'); 
    $data['var']= $this->Travel->search_travel($departure, $destination);
    $this->load->view('flight-list',$data);
}

And the model method:

function search_travel($departure, $destination){
    $this->db->select()->from('travels_detail')->where('departure', $departure)->where('destination', $destination);
    $query= $this->db->get();
    return $query->result_array();      
} 

The problem is, I want to execute another query and want to get some data from another table in the same page.

For instance, I'm getting data from travels_detail table of airline: arik and I want to get the image of arik airline from airlines table.

The query should be:

select image from airline where airline = '$airline'

How do I do this in CodeIgniter?

  • 写回答

2条回答 默认 最新

  • doutui839638 2016-11-29 16:09
    关注

    I think you can fetch both information of airlines and its image by simply joining two table.

    The code in codeigniter could be like this:

    function search_travel($departure, $destination){
        return $this->db
                    ->select('travels_detail.airline, airlines.image')
                    ->from('travels_detail')
                    ->join('airlines', 'airlines.airline = travels_detail.airline')
                    ->where('travels_detail.departure', $departure)
                    ->where('travels_detail.destination', $destination)
                    ->get()
                    ->result_array();    
    }
    

    Hope this will solve your problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ssh登录页面的问题
  • ¥60 渗透一个指定银行app,拿到客户信息,需要什么级别
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问
  • ¥15 用MATLAB汇总拟合图