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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记