Advertisement
Guest User

serverSide.php

a guest
Jul 4th, 2015
2,421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.05 KB | None | 0 0
  1. <?php
  2.     /**
  3.         * Script:    DataTables server-side script for PHP 5.2+ and MySQL 4.1+
  4.         * Notes:     Based on a script by Allan Jardine that used the old PHP mysql_* functions.
  5.         *            Rewritten to use the newer object oriented mysqli extension.
  6.         * Copyright: 2010 - Allan Jardine (original script)
  7.         *            2012 - Kari Söderholm, aka Haprog (updates)
  8.         * License:   GPL v2 or BSD (3-point)
  9.     */
  10.     mb_internal_encoding('UTF-8');
  11.    
  12.     /**
  13.         * Array of database columns which should be read and sent back to DataTables. Use a space where
  14.         * you want to insert a non-database field (for example a counter or static image)
  15.     */
  16.     $aColumns = array( 'id','userid', 'real_name', 'npm', 'kelas' ); //Kolom Pada Tabel
  17.    
  18.     // Indexed column (used for fast and accurate table cardinality)
  19.     $sIndexColumn = 'id';
  20.    
  21.     // DB table to use
  22.     $sTable = 'user'; // Nama Tabel
  23.    
  24.     // Database connection information
  25.     $gaSql['user']     = 'root';
  26.     $gaSql['password'] = '';  
  27.     $gaSql['db']       = 'datatables_ku';  //Database
  28.     $gaSql['server']   = 'localhost';  
  29.     $gaSql['port']     = 3306; // 3306 is the default MySQL port
  30.    
  31.     // Input method (use $_GET, $_POST or $_REQUEST)
  32.     $input =& $_POST;
  33.  
  34.     $gaSql['charset']  = 'utf8';
  35.    
  36.     /**
  37.         * MySQL connection
  38.     */
  39.     $db = new mysqli($gaSql['server'], $gaSql['user'], $gaSql['password'], $gaSql['db'], $gaSql['port']);
  40.     if (mysqli_connect_error()) {
  41.         die( 'Error connecting to MySQL server (' . mysqli_connect_errno() .') '. mysqli_connect_error() );
  42.     }
  43.    
  44.     if (!$db->set_charset($gaSql['charset'])) {
  45.         die( 'Error loading character set "'.$gaSql['charset'].'": '.$db->error );
  46.     }
  47.    
  48.    
  49.     /**
  50.         * Paging
  51.     */
  52.     $sLimit = "";
  53.     if ( isset( $input['iDisplayStart'] ) && $input['iDisplayLength'] != '-1' ) {
  54.         $sLimit = " LIMIT ".intval( $input['iDisplayStart'] ).", ".intval( $input['iDisplayLength'] );
  55.     }
  56.    
  57.    
  58.     /**
  59.         * Ordering
  60.     */
  61.     $aOrderingRules = array();
  62.     if ( isset( $input['iSortCol_0'] ) ) {
  63.         $iSortingCols = intval( $input['iSortingCols'] );
  64.         for ( $i=0 ; $i<$iSortingCols ; $i++ ) {
  65.             if ( $input[ 'bSortable_'.intval($input['iSortCol_'.$i]) ] == 'true' ) {
  66.                 $aOrderingRules[] =
  67.                 "`".$aColumns[ intval( $input['iSortCol_'.$i] ) ]."` "
  68.                 .($input['sSortDir_'.$i]==='asc' ? 'asc' : 'desc');
  69.             }
  70.         }
  71.     }
  72.    
  73.     if (!empty($aOrderingRules)) {
  74.         $sOrder = " ORDER BY ".implode(", ", $aOrderingRules);
  75.         } else {
  76.         $sOrder = "";
  77.     }
  78.    
  79.    
  80.     /**
  81.         * Filtering
  82.         * NOTE this does not match the built-in DataTables filtering which does it
  83.         * word by word on any field. It's possible to do here, but concerned about efficiency
  84.         * on very large tables, and MySQL's regex functionality is very limited
  85.     */
  86.     $iColumnCount = count($aColumns);
  87.    
  88.     if ( isset($input['sSearch']) && $input['sSearch'] != "" ) {
  89.         $aFilteringRules = array();
  90.         for ( $i=0 ; $i<$iColumnCount ; $i++ ) {
  91.             if ( isset($input['bSearchable_'.$i]) && $input['bSearchable_'.$i] == 'true' ) {
  92.                 $aFilteringRules[] = "`".$aColumns[$i]."` LIKE '%".$db->real_escape_string( $input['sSearch'] )."%'";
  93.             }
  94.         }
  95.         if (!empty($aFilteringRules)) {
  96.             $aFilteringRules = array('('.implode(" OR ", $aFilteringRules).')');
  97.         }
  98.     }
  99.    
  100.     // Individual column filtering
  101.     for ( $i=0 ; $i<$iColumnCount ; $i++ ) {
  102.         if ( isset($input['bSearchable_'.$i]) && $input['bSearchable_'.$i] == 'true' && $input['sSearch_'.$i] != '' ) {
  103.             $aFilteringRules[] = "`".$aColumns[$i]."` LIKE '%".$db->real_escape_string($input['sSearch_'.$i])."%'";
  104.         }
  105.     }
  106.    
  107.     if (!empty($aFilteringRules)) {
  108.         $sWhere = " WHERE ".implode(" AND ", $aFilteringRules);
  109.         } else {
  110.         $sWhere = "";
  111.     }
  112.    
  113.    
  114.     /**
  115.         * SQL queries
  116.         * Get data to display
  117.     */
  118.     $aQueryColumns = array();
  119.     foreach ($aColumns as $col) {
  120.         if ($col != ' ') {
  121.             $aQueryColumns[] = $col;
  122.         }
  123.     }
  124.    
  125.     $sQuery = "
  126.    SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", $aQueryColumns)."`
  127.    FROM `".$sTable."`".$sWhere.$sOrder.$sLimit;
  128.    
  129.     $rResult = $db->query( $sQuery ) or die($db->error);
  130.    
  131.     // Data set length after filtering
  132.     $sQuery = "SELECT FOUND_ROWS()";
  133.     $rResultFilterTotal = $db->query( $sQuery ) or die($db->error);
  134.     list($iFilteredTotal) = $rResultFilterTotal->fetch_row();
  135.    
  136.     // Total data set length
  137.     $sQuery = "SELECT COUNT(`".$sIndexColumn."`) FROM `".$sTable."`";
  138.     $rResultTotal = $db->query( $sQuery ) or die($db->error);
  139.     list($iTotal) = $rResultTotal->fetch_row();
  140.    
  141.    
  142.     /**
  143.         * Output
  144.     */
  145.     $output = array(
  146.     "sEcho"                => intval($input['sEcho']),
  147.     "iTotalRecords"        => $iTotal,
  148.     "iTotalDisplayRecords" => $iFilteredTotal,
  149.     "aaData"               => array(),
  150.     );
  151.    
  152.     // Looping Data
  153.     while ( $aRow = $rResult->fetch_assoc() ) {
  154.         $row = array();
  155.         $btn = '<a href="#" onClick="showModals(\''.$aRow['id'].'\')">Edit</a> | <a href="#" onClick="deleteUser(\''.$aRow['id'].'\')">delete</a>';
  156.         for ( $i=0 ; $i<$iColumnCount ; $i++ ) {
  157.             $row[] = $aRow[ $aColumns[$i] ];
  158.         }
  159.         $row = array( $btn, $aRow['userid'], $aRow['real_name'], $aRow['npm'], $aRow['kelas'] );
  160.         $output['aaData'][] = $row;
  161.     }
  162.    
  163.     echo json_encode( $output );
  164.    
  165. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement