|
楼主 |
发表于 2011-4-14 10:40:24
|
显示全部楼层
直接贴出来得了。
PHP复制代码
<?php if (! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
/**
* DataTables AJAX json 数据驱动
*
* 针对DataTables提供指定格式的json数据返回。
*
* TheSmallCar@Gmail.com
*
* 版权声明: 该份代码完全开放源代码,任何人可以在任何场合和环境下使用、分发、修改。
* 但请保留该头部注释的内容(作者、Email、版权以及其他注释内容)。
*
* ---------------------------------[更新列表]---------------------------------
* 2011-04-11 完成基本结构。
* 2011-04-12 使用CI的db驱动的Active Record 类修改了一部分对数据库的调用。
* 同时支持了简单的表连接
* 2011-04-13 增加 $where='' 参数。
* 2011-04-14 增加 event_json_row_data_serializing 事件,以支持对数据的特殊处理。
*
*
* ---------------------------------[bug列表]---------------------------------
* 1. 2011-04-13 发现 关于delete数据的搜索,存在问题
2011-04-13 关闭 在使用时,通过参数$where来传入 `delete` <> 1 这样的条件。
。
*/
class json {
public $table = null; //表
public $columns = null; //列
public $prime = null; //主键
public $db = null; //数据库类
private $limit = '';
private $order = '';
private $where = '';
//////////////[事件定义]//////////////
/*
* 数据输出
* 可以使用本事件来修改数据库中查询出来的数据。
* 参数 $row
* 返回值可以修改 $row
*
* 示例:
* $this->json->event_json_row_data_serializing = array($this,'row_data_serializing');
* 然后在row_data_serializing中处理$row参数并返回结果即可。
*/
public $event_json_row_data_serializing = null;
//////////////[方法定义]//////////////
public function __construct ($db){
$this->db = $db['db'];
}
public function get ($table, $prime, $columns, $where='', $join='', $join_condition='', $join_type='')
{
$this->table = $this->db->dbprefix.$table;
$this->prime = $prime;
$this->columns = $columns;
$this->build();
$this->db->select(implode(", ", $columns));
$this->db->from($this->table);
if($where != '')
$this->db->where($where);
if($join != null)
{
if($join_condition == '')
throw new Exception ( '表连接缺少连接条件!' );
$this->db->join($join, $join_condition, $join_type);
}
if($this->where != '')
$this->db->where($this->where);
if($this->order != '')
$this->db->order_by($this->order);
if($this->limit != '')
$this->db->limit($this->limit);
$query = $this->db->get();
$data = $query->result_array();
//echo $this->db->last_query();
$count = count($data);
$sQuery = " SELECT COUNT(".$prime.") FROM `$this->table`";
$display = $this->db->query($sQuery);
$display = $display->result_array();
$display = $display[0];
return $this->serialize($data, $count, $display);
}
private function build ()
{
//limit
$this->limit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$this->limit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
mysql_real_escape_string( $_GET['iDisplayLength'] );
}
//order
if ( isset( $_GET['iSortCol_0'] ) )
{
$this->order = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$this->order .= $this->columns[ intval( $_GET['iSortCol_'.$i] ) ]." ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
}
}
$this->order = substr_replace( $this->order, "", -2 );
if ( $this->order == "ORDER BY" )
{
$this->order = "";
}
}
//where
$this->where = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$this->where = "WHERE (";
for ( $i=0 ; $i<count($this->columns) ; $i++ )
{
$this->where .= $this->columns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
$this->where = substr_replace( $this->where, "", -3 );
$this->where .= ')';
}
for ( $i=0 ; $i<count($this->columns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $this->where == "" )
{
$this->where = "WHERE ";
}
else
{
$this->where .= " AND ";
}
$this->where .= $this->columns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
}
private function serialize($data, $count, $display)
{
$output = array(
"sEcho" => isset($_GET['sEcho'])? intval($_GET['sEcho']):0,
"iTotalRecords" => $count,
"iTotalDisplayRecords" => $display,
"aaData" => array()
);
foreach ($data as $row)
{
//event_select_begin事件
if( isset($this->event_json_row_data_serializing) )
{
$row = call_user_func($this->event_json_row_data_serializing, $row);
}
$line = array();
foreach($row as $key=>$col)
{
$line[] = $row[$key];
}
$output['aaData'][] = $line;
}
$this->event_json_row_data_serializing = null;
return json_encode( $output );
}
}
复制代码 |
|