我有一个目标像下面这样
id | subject | achievement | staff_id
1 Target January 1150000 1
2 Target January 1350000 2
3 Target February 20000000 1
4 Target February 23500000 2
5 Target January 1500000 3
我想展示的是在codeigniter中使用sql查询
SELECT * FROM `target` WHERE `staff_id`='$id' ORDER BY 'id' DESC LIMIT 3,1
我试过在codeigniter中使用get where、order by和limit查询,但是屏幕变空了
这是型号代码
$id=get_staff_user_id();
$this->db->get_where('target', array('staff_id'=>$id,));
$this->db->order_by('id','desc');
$this->db->limit(3, 1);
$query=$this->db->get();
return $query;
这是控制器代码
$data['target'] = $this->target_model->getAllTarget()->result();
这是查看代码
<?php foreach($targets as $target){
if($total >= floatval($target->achievement)) {
$percent = 100;
} else {
if ($total !== 0) {
$percent = number_format(($total * 100) / $target->achievement, 2);
}
}
echo $percent;
?>
我的代码哪里出错了?
谢谢你
最佳答案
问题是-get_where()
已经返回一个DB Result实例-为了解决您的问题,您必须执行如下操作
return $this->db
->select('*')
->from('target')
->where('staff_id', get_staff_user_id())
->order_by('id', 'desc')
->limit(3,1)
->get();