[CODEIGNITER] Menampilkan Data Enum di Codeigniter
Bagaimana cara menampilkan data enum atau set di codeigniter ?
1. source code di model
//menampilkan data dari tipe data enum di table
public function tampilkanenum($table='', $field=''){
$query = "SHOW COLUMNS FROM ".$table." LIKE '$field'";
$row = $this->db->query("SHOW COLUMNS FROM ".$table." LIKE '$field'")->row()->Type;
$regex = "/'(.*?)'/";
preg_match_all( $regex , $row, $enum_array );
$enum_fields = $enum_array[1];
foreach ($enum_fields as $key=>$value)
{
$enums[$value] = $value;
}
return $enums;
}
1. source code di model
//menampilkan data dari tipe data enum di table
public function tampilkanenum($table='', $field=''){
$query = "SHOW COLUMNS FROM ".$table." LIKE '$field'";
$row = $this->db->query("SHOW COLUMNS FROM ".$table." LIKE '$field'")->row()->Type;
$regex = "/'(.*?)'/";
preg_match_all( $regex , $row, $enum_array );
$enum_fields = $enum_array[1];
foreach ($enum_fields as $key=>$value)
{
$enums[$value] = $value;
}
return $enums;
}
public function tampil_artikel(){
$data = array(
'tampilkanenum' => $this->Post_model->tampilkanenum('loker','syarat')
);
$this->load->view('view_artikel',$data);
}
note : loker adalah nama tabel dan syarat adalah nama field
3. source code di view
$array = array_values($tampilkanenum);
foreach ($array as $isienum) {
echo "<li>" .$isienum."</li>";
}
Comments
Post a Comment