猿问

Mysql查询顺序在codeigniter中按desc限制

我有一个像下面这样的表目标


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

我想展示的是在使用 sql 查询的 codeigniter 中


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;

 ?>

我的代码中的错误在哪里?


白衣非少年
浏览 158回答 2
2回答

吃鸡游戏

问题是 -get_where()已经返回一个数据库结果实例 - 为了解决您的问题,您必须执行类似的操作return $this->db&nbsp; &nbsp; ->select('*')&nbsp; &nbsp; ->from('target')&nbsp; &nbsp; ->where('staff_id', get_staff_user_id())&nbsp; &nbsp; ->order_by('id', 'desc')&nbsp; &nbsp; ->limit(3,1)&nbsp; &nbsp; ->get();

莫回无

在模型中:删除多余的逗号&nbsp; &nbsp; $id=get_staff_user_id();&nbsp; &nbsp; $this->db->get_where('target', array('staff_id'=>$id));&nbsp; &nbsp; $this->db->order_by('id','desc');&nbsp; &nbsp; $this->db->limit(3, 1);&nbsp;&nbsp;&nbsp; &nbsp; $query=$this->db->get();&nbsp; &nbsp; return $query;在控制器中:将目标更改为目标,就像在 foreach 中一样,它从相同的数组名称中获取;$data['targets'] = $this->target_model->getAllTarget()->result();
随时随地看视频慕课网APP
我要回答