在同一网页上显示两个表的数据 [英] Displaying data of two tables on the same web page

查看:114
本文介绍了在同一网页上显示两个表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的codeigniter。目前我正在做一个小项目作为练习,我试图在同一网页上显示两个表的数据。
我尝试在codeigniter中使用 $ this-> db-> join();
,但它不适用于我。我想知道是否有其他选项,我可以探索在同一网页上显示两个表数据?



我也发布了我尝试的加入方法 - 也许你可以告诉我我做错了什么?



< model



  $ this-> db-> select('tblanswers。证书。*'); 
$ this-> db-> from('tblanswers');
$ this-> db-> join('credentials','tblanswers.answerid = credentials.cid','left');
$ query = $ this-> db-> get();
return $ query-> result();

这个连接函数的问题我只显示一个表,我使用 print_r($ data ['query']);



编辑


$ b

表结构:



凭证

  + -------------- + ------ + ------------- + ---- --- + ------- + 
| cid(PRIMARY)|名称| second_name |电话|电子邮件|
+ -------------- + ------ + ------------- + ------- + - ------ +

tblanswers

  + ------------------- + -------- +  - ---------- + --------- + --------- + --------- + 
| answerid(PRIMARY)| userid |问题| answerA | answerB | answerC |
+ ------------------- + -------- + ------------ + --- ------ + --------- + --------- +


解决方案

确定,因此首先您的表必须有关系数据执行加入

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

,因此此操作将执行从凭据表中获取数据的查询,其中* answerid字段= cid字段



EG

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

EDIT b
$ b

似乎你没有事件需要使用加入你想要的东西

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

因为它似乎没有两者之间的关系数据,例如你可以得到所有问题

  return $ this-> db-> select('tblanswers。*,questions。 ')
- > join('questions AS q','tblanswers.questionid = q.question_id','LEFT')
- > get('tblanswers')
- > ; result_object()


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->db->join(); in codeigniter but it dose not work for me. I was wondering is there other options that I can explore of displaying two table data on a same web page?

I am also posting my join method that I have tried - maybe you can tell me I did something wrong?

model

  $this->db->select('tblanswers.*,credentials.*');
    $this->db->from('tblanswers');
    $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'left'); 
    $query = $this->db->get();
    return $query->result();

The problem with this join function I have that it only displays one table but not the other. I used the print_r($data['query']); die() to checked this it only returns the tblanswer not both.

EDIT

table structure:

credentials

+--------------+------+-------------+-------+-------+
| cid(PRIMARY) | name | second_name | phone | email |
+--------------+------+-------------+-------+-------+

tblanswers

+-------------------+--------+------------+---------+---------+---------+
| answerid(PRIMARY) | userid | questionid | answerA | answerB | answerC |
+-------------------+--------+------------+---------+---------+---------+

解决方案

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

这篇关于在同一网页上显示两个表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆