2012-10-08 26 views
0

get_poll功能在以下Poll模型:我可以确定Codeigniter中query_result数组的键吗?

class Poll_model extends CI_Model 
{ 
    public function get_poll($parameter) { 
     $this->db->select('question.id, question.title, question.question, answer.answer')->from('answer')->join('question', 'answer.question_id = question_id')->where('question.id',$parameter); 
     $query = $this->db->get(); 
     return $query->result_array(); 
} 

因为我使用的加盟得到2表和表questionanswer导致都有列content,所以在result_array,结构是这样的:

Array ([id] => 1 [title] => favourate character [content] => Green) 1 

在这里只有answer content,我觉得question content是重写,因为两者具有相同的列“内容”。表结构如下图所示:

CREATE TABLE `answer` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `question_id` int(11) unsigned NOT NULL, 
    `content` text NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `question_id` (`question_id`), 
    CONSTRAINT `answer_ibfk_1` FOREIGN KEY (`question_id`) REFERENCES `question` (`id`) ON DELETE CASCADE ON UPDATE CASCADE 
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1; 


CREATE TABLE `question` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `title` varchar(128) NOT NULL DEFAULT '', 
    `content` text NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1; 

有什么办法可以解决这个问题吗?

回答

0

只需重命名列->select()这样调用内:现在

$this->db 
    ->select(' 
     question.id id, 
     question.title title, 
     question.content question, 
     answer.content answer') 
    ->from('answer') 
    ->join('question', 'answer.question_id = question_id') 
    ->where('question.id', $parameter); 

结果行应包含questionanswer指数。

相关问题