Displaying data of two tables on the same web page

后端 未结 1 1942
遇见更好的自我
遇见更好的自我 2020-12-22 06:21

I am new to codeigniter. Currently I am working on a small project as practice and I am trying to display data of two tables on the same web page. I tried to use$this-

相关标签:
1条回答
  • 2020-12-22 06:50

    ok so first of all your table MUST have relational data to perform a join

    return $this->db->select('tblanswers.*,credentials.*')
                          ->join('credentials cred', 'tblanswers.answerid = credentials.cid', 'LEFT')
                          ->get('tblanswers')
                          ->result_object()
    

    so this will perform a query fetching the data from the credentials table where the *answerid field = the cid field

    E.G

      SELECT * FROM tblanswers
      JOIN credentials ON credentials.cid = tblanswers.answerid
    

    EDIT

    seems like you dont event need to use a join for what you want you could simply go

     return $this->db->select('tblanswers.*,credentials.*')
                    ->from('tblanswers, credentials')
                    ->get()
                    ->result_object();
    

    because it doesnt seem like you have any relational data between the two but for example you could get all questions relating to that awnser by going

    return $this->db->select('tblanswers.*,questions.*')
                          ->join('questions AS q', 'tblanswers.questionid = q.question_id', 'LEFT')
                          ->get('tblanswers')
                          ->result_object()
    
    0 讨论(0)
提交回复
热议问题