DataTable从行中获取已修改的数据

时间:2016-12-06 08:42:49

标签: php jquery mysql ajax datatables

我在数据表中从一行打印正确的内容时遇到问题。

我有一个名为 response.php 的文件,其中包含以下内容:

Text: fsdfgsfgf
it is number is: 1
Text: b3cbc3b45
it is number is: 2
Text: 456346aaa
it is number is: 3
Text: bce45fa45
it is number is: 4

现在在我的 index.php 中 我这样称呼这个表:

//include connection file
session_start();
include_once("connection.php");

// initilize all variable
$params = $columns = $totalRecords = $data = array();
$params = $_REQUEST;

//define index of column
$columns = array( 
    0 =>'log_in_timestamp',
    1 =>'liDateInserted',
    2 => 'browser',
    3 => 'location',
    4 => 'lo_li_time'
);

$where = $sqlTot = $sqlRec = "";

// check search value exist
if( !empty($params['search']['value']) ) {   
    $where .=" WHERE ";
    $where .=" ( browser LIKE '".$params['search']['value']."%' ";
    $where .=" OR location LIKE '".$params['search']['value']."%' ";
    $where .=" AND ut.id = '".$_SESSION["userSession"]."%' ";
}

// getting total number records without any search
$sql = "SELECT li.log_in_timestamp, li.date_inserted liDateInserted, li.browser, li.location, (lo.log_out_timestamp - li.log_in_timestamp) lo_li_time, li.user_who_used_the_session, li.id historyId,
        lo.id, lo.user_who_used_the_session, lo.log_out_timestamp, lo.date_inserted, ut.id, ut.username
        FROM log_in_user_sessions li 
        LEFT JOIN log_out_user_sessions lo ON li.unique_id_login = lo.unique_id_logout 
        LEFT JOIN user_table ut ON li.user_who_used_the_session = ut.username
        WHERE ut.id = '".$_SESSION["userSession"]."'";
$sqlTot .= $sql;
$sqlRec .= $sql;

//concatenate search sql if value exist
if(isset($where) && $where != '') {

    $sqlTot .= $where;
    $sqlRec .= $where;
}


$sqlRec .=  " ORDER BY ". $columns[$params['order'][0]['column']]."   ".$params['order'][0]['dir']."  LIMIT ".$params['start']." ,".$params['length']." ";

$queryTot = mysqli_query($conn, $sqlTot) or die("database error:". mysqli_error($conn));


$totalRecords = mysqli_num_rows($queryTot);

$queryRecords = mysqli_query($conn, $sqlRec) or die("error to fetch employees data");

//iterate on results row and create new index array of data
while( $row = mysqli_fetch_row($queryRecords) ) {
    $data[] = $row;
}   

$json_data = array(
        "draw"            => intval( $params['draw'] ),   
        "recordsTotal"    => intval( $totalRecords ),  
        "recordsFiltered" => intval($totalRecords),
        "data"            => $data   // total data array
        );

echo json_encode($json_data);  // send data as json format

我已经尝试了这样的<div class="container"> <div class=""> <h3>User Sessions</h3><br> <div class=""> <table id="employee_grid" class="display" width="100%" cellspacing="0"> <thead> <tr> <th>Time</th> <th>Date</th> <th>Browser</th> <th>Location</th> <th>Duration</th> </tr> </thead> <tfoot> <tr> <th>Time</th> <th>Date</th> <th>Browser</th> <th>Location</th> <th>Duration</th> </tr> </tfoot> </table> </div> </div> </div>

js

我的输出如下:current output

我在<script type="text/javascript"> $( document ).ready(function() { $('#employee_grid').DataTable({ "bProcessing": true, "serverSide": true, "columnDefs": [ { "width": "20%", "defaultContent": "Not Logged Out","targets": "_all" } ], "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { // Bold the grade for all 'A' grade browsers if (!aData[4]) { $(nRow).addClass( 'alert-danger' ).css('background-color', '#f2dede'); } }, "lengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]], "responsive": true, "ajax":{ url :"../test/dt/dt_i/response.php", type: "post", error: function() { $("#employee_grid_processing").css("display", "none"); } } }); }); </script> 为我自己创造了同样的东西,它看起来像这样。我希望DataTable看起来像这样: look i want

我试图从hereherehere获取我的知识

我现在的问题是,如何在第二张图片中输出正确的格式化数据,例如php和正确的css背景

编辑:

我将我的sql语句改为:

unixtimestamp

现在一切都格式正确,但是css-background仍然存在问题...... current

有人可以在这里纠正这一行吗?:

SELECT FROM_UNIXTIME(li.log_in_timestamp, '%H:%i'), DATE(li.date_inserted) liDateInserted, li.browser, li.location, FROM_UNIXTIME((lo.log_out_timestamp - li.log_in_timestamp), '%H:%i:%sh') lo_li_time, li.user_who_used_the_session, li.id historyId,
    lo.id, lo.user_who_used_the_session, lo.log_out_timestamp, lo.date_inserted, ut.id, ut.username
    FROM log_in_user_sessions li 
    LEFT JOIN log_out_user_sessions lo ON li.unique_id_login = lo.unique_id_logout 
    LEFT JOIN user_table ut ON li.user_who_used_the_session = ut.username
    WHERE ut.id = '" . $_SESSION["userSession"] . "'

1 个答案:

答案 0 :(得分:1)

您可以从时间戳中获取时间,例如:

SELECT FROM_UNIXTIME(1447430881);
-> '2015-11-13 10:08:01'

将FROM_UNIXTIME放在时间戳列上并获取时间。

要从datetime获取日期,请使用EXTRACT,如:

SELECT DATE(datetime) from table;

将这两个放在查询中检查结果。