我正在尝试显示我的数据库的一些推荐,但是在尝试不同的代码组合 2 天后,我不得不放弃并希望这里有人比我更了解:)
这是我到目前为止所拥有的:
型:
function getTestimonials() {
$this->db->select('*');
$this->db->from('feedback_comments');
$c = $this->db->get();
return $c;
}
控制器:
$testimonials = $this->feedback_model->getTestimonials();
$this->outputData['testimonials'] = $testimonials;
最后是观点:
<?php $i = 1; foreach($testimonials as $testimonial) { ?>
<?php echo $testimonial->dated; ?>, <?php echo $testimonial->comment; ?>
<?php } ?>
由于某种原因,未显示结果。它没有显示任何错误,但它确实显示 7 行,每行有两个逗号,这很奇怪,因为 BD 只有一条记录。
关于我做错了什么的任何线索?
提前致谢:)
可能想看看这个: http://ellislab.com/codeigniter/user-guide/database/results.html
问题很简单,您没有将结果存储到任何东西中。基本上,您需要更改:
return $c;
到:
return $c->result();
为什么需要进行上述更改?基本上,每当您运行 get() 方法时,您都会检索与数据库查询的连接,这允许您检查行数、有关连接类型的细节等。如果要访问结果,只需对连接字符串调用 result() 方法(或result_array,如果您愿意)。
用这个替换你的模型代码。
function getTestimonials() {
$this->db->select('*');
$this->db->from('feedback_comments');
$c = $this->db->result();
return $c;
}
模型方法就是这样:
function getTestimonials() {
return $this->db->get('feedback_comments')->result();
}
但是,您没有显示如何加载视图。这一行:$this->outputData['testimonials'] = $testimonials;
看起来有点不标准(因为你用了$this,但它应该可以工作)。这是加载视图的基本概念:
$outputData['testimonials'] = $testimonials;
$this->load->view('my_view', $outputData);