如何将本机 PHP 代码转换为 CodeIgniter
这就是我所参考的。它是本机 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)