我可以使这个代码仅使用php native,但我想确切知道如果我真的使用Codeigniter会有什么样的,就像有Controller,Model和View不是吗?你能看看并帮我把代码移到Codeigniter
所以这段代码是关于显示所有员工的名字,旁边有详细按钮,可以显示员工本身的详细信息
的index.php
<?php
$connect = mysqli_connect("localhost", "root", "", "testing_ci");
$query = "SELECT * FROM tbl_employee";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<br /><br />
<div class="container" style="width:700px;">
<h3 align="center"> </h3>
<br />
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="70%">Employee Name</th>
<th width="30%">View</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["name"]; ?></td>
<td><input type="button" name="view" value="view" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_dawta" /></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
<div id="dataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Employee Details</h4>
</div>
<div class="modal-body" id="employee_detail">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$('.view_dawta').click(function(){
var employee_id = $(this).attr("id");
$.ajax({
url:"select.php",
method:"post",
data:{employee_id:employee_id},
success:function(data){
$('#employee_detail').html(data);
$('#dataModal').modal("show");
}
});
});
});
</script>
select.php
<?php
if(isset($_POST["employee_id"]))
{
$output = '';
$connect = mysqli_connect("localhost", "root", "", "testing_ci");
$query = "SELECT * FROM tbl_employee WHERE id = '".$_POST["employee_id"]."'";
$result = mysqli_query($connect, $query);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">';
while($row = mysqli_fetch_array($result))
{
$output .= '
<br>
<tr>
<td width="30%"><label>Name</label></td>
<td width="70%">'.$row["name"].'</td>
</tr>
<tr>
<td width="30%"><label>address</label></td>
<td width="70%">'.$row["address"].'</td>
</tr>
<tr>
<td width="30%"><label>gender</label></td>
<td width="70%">'.$row["gender"].'</td>
</tr>
<tr>
<td width="30%"><label>designation</label></td>
<td width="70%">'.$row["designation"].' Year</td>
</tr>
';
}
$output .= "</table></div>";
echo $output;
}
?>
答案 0 :(得分:0)
注意此代码尚未经过全面测试,并且是基本示例,说明如何执行您想要的操作。
这假设您正在使用名为&#34; users&#34;列名为&#34; id&#34;和&#34;名称&#34;。
您需要在/application/config/database.php。
中设置数据库连接<强>型号:强>
/application/models/Users_model.php
这是所有数据库查询的发生位置:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Users_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
// get a list of all users
public function get_all_users() {
return $this->db->query("
SELECT id, name
FROM users;
")->result();
}
// get the details for one specific user
public function get_user(int $id) {
return $this->db->query("
SELECT *
FROM users
WHERE id = ?;
", $id)->result();
}
}
<强>控制器:强>
/application/controllers/Users.php
这是您的所有请求开始的地方。转到&#39; domain.com / users&#39;访问它(或&#39; localhost / myapp / users&#39;取决于您的设置)。
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model('Users_model');
}
// show a list of all users
public function index() {
// do this by loading a "view" that formats the data we gather here.
$this->load->view('display_users_view', [
'users' => $this->Users_model->get_all_users(),
'title' => 'Title for your HTML could go here.'
]);
}
// return the details for one user
// if $id is not specified, then look for it in a POST variable
public function get_user($id = null) {
// get the employee_id from the ajax request
// this means if $id is set (not null) then make $employee_id = $id. Otherwise, make $employee_id = posted input
$employee_id = ($id) ? $id : (int) $this->input->post('employee_id');
$user = $this->Users_model->get_user($employee_id);
// if the user made an ajax request, send json encoded data
if (! $this->input->is_ajax_request())
echo json_encode($user);
else
return $user;
}
}
查看:强>
/application/views/display_users_view.php
我没有非常清理你的HTML,但我确实添加了脚本来加载jquery和一个模态。
<!DOCTYPE html>
<html>
<head>
<title><?= $title; ?></title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />
</head>
<body>
<br><br>
<div class="container" style="width:700px;">
<h3 align="center"> </h3>
<br>
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="70%">Employee Name</th>
<th width="30%">View</th>
</tr>
<?php foreach($users as $user): ?>
<tr>
<td><?= $user->name; ?></td>
<td><input type="button" name="view" value="view" id="<?= $user->id; ?>" class="btn btn-info btn-xs view_dawta"></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
<div id="dataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Employee Details</h4>
</div>
<div class="modal-body" id="employee_detail">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$('.view_dawta').click(function(){
var employee_id = $(this).attr("id");
$.ajax({
url:"/users/get_user",
method:"post",
data:{employee_id:employee_id},
success:function(data){
$('#employee_detail').html(data);
$('#dataModal').modal("show");
}
});
});
});
</script>
</body>
</html>
请注意,这不是检查是否允许此人访问此信息。如果您想这样做,请将以下内容添加到控制器中的__construct()
:
// if the user isn't logged in, get them out of here
if ($this->session->userdata('access_level') < 10) {
$this->output->set_status_header(403);
return;
}