我需要将这个简单的PHP代码转换为codeigniter模型和控制器。 内部while循环时获取数据$ row索引应该通过foreach循环($ columns)来实现。
<?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)来实现。 谢谢
答案 0 :(得分:1)
<强>步骤1 强>
首先,数据库凭据转到application-&gt; config-&gt; database.php
<强>步骤-2 强>
控制器
class controllerName extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('your_model');
}
public function index(){
$data['columns'][0]='id';
$data['columns'][1]='first_name';
$data['columns'][2]='last_name';
$data['columns'][3]='position';
$data['columns'][4]='salary';
$result=json_encode($this->your_model->getResults($data));
print_r($result);
}
}
<强>步骤-3 强>
模型
class Your_model extends CI_Model {
public function getResults($data)
{
$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'];
}
$result=$this->db->query('SELECT * from users'$where_clause.$order_by.$limit)->result_array();
return $result;
}