forked from zadr50/maxon
/
table_model.php
123 lines (109 loc) · 4.09 KB
/
table_model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
class Table_model extends CI_Model {
private $result_id=0;
private $fields=null;
private $table_name='';
private $field_key='';
private $sql='';
function __construct(){
parent::__construct();
$this->load->helper('mylib_helper');
$this->load->library('table');
$this->load->library('pagination');
}
function load($table_name,$field_key=''){
$this->browse($param);
}
function table_def($table_name=''){
if($table_name!='')$this->table_name=$table_name;
$q = mysql_query('DESCRIBE '.$this->table_name);
while($row = mysql_fetch_array($q)) {
// echo "{$row['Field']} - {$row['Type']}\n";
preg_match('/([a-zA-Z]+)(\(\d+\))?/', $row['Type'], $matches);
$type = (array_key_exists(1, $matches)) ? $matches[1] : NULL;
$length = (array_key_exists(2, $matches)) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL;
$F = new stdClass();
$F->name = $row['Field'];
$F->type = $type;
$F->default = $row['Default'];
$F->max_length = $length;
$F->primary_key = ( $row['Key'] == 'PRI' ? 1 : 0 );
$F->visible=True;
$F->caption=str_replace('_',' ',$F->name);
$retval[] = $F;
}
return $retval;
}
function data($table_name='',$offset=0,$limit=10,$field_key='',$field_val=''){
$sql="select * from ".$table_name;
if($field_key<>"")$sql.=" where ".$field_key." like '".$field_val."%'";
$query=$this->db->query($sql);
$i=0;
foreach($query->result_array() as $row){
$rows[$i++]=$row;
};
$data['total']=$i;
$data['rows']=$rows;
echo json_encode($data);
}
function browsex($param=null)
{
//add header
foreach($this->fields as $fld){
$header[]=$fld->caption;
}
$this->table->set_heading($header);
$this->table->set_empty(' ');
//add row
$offset=$param['offset'];
$limit=$param['limit'];
$offset=$offset*$limit;
if($offset==0)$offset=1;
$this->db->from($this->table_name);
$query=$this->db->limit($limit,$offset);
$query=$this->db->get();
$i=0;
foreach($query->result_array() as $row){
$i=0;
foreach($this->fields as $fld){
$newrow[$i++]=$row[$fld->name];
};
$this->table->add_row($newrow);
};
$tmpl=$this->table_template();
$this->table->set_template($tmpl);
$retval=$this->table->generate();
$config['base_url']=site_url();
$config['total_rows']=$i;
$config['per_page']=10;
$config['uri_segment']=3;
$this->pagination->initialize($config);
$link=$this->pagination->create_links();
//show table
$data['_content']=$retval;
$data['caption']=$this->table_name;
//$data['_top_menu']=$this->load->view('menu',$data, true);
//$data['_header']=$this->load->view('template/standard/header',$data);
$this->load->view('table',$data);
}
function table_template(){
return array (
'table_open' => '<table id="dg"
cellpadding="0" cellspacing="0" border="0" class="display" title="Basic DataGrid"
>',
'heading_row_start' => '<tr>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>',
'table_close' => '</table>'
);
}
}