【问题标题】:Codeigniter- Check user exists in monthCodeigniter-检查用户是否存在于月份
【发布时间】:2020-01-16 08:25:10
【问题描述】:

我们如何在codeigniter中检查本月(从提交表单获得的月份)的用户退出

eg: 有一种叫薪金计算的表格。每当我们输入员工工资时,需要检查该特定月份的退出,当提交表格时,有日期字段可以选择哪个日期需要给出工资。从表单中我们可以得到用户 ID 和月份。它的代码模型查询是什么

表名salary,数据库日期字段类型为date

请查看我的型号查询码

public function ($userid, $month)
{
    $this->db->where('salary_employee_id',$userid);
    $this->db->where('salary_date',$month);
    $query=$this->db->get('salary');
    $query->row_array();

    if(empty($query))
        {
            return true;
        }
        else{
            return false;
        }

}

【问题讨论】:

    标签: php mysql codeigniter date exists


    【解决方案1】:

    由于您数据库中的列salary_date 是月份类型,因此该列中的数据将存储如下:

    2019-01-12
    2019-02-01
    2019-03-13
    

    我假设一天可以是一个月中的任何一天,而不一定是每个月的第一天。 所以你的Mysql Query应该使用LIKE作为salary_date,应该是这样的

    SELECT * FROM `salary` WHERE `salary_employee_id` = '3' AND `salary_date` LIKE '2019-03%'
    

    如果我们假设用户在您的表单中输入月份数。您可以如下更改您的功能

        public function checkUser($userid, $month)
        {
            //$month contains the month number. If it contains Jan, Feb etc change accordingly.
            $dateObj   = DateTime::createFromFormat('!m', $month);
            $m = $dateObj->format('m'); // gives month number with 0 as prefix.
            $dateFormat = date('Y') . '-' . $m; //Date Format as used by mysql
    
            $this->db->where('salary_employee_id',$userid);
            $this->db->like('salary_date', $dateFormat, 'after'); //Construct Like Condition.
            $query=$this->db->get('salary');
    
            if( $query->num_rows() > 0 ) { //If Result Found, return true.
                return true;
            } else {
                return false;
            }
        }   
    

    请注意,这只会检查当前年份。如果要检查前几年或任何特定年份,则需要传递 year 参数 作为该函数的另一个参数。

    更新

    感谢@Strawberry 的评论,在查询中使用日期范围而不是 LIKE 会更快。所以更新方法如下

        public function checkUser($userid, $month)
        {
    
            //$inputDate = date('Y-') . $month;  //Current Year and given month.
            $dateObj   = DateTime::createFromFormat('Y-m', $month);  //Create Date Object. $month is of format Y-m 
            $startDate = $dateObj->format('Y-m-01'); // First Date of Month
            $endDate = $dateObj->format('Y-m-t'); // Last Date of Month
    
            $this->db->where('salary_employee_id',$userid);
            $this->db->where('salary_date >=', $startDate);
            $this->db->where('salary_date <=', $endDate);       
            $query=$this->db->get('salary');
    
            if( $query->num_rows() > 0 ) {
                return true;
            } else {
                return false;
            }
        }
    

    【讨论】:

    • 我怀疑一个范围可能比 LIKE 更快,例如salary_date &gt;= 2019-03-01 AND salary_date &lt; 2019-04-01
    • @Strawberry 感谢您的建议。你是正确的,范围会更快。我已经相应地更新了答案。
    • 每当使用上述代码时,我都会收到类似“消息:在布尔值上调用成员函数 format()”的错误
    • @VishnuJayaraj 请指定变量 $month 的值。
    • @VishnuJayaraj 我希望 $month 是 1 到 12 之间的数值。如果它是其他值,那么您需要相应地构造日期对象。
    猜你喜欢
    • 1970-01-01
    • 2014-01-20
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 2021-02-11
    • 2018-04-23
    • 1970-01-01
    相关资源
    最近更新 更多