我已经将2个表连接到服务器端的数据表中,但它在视图中显示重复的行,我误解了
型号:
var $table = 'user_provider';
var $column_order = array(null,'u.nm_uprov','u.email','g.nm_gprov'); //set column field database for datatable orderable
var $column_search = array('u.nm_uprov','u.email','g.nm_gprov');
var $order = array('u.id_uprov' => 'desc'); // default order
function __construct(){
parent::__construct();
$this->load->database();
}
private function _get_datatables_query()
{
$this->db->from($this->table);
$this->db->select('*');
$this->db->from('user_provider as u');
$this->db->join('grup_provider as g','u.id_gprov = g.id_gprov');
$i = 0;
foreach ($this->column_search as $item) // loop column
{
if($_POST['search']['value']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
}
else
{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}
if(isset($_POST['order'])) // here order processing
{
$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
控制器:
public function ajax_list()
{
$list = $this->M_user_provider->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $uprov) {
$no++;
$row = array();
$row[] = $no;
$row[] = $uprov->nm_uprov;
$row[] = $uprov->email;
$row[] = $uprov->nm_gprov;
//add html for action
$row[] = '<a class="btn btn-info btn-fill btn-sm" href="javascript:void(0)" title="Edit" onclick="edit_pic('."'". $uprov->id_uprov."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-danger btn-fill btn-sm" href="javascript:void(0)" title="Hapus" onclick="delete_pic('."'".$uprov->id_uprov."'".')"><i class="glyphicon glyphicon-trash"></i> Hapus</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->M_user_provider->count_all(),
"recordsFiltered" => $this->M_user_provider->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
表:
并在表格的底部显示此&#34;显示4个条目中的1到4个(从2个条目中过滤掉)&#34;,我不知道错误导致一切正常很好,除了表格显示重复的行。
答案 0 :(得分:0)
您要添加两次表格:
$this->db->from($this->table);
$this->db->select('*');
$this->db->from('user_provider as u');
尝试删除第一个“from”子句,因为在第二个子句中添加别名