2017-05-10 59 views
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)來實現。 由於

回答

1

步驟-1

第一件事第一,DB憑證進到應用程序 - > config-> 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); 
    } 
} 

Step-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; 

     } 
+0

好這是偉大的@Malik Mudassar的 –

+0

ü可以嘗試烏爾側面驗證碼與數據表的jQuery –

+0

因爲我試過,但沒有工作 –

相關問題