converting mysqli to to codeigniter syntax -
good day! trying convert mysql syntax codeigniter working ajax head aching solve problem maybe because i'm new in codigniter,.. here code
in model
employee-grid-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.=" employee"; $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: employees"); $totaldata = mysqli_num_rows($query); $totalfiltered = $totaldata; $sql = "select id, employee_name, employee_salary, employee_age "; $sql.=" employee 1=1"; if( !empty($requestdata['search']['value']) ) { $sql.=" , ( employee_name '".$requestdata['search']['value']."%' "; $sql.=" or employee_salary '".$requestdata['search']['value']."%' "; $sql.=" or employee_age '".$requestdata['search']['value']."%' )"; } $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: employees"); $totalfiltered = mysqli_num_rows($query); $sql.=" order ". $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: employees"); $data = array(); while( $row=mysqli_fetch_array($query) ) { // preparing 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 no searching totalfiltered = totaldata "data" => $data // total data array ); echo json_encode($json_data); // send data json format ?>
and here view 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 , default error: function(){ // error handling $(".employee-grid-error").html(""); $("#employee-grid").append('<tbody class="employee- grid-error"><tr><th colspan="3">no data found in 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>
here syntax i'm trying change model area.
// getting total number records without search $this->db->select('id, employee_name, employee_salary, employee_age'); $this->db->from('employee');
i removed database connection syntax , apply config/database.php wont work @ all.
lots of information need here: http://www.codeigniter.com/user_guide/database/index.html
if haven't gone through tutorials, can find them here: http://www.codeigniter.com/user_guide/tutorial/index.html
they're helpful figure out basics.
config/database.php
$db['default'] = array( 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'test', ... );
model
i'll call 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') post data // use $this->input->get('id') 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 need 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); }
from view, url :"http://example.com/your_controller/employee_grid_data",
call controller:
class your_controller extends ci_controller { public function employee_grid_data() { $this->load->model('employee_model'); $data = $this->employee_model->get_employee_grid(); // if expect json data, can echo echo $data; // otherwise, pass variable view } }
hopefully puts on right path...
Comments
Post a Comment