我有一张两张桌子,question
和answer
。
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
-------------------------------------------
我有问题,我如何显示否。每个特定问题的答案(计数答案)?
更新型号:
//-------------------------Get hot question by------------------------------
public function fetch_allquestions($limit, $start) {
$this->load->database();
$this->db->limit($limit, $start);
$select =array(
'q.*',
'COUNT(a.answer_id) AS `Answers`'
);
$this->db->select($select);
$this->db->from('question q');
$this->db->group_by('q.question_id');
$this->db->join('answer a', 'answer.question_id = q.question_id');
$this->db->join('userdetails ', 'userdetails.user_id = question.user_id'); //this is not working fine
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}