0
我需要將這個簡單的php代碼轉換爲codeigniter模型和控制器。 獲取數據時內部while循環$ row索引應通過foreach循環($ columns)來實現。將給定的簡單php轉換爲codeigniter控制器和模型
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dt";
$columns[0] = 'id';
$columns[1] = 'first_name';
$columns[2] = 'last_name';
$columns[3] = 'position';
$columns[4] = 'office';
$columns[5] = 'salary';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$where_clause = "";
$where_clause_array = [];
foreach($_REQUEST['columns'] as $key=>$column) {
if($column['search']['value'] != '') {
$where_clause_array[] = $columns[$column['data']]." LIKE "."'".$column['search']['value']."%'";
}
}
if(sizeof($where_clause_array) > 0) {
$where_clause = " WHERE ".implode(" AND ", $where_clause_array);
}
$order_by = " ORDER BY ".$columns[$_REQUEST['order'][0]['column']]." ".$_REQUEST['order'][0]['dir'];
$limit = " LIMIT ".$_REQUEST['start'].", ".$_REQUEST['length'];
$sql = "SELECT * FROM users".$where_clause.$order_by.$limit;
$result = $conn->query($sql);
$sqlTotal = $sql = "SELECT * FROM users".$where_clause.$order_by;
$resultTotal = $conn->query($sqlTotal);
$finalResult = [];
$finalResult['draw'] = $_REQUEST['draw'];
$finalResult['recordsTotal'] = $resultTotal->num_rows;
$finalResult['recordsFiltered'] = $resultTotal->num_rows;
$i=0;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$finalResult['data'][$i][] = $row["id"];
$finalResult['data'][$i][] = $row["first_name"];
$finalResult['data'][$i][] = $row["last_name"];
$finalResult['data'][$i][] = $row["position"];
$finalResult['data'][$i][] = $row["office"];
$finalResult['data'][$i][] = $row["salary"];
$i++;
}
} else {
$finalResult['data'] = [];
}
echo json_encode($finalResult);
$conn->close();?>
我需要將此簡單的php代碼轉換爲codeigniter模型和控制器。 獲取數據時內部while循環$ row索引應通過foreach循環($ columns)來實現。 由於
好這是偉大的@Malik Mudassar的 –
ü可以嘗試烏爾側面驗證碼與數據表的jQuery –
因爲我試過,但沒有工作 –