I'm trying to join two tables and show the data with ajax, codeigniter, and active record, but suddenly shows an error:
Column 'userfile' in field list is ambiguous
Table structure:
Album: id_album, judul_album, userfile, userfile_type
Photo: id_photo, id_album, judul_photo, userfile, userfile_type
Controller:
public function ajax_list()
{
$list = $this->Photo_model->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $data_foto)
{
$no++;
$row = array();
$row[] = '<p style="text-align: center">'.$no.'</p>';
$row[] = '<p style="text-align: left">'.$data_foto->judul_foto.'</p>';
$row[] = '<p style="text-align: left">'.$data_foto->id_album.'</p>';
$row[] = '<p style="text-align: center"><img src="'.base_url('assets/images/photo/').$data_foto->userfile.$data_foto->userfile_type.'" width="100px"></p>';
$row[] = "
<p style='text-align: center'>
<a class='btn btn-sm btn-warning' href='".base_url('admin/photo/update/').$data_foto->id_photo."' title='Edit'><i class='fa fa-pencil'></i> EDIT</a>
</p>
<p style='text-align: center'>
<a class='btn btn-sm btn-danger' href='".base_url('admin/photo/delete/').$data_foto->id_photo."', onClick=\"return confirm('Are you sure?');\"><i class='glyphicon glyphicon-trash'></i> DELETE</a>
</p>";
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->Photo_model->count_all(),
"recordsFiltered" => $this->Photo_model->count_filtered(),
"data" => $data
);
//output to json format
echo json_encode($output);
}
Model:
public function get_datatables()
{
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->select('id_foto,judul_foto,foto_seo,ket,userfile as ufile,userfile_type as ufile_type');
$this->db->join('album ', 'album.id_album = foto.id_photo');
$query = $this->db->get();
return $query->result();
}
Any help will be so appreciate.
You need to specify which table you want to get the columns from where the columns have the same name. EG. album.id_album
and photo.id_album
. So you select should look like this
$this->db->select('id_foto,judul_foto,foto_seo,ket,[album|photo].userfile as ufile,[album|photo].userfile_type as ufile_type');
Replacing [album|photo]
with whichever table you need the column to come from. If you need it from both then the below would work
$this->db->select('id_foto,judul_foto,foto_seo,ket,album.userfile as album_ufile,album.userfile_type as album_ufile_type,photo.userfile as photo_ufile,photo.userfile_type as photo_ufile_type');
You should use table_name
.column name
to solve ambiguous problem.
Try this way:
public function get_datatables()
{
$this->_get_datatables_query();
if ($_POST['length'] != -1) {
$this->db->select('id_foto, judul_foto, foto_seo,ket, album.userfile as ufile, album.userfile_type as ufile_type');
}
$this->db->join('album ', 'album.id_album = foto.id_photo');
$query = $this->db->get();
return $query->result();
}
thanks everyone, finally it solved with:
$this->db->select('id_foto, judul_foto, judul_album, foto.uploader as uploaders,foto.userfile as ufile,foto.userfile_type as ufile_type');
$this->db->join('album ', 'album.id_album = foto.id_album');