2

我有一张两张桌子,questionanswer

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;
       }
4

1 回答 1

2

你的结构不好,你应该改变它。

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

或者

$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 
}
?>

您应该在查询中注意我已指定Answers为别名,因此您需要使用它$row->Answers来显示计数。

新的编辑

你的模型应该是这样的

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;
    }
}
于 2013-04-14T05:10:31.103 回答