Home > Mobile >  Codeigniter ..What is wrong with this function? It dont detect if already time in
Codeigniter ..What is wrong with this function? It dont detect if already time in

Time:02-25

It always save new record even if there is already time in on that ID. How can I add a timeout if timein is already in the database.\

Please help, thank you... In my controller ...

public function save() {
        $v_id = $this->input->post('v_id');     
        $date = date('Y-m-d');
        $time = date('h:i:s A');
        //check if record exist
        $this->form_validation->set_rules('v_id','Plate Number','trim|callback_get_record');
        //if record not found
        if($this->form_validation->run()==FALSE){
            //show error
            $this->session->set_flashdata('error', 'Cannot find QRCode number '.$v_id);
            redirect('attendance/create');
        }
        else{
            //check if already timein in        
            $signin = $this->attendance_model->checktimein($v_id);
                  //if timein found  
            if($signin==TRUE){ 
                $this->attendance_model->update(array('V_ID'=>$v_id,'TIMEOUT' => $time,'STATUS' => 1));
                $this->session->set_flashdata('success', 'Successfully Time OUT '.$v_id);
                redirect('attendance/create');
            }
            else{
                    $this->attendance_model->save(array('V_ID'=>$v_id,'TIMEIN' => $time,'LOGDATE' => $date,'STATUS' => 0));
                    $this->session->set_flashdata('success', 'Successfully Time IN '.$v_id);
                    redirect('attendance/create');
            }  
            
        }
    }

public function get_record($v_id){ 
        $row = $this->db->where('v_id',$v_id)                   
                    ->get('vehicle_info')
                    ->num_rows(); 
        if($row == 1){  
            return TRUE;
        }else{ 
            return FALSE;           
        } 
    }

It always save new record even if there is already time in on that ID. How can I add a timeout if timein is already in the database.\

Please help, thank you...

While in Model...

public function checktimein($v_id){ 
        $row = $this->db->where('V_ID',$v_id)
                        ->where('LOGDATE',$date)
                        ->where('STATUS',0)
                        ->get('attendance')
                        ->num_rows(); 
        if($row > 0){  
            return TRUE;            
        }else{ 
            return FALSE;            
        } 
    }   

public function save($data){
            $this->db->where('V_ID',$data['v_id']);
            $this->db->insert('attendance',$data);      
    }

    public function update($data){ 
        return  $this->db->where('V_ID',$data['v_id'])
                    ->update('attendance',$data);
    } 

CodePudding user response:

Bro you are missing the $date here

$signin = $this->attendance_model->checktimein($v_id);

you have to pass the date as an argument if in your query you are using $date for LOGDATE

$signin = $this->attendance_model->checktimein($v_id,$date);
public function checktimein($v_id,$date){ 

CodePudding user response:

This is what I am referencing.. It is a native php code and I want to convert it to CI.

<?php
    session_start();
                        $server = "localhost";
                        $username="root";
                        $password="";
                        $dbname="attendancedb";

    $conn = new mysqli($server,$username,$password,$dbname);

    if($conn->connect_error){
        die("Connection failed" .$conn->connect_error);
    }

    if(isset($_POST['studentID'])){
        
        $studentID =$_POST['studentID'];
        $date = date('Y-m-d');
        $time = date('H:i:s A');

        $sql = "SELECT * FROM vehicle_info WHERE v_id = '$studentID'";
        $query = $conn->query($sql);

        if($query->num_rows < 1){
            $_SESSION['error'] = 'Cannot find QRCode number '.$studentID;
        }else{
                $row = $query->fetch_assoc();
                $id = $row['STUDENTID'];
                $sql ="SELECT * FROM attendance WHERE STUDENTID='$id' AND LOGDATE='$date' AND STATUS='0'";
                $query=$conn->query($sql);
                if($query->num_rows>0){
                $sql = "UPDATE attendance SET TIMEOUT='$time', STATUS='1' WHERE STUDENTID='$studentID' AND LOGDATE='$date'";
                $query=$conn->query($sql);
                $_SESSION['success'] = 'Successfuly Time Out: '.$row['FIRSTNAME'].' '.$row['LASTNAME'];
            }else{
                    $sql = "INSERT INTO attendance(STUDENTID,TIMEIN,LOGDATE,STATUS) VALUES('$studentID','$time','$date','0')";
                    if($conn->query($sql) ===TRUE){
                     $_SESSION['success'] = 'Successfuly Time In: '.$row['FIRSTNAME'].' '.$row['LASTNAME'];
             }else{
              $_SESSION['error'] = $conn->error;
           }    
        }
    }

    }else{
        $_SESSION['error'] = 'Please scan your QR Code number';
        }
header("location: index.php");
       
$conn->close();
?>

Please help me.

  • Related