书接上文,接着把数据读取,修改,删除的部分的部分完成。
各个程序完整代码如下
application/models/User_model.php
<?php
class User_model extends CI_model{
public function create(array $formArray):void{
$this->db->insert('users',$formArray);
}
public function all(){
return $users = $this->db->get('users')->result_array();
}
public function getUser($userId){
$this->db->where('user_id',$userId);
return $this->db->get('users')->row_array();
}
public function updateUser(int $userId, array $formArray){
$this->db->where('user_id',$userId);
$this->db->update('users',$formArray);
}
public function deleteUser($userId){
$this->db->where('user_id',$userId);
$this->db->delete('users');
}
}
?>
application/views/list.php
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>CRUD application -- list</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
<!-- 一个navbar而已 -->
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">CRUD Application</a>
</div>
</div>
<div class="container pt-5" >
<div class="row">
<!-- 分别展示上一轮操作是否成功 -->
<div class="col-md-12">
<?php
$success = $this->session->userdata('success');
if($success != ''){
?>
<div class="alert alert-success">
<?php echo $success; ?>
</div>
<?php
}
?>
<?php
$failure = $this->session->userdata('failure');
if($failure != ''){
?>
<div class="alert alert-success">
<?php echo $failure; ?>
</div>
<?php
}
?>
</div>
<div class="col-md-8">
<div class="row">
<div class="col-6"><h3> View All Users</h3></div>
<div class="col-6 text-right">
<a href="<?php echo base_url().'index.php/user/create';?>" class="btn btn-primary">Create</a>
</div>
</div>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-8">
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php if(! empty($users)){
foreach($users as $user){
?>
<tr>
<td><?php echo $user['user_id'];?></td>
<td><?php echo $user['name'];?></td>
<td><?php echo $user['email'];?></td>
<td>
<a href="<?php echo base_url().'index.php/user/edit/'.$user['user_id'];?>" class="btn btn-primary">Edit</a>
</td>
<td>
<a href="<?php echo base_url().'index.php/user/delete/'.$user['user_id'];?>" class="btn btn-primary">Delete</a>
</td>
</tr>
<?php
}
}else{
?>
<tr>
<td colspan="5">Records not found</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
applicaiton/views/create.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
public function index()
{
$this->load->model('User_model');
$users = $this->User_model->all();
$data = array();
$data['users'] = $users;
$this->load->view('list',$data);
}
public function create(){
$this->load->model('User_model');
$this->form_validation->set_rules('name','Name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('email','Email','required|valid_email');
if($this->form_validation->run() == false){
$this->load->view('create');
}else{
//save user to database;
$formArray = array();
$formArray['name'] = $this->input->post('name');
$formArray['email'] = $this->input->post('email');
$formArray['created_at'] = date('Y-m-d');
$this->User_model->create($formArray);
$this->session->set_flashdata('success','Record Added OK');
redirect(base_url().'index.php/user/index');
}
}
public function edit($userId){
$this->load->model('User_model');
$user = $this->User_model->getUser($userId);
$data = array();
$data['user'] = $user;
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('email','Email','required|valid_email');
if($this->form_validation->run() == false){
$this->load->view('edit',$data);
}else{
//save user to database;
$formArray = array();
$formArray['name'] = $this->input->post('name');
$formArray['email'] = $this->input->post('email');
$this->User_model->updateUser($userId,$formArray);
$this->session->set_flashdata('success','Update user information OK');
redirect(base_url().'index.php/user/index');
}
}
public function delete($userId){
$this->load->model('User_model');
$user = $this->User_model->getUser($userId);
if(empty($user)){
$this->session->set_flashdata('Fail','No user with this id is found');
redirect(base_url().'index.php/user/index');
}
$this->User_model->deleteUser($userId);
$this->session->set_flashdata('Success','User with this id is deleted');
redirect(base_url().'index.php/user/index');
}
}
application/views/edit.php
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>CRUD application -- edit</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">CURD Application</a>
</div>
</div>
<div class="container pt-5">
<h3>Create User</h3>
<hr>
<form class="" name="createUser" action="<?php echo base_url().'index.php/user/edit/'.$user['user_id'];?>" method="post">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="">Name</label>
<input type="text" name="name" value="<?php echo set_value('name',$user['name']);?>" class="form-control">
<?php //echo form_error('name');?>
</div>
<div class="form-group">
<label for="">Email</label>
<input type="text" name="email" value="<?php echo set_value('email',$user['email']);?>" class="form-control">
<?php //echo form_error('email');?>
</div>
<div class="form-group">
<button class="btn btn-primary">Update</button>
<a href="<?php //echo base_url().'index.php/user/index';?>" class="btn btn-secondary">Cancel</a>
</div>
</div>
</div>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
齐活。