[英]Mysql query order by desc limit in codeigniter
I have a table target like below 我有一个如下表的目标
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
What I want to show is in codeigniter using sql query like 我想显示的是在Codeigniter中使用sql查询
SELECT * FROM `target` WHERE `staff_id`='$id' ORDER BY 'id' DESC LIMIT 3,1
I have tried in codeigniter using get where,order by and limit query however the screen is going blank 我已经在Codeigniter中尝试过使用get where,order by和limit查询,但是屏幕空白
Here's the model code 这是模型代码
$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;
Here's the controller code 这是控制器代码
$data['target'] = $this->target_model->getAllTarget()->result();
Here's the view code 这是查看代码
<?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;
?>
Where's the error in my code ? 我的代码中的错误在哪里?
Thank you 谢谢
The thing is - get_where()
returns already a DB Result Instance - in order to resolve your issue you have to do something like 问题是
get_where()
已经返回了数据库结果实例-为了解决您的问题,您必须执行类似的操作
return $this->db
->select('*')
->from('target')
->where('staff_id', get_staff_user_id())
->order_by('id', 'desc')
->limit(3,1)
->get();
In Model: 在模型中:
Remove the extra comma
删除多余的逗号
$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;
In Controller: 在控制器中:
Change target to targets as in foreach it gets from same array name;
将目标更改为目标,就像在foreach中一样,它来自相同的数组名称;
$data['targets'] = $this->target_model->getAllTarget()->result();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.