Php Codeigniter:如何在迭代第一个表值时从第二个表中获取计数
我有两张桌子,Php Codeigniter:如何在迭代第一个表值时从第二个表中获取计数,php,codeigniter,Php,Codeigniter,我有两张桌子,问题和答案 question ----------- question_id PK Auto_Incr question varchar... votes int answer ------------ answer_id PK Auto_icre question_id FK refrences question content longtext 我对每个问题都有几个答案,我正在迭代我的问题列表,但我不知道如何在迭代视图页面中
问题
和答案
question
-----------
question_id PK Auto_Incr
question varchar...
votes int
answer
------------
answer_id PK Auto_icre
question_id FK refrences question
content longtext
我对每个问题都有几个答案,我正在迭代我的问题列表,但我不知道如何在迭代视图页面中的问题表数据的同时显示每个问题的答案数
表数据结构数据:
question
-----------
question_id question votes
1 what's name? 0
2 where you? 3
answer
----------
answer_id question_id content
4 2 India
5 2 Nepal
6 2 Pakistan
7 1 Mr Osama Binladan
因此,从上面显示的表结构中,我希望输出显示在包含以下数据的视图页面中
------------------------------------------
Question : what's name?
votes : 0
No. of answer: 1
-------------------------------------------
Question : where you?
votes : 3
No. of answer: 3
-------------------------------------------
我有问题,如何显示每个特定问题的答案数(计数答案)
更新型号:
你的结构不好,你应该改变它
question
-----------
id PK Auto_Incr
question varchar...
votes int
answer
------------
answer_id PK Auto_icre
content longtext
question_id FK refrences question table
现在您可以通过这个查询轻松地计算它们
SELECT
q.*,
COUNT(a.answer_id) AS `Answers`
FROM question q
LEFT JOIN answer a ON a.question_id = q.id
GROUP BY q.id
$query = "
SELECT
q.*,
COUNT(a.answer_id) AS `Answers`
FROM question q
LEFT JOIN answer a ON a.question_id = q.id
GROUP BY q.id";
$this->db->query($query);
或
要显示的视图
<?php
foreach ($query3 as $row)
{
?>
<?php echo $row->votes ?>
<?php echo $row->question ?>
<?php echo $row->Answers ?>
<?php
}
?>
请发布一个示例问题及其答案和所需输出。我在这里看到一个错误
$this->db->join('answer','answer.question\u id=question.question\u id')代码>你没有给答案添加别名更改它$this->db->join('answer a','answer.question\u id=question.question\u id')代码>这很好,但是现在我在映射userdetails表时遇到了问题。我尝试了上面的模型代码,但出现了错误严重性:注意,未定义属性$user\u id
。为什么?请参见我的回答中编辑的模型工作正常,但它只返回问题中的一行,而不返回表中的整行问题
我尝试打印'print\r'('Number of rows=“.query->num rows());`但它只返回一个结果。但是在我的表格里有26个结果。。为什么会这样?我贴错了,我的桌子结构不对。现在我更新了,请检查我更新的问题。@Burlswills尽管如此,我已经给出了适合您需要的答案。你检查过了吗?是的,现在我如何在我的视图页面中显示计数值以及问题表字段值(如我的示例输出中所示)?你还忘了提到我的连接$this->db->join('userdetails','userdetails.user_id=question.user_id')代码>对不起!错误(我尝试了第二个):发生了数据库错误错误编号:1054“字段列表”中的未知列“A.answer\u id”选择
q*,将(A.answer\u id)计数为来自(问题
q)的
Answers`加入answer
ONanswer
question\u id
question\u id
按q
分组question\u id
限制5文件名:D:\xampp\htdocs\system\database\DB\u driver.php行号:330`
$select =array(
'q.*',
'COUNT(a.answer_id) AS `Answers`'
);
$this->db->select($select);
$this->db->from('question q');
$this->db->group_by('q.id');
$this->db->join('answer a', 'answer.question_id = question.id');
$query = $this->db->get();
<?php
foreach ($query3 as $row)
{
?>
<?php echo $row->votes ?>
<?php echo $row->question ?>
<?php echo $row->Answers ?>
<?php
}
?>
public function fetch_allquestions($limit, $start)
{
$this->load->database();
$this->db->limit($limit, $start);
$select =array(
'question.*',
'userdetails.*'
'COUNT(answer.answer_id) AS `Answers`',
);
$this->db->select($select);
$this->db->from('question');
$this->db->join('answer', 'answer.question_id = question.question_id');
$this->db->join('userdetails', 'userdetails.user_id = question.user_id');
$this->db->group_by('question.question_id');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->result();// returning result is enough you dont have to loop
}else{
return false;
}
}