美好的一天!我正在尝试将mysql语法转换为使用ajax的codeigniter,但我的头脑很难解决这个问题,可能是因为我是新的codigniter,..这里是我的代码
在我的模特中
雇员并网data.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
$requestData= $_REQUEST;
$columns = array(
0 =>'employee_name',
1 => 'employee_salary',
2=> 'employee_age',
3 => 'id',
);
$sql = "SELECT id, employee_name, employee_salary, employee_age ";
$sql.=" FROM employee";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "SELECT id, employee_name, employee_salary, employee_age ";
$sql.=" FROM employee WHERE 1=1";
if( !empty($requestData['search']['value']) ) {
$sql.=" AND ( employee_name LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR employee_salary LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR employee_age LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query);
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData[] = $row["id"];
$nestedData[] = $row["employee_name"];
$nestedData[] = $row["employee_salary"];
$nestedData[] = $row["employee_age"];
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ),
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
?>
这是我的看法 的index.php
<!DOCTYPE html>
<html>
<title>Datatable</title>
<head>
<link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css">
<script type="text/javascript" language="javascript" src="js/jquery.js"> </script>
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.js"></script>
<link rel="stylesheet" type="text/css" href="assets/css/table.css">
<script type="text/javascript" language="javascript" >
$(document).ready(function() {
var dataTable = $('#employee-grid').DataTable( {
"processing": true,
"serverSide": true,
"ajax":{
url :"employee-grid-data.php", // json datasource
type: "post", // method , by default get
error: function(){ // error handling
$(".employee-grid-error").html("");
$("#employee-grid").append('<tbody class="employee- grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
$("#employee- grid_processing").css("display","none");
}
}
} );
} );
</script>
<style>
div.container {
margin: 0 auto;
max-width:760px;
}
div.header {
margin: 100px auto;
line-height:30px;
max-width:760px;
}
body {
background: #f7f7f7;
color: #333;
font: 90%/1.45em "Helvetica Neue",HelveticaNeue,Verdana,Arial,Helvetica,sans-serif;
}
</style>
</head>
<body>
<div class="header"><h1>DataTable</h1></div>
<div class="container">
<table style="color:black;" class="rwd-table" id="employee-grid" cellpadding="0" cellspacing="0" border="0" class="display" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Employee name</th>
<th>Salary</th>
<th>Age</th>
</tr>
</thead>
</table>
</div>
</body>
</html>
这是我正在尝试更改为模型区域的语法。
// getting total number records without any search
$this->db->select('id, employee_name, employee_salary, employee_age');
$this->db->from('employee');
我删除了数据库连接语法并应用于config / database.php但根本不工作。
答案 0 :(得分:1)
您需要的大量信息:http://www.codeigniter.com/user_guide/database/index.html
如果您还没有完成教程,可以在此处找到它们:http://www.codeigniter.com/user_guide/tutorial/index.html
他们非常有助于弄清楚基础知识。
config/database.php
$db['default'] = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test',
...
);
model
我称之为employee_model
。 models/employee_model.php
class Employee_model extends CI_Model {
public function __construct()
{
$this->load->database(); // or autoload it...
}
public function get_employee_grid()
{
$result = StdClass();
//$requestData= $_REQUEST;
// use $this->input->post('id') for POST Data
// use $this->input->get('id') for GET Data
// etc...
$q = $this->db->select("id, employee_name, employee_salary, employee_age")->get('employee');
$result->count = $q->num_rows(); //get's number of rows
$result->rows = $q->result(); // object of resulting rows
...
// when you need to search... use ->where()
$this->db->$this->db->select("id, employee_name, employee_salary, employee_age")->where('id', $this->input->post('id'))->get('employee');
return json_encode($result);
}
从您的观点来看,url :"http://example.com/your_controller/employee_grid_data",
致电控制器:
class Your_controller extends CI_Controller {
public function employee_grid_data()
{
$this->load->model('employee_model');
$data = $this->employee_model->get_employee_grid();
// if you expect ONLY JSON data, you can echo
echo $data;
// otherwise, you pass the variable to the view
}
}
希望这会让你走上正确的道路......