Get last updated data - Codeigniter

假如想象 提交于 2021-02-04 19:49:27

问题


I am working in codeigniter, i have a below table of schemes, my problem is i want to show last updated data of each scheme. I dont know how to do that, please help.

my_table

scheme_code        updated_on            scheme_name
1                  2015-04-13            One
3                  2015-04-12            Three
4                  2015-04-13            Four
3                  2015-04-10            Three
3                  2015-04-8             Three
1                  2015-04-10            One
4                  2015-04-11            Four

My Model

    function show_last_updated_data(){
        $this->db->select('*');
        $this->db->from('my_table');
        $this->db->order_by('updated_on', 'DESC');

        $query = $this->db->get();

        return $query->result();
    }

Output Needed

scheme_code        updated_on            scheme_name
    1              2015-04-13            One
    3              2015-04-12            Three
    4              2015-04-13            Four

回答1:


My answer is based on @Syed Qarib. I modified it to be compatible with codeigniter active record format.

function show_last_updated_data() {
    $this->db->select('*, str_to_date(updated_on, "%Y-%M-%d") As date', false); // false to skip escape
    $this->db->from('scheme');
    $this->db->group_by('scheme_code');
    $this->db->order_by('date', 'DESC');
    $query = $this->db->get();
    return $query->result();
}

Edit

In another way,

function show_last_updated_data() {
    $max = $this->db->select_max("updated_on")->group_by("scheme_code")->get('scheme')->result_array();
    $updated_on = array();

    if (count($max)) {
        $updated_on = array_column($max, "updated_on"); // make sure your php version is >= 5.5
    }

    $this->db->select("*");
    $this->db->from("scheme");
    $this->db->where_in("updated_on", $updated_on);
    $query = $this->db->get();
    return $query->result();
}

Hope it will be useful for you.




回答2:


Try this:

function show_last_updated_data(){
    $this->db->select('*');
    $this->db->from('my_table');
    $this->db->group_by('scheme_name');
    $this->db->order_by('updated_on', 'DESC');
    $query = $this->db->get();
    return $query->result();
}



回答3:


You need to use group by it will retrieve your record based on scheme_code without repeating it and your desire results. :)

function show_last_updated_data() {

            $query = $this->db->select('*')
                          ->from('my_table')
                          ->group_by('scheme_code')
                          ->order_by('updated_on', 'DESC')
                          ->get();
            return $query->result();

 }



回答4:


As you have used custom date format, so the ordering will not work correctly until you convert the string to date format. Try this:

function show_last_updated_data(){
      $this->db->select('*, str_to_date(updated_on, "%Y-%M-%d") date');
      $this->db->from('my_table');
      $this->db->order_by('date', 'DESC');
      $query = $this->db->get();
      return $query->result();
}

Note: It is recommended to use native date/datetime field, do not use custom formats. You can also go for UNIX timestamp and save it in an int field. As the date can be fetched in any format afterwards and will save you hassle like this one.



来源:https://stackoverflow.com/questions/29620542/get-last-updated-data-codeigniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!