如何将本机 PHP 代码转换为 CodeIgniter

发布于 2025-01-10 17:31:30 字数 4400 浏览 0 评论 0原文

这就是我所参考的。它是本机 PHP 代码,我想将其转换为 CodeIgniter。

<?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();
?>

这就是我到目前为止所想到的。但它没有更新状态为“0”的列。它只是创造了另一个记录。

在我的控制器中:

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){
        $ssdata = (object) array(
                  'V_ID'=> $v_id,
                  'TIMEOUT'=> $time,
                  'LOGDATE'=> $date,
                  'STATUS'=> 1);   
        //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,$date);
        $row = $signin->row_array();    
        $id = $row['V_ID'];     

        //if timein found  
        if($signin==TRUE){ 
              
            $this->attendance_model->update($ssdata);
            $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;     
    } 
}

在模型中:

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

function update($data){
    return $this->db->set('TIMEOUT',$time)
         ->set('STATUS',1)->where('V_ID',$data['v_id'])
         ->where('LOGDATE',$date)->update('attendance');
}

public function checktimein($v_id,$date){ 
    $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;            
    } 
}   

即使检测到用户有时间,它也只保存新记录。

但它没有更新状态为“0”的列。它只是创造了另一个记录。

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

<?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();
?>

And this is what I come up so far. But it is not updating the column with STATUS '0'. It just creates another record.

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){
        $ssdata = (object) array(
                  'V_ID'=> $v_id,
                  'TIMEOUT'=> $time,
                  'LOGDATE'=> $date,
                  'STATUS'=> 1);   
        //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,$date);
        $row = $signin->row_array();    
        $id = $row['V_ID'];     

        //if timein found  
        if($signin==TRUE){ 
              
            $this->attendance_model->update($ssdata);
            $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;     
    } 
}

And in model:

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

function update($data){
    return $this->db->set('TIMEOUT',$time)
         ->set('STATUS',1)->where('V_ID',$data['v_id'])
         ->where('LOGDATE',$date)->update('attendance');
}

public function checktimein($v_id,$date){ 
    $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;            
    } 
}   

It saves only new records even if it detects that the user has time IN.

But it is not updating the column with STATUS '0'. It just creates another record.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2025-01-17 17:31:30
$this->db->update()

生成更新字符串并根据您的数据运行查询
供应。您可以将数组或对象传递给函数。这是一个
使用数组的示例:

$data = array(
        'title' => $title,
        'name' => $name,
        'date' => $date
);

$this->db->where('id', $id);
$this->db->update('mytable', $data);
$this->db->update()

Generates an update string and runs the query based on the data you
supply. You can pass an array or an object to the function. Here is an
example using an array:

$data = array(
        'title' => $title,
        'name' => $name,
        'date' => $date
);

$this->db->where('id', $id);
$this->db->update('mytable', $data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文