require_once"../koneksivar.php";
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
$buffer = "";
$separator = ","; //, atau ;
$newline = "\r\n";
$buffer = "id_admin".$separator ."nama_admin".$separator ."telepon".$separator ."email".$separator ."status".$separator ;
$buffer .= $newline;
$sql="select `id_admin`,`nama_admin`,`telepon`,`email`,`status` from `$tbadmin` order by `id_admin` desc";
$jum=getJum($conn,$sql);
if($jum>0){
$arr=getData($conn,$sql);
foreach($arr as $d) {
$value=$d["id_admin"];$buffer .= "\"".$value."\"".$separator;
$value=$d["nama_admin"];$buffer .= "\"".$value."\"".$separator;
$value=$d["telepon"];$buffer .= "\"".$value."\"".$separator;
$value=$d["email"];$buffer .= "\"".$value."\"".$separator;
$value=$d["status"];$buffer .= "\"".$value."\"".$separator;
$buffer .= $newline;
}
}
else{
$buffer .= $newline;
}
header("Content-type: application/vnd.ms-excel");
header("Content-Length: ".strlen($buffer));
header("Content-Disposition: attachment; filename=report.csv");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");
print $buffer;
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
function getJum($conn,$sql){
$rs=$conn->query($sql);
$jum= $rs->num_rows;
$rs->free();
return $jum;
}
function getData($conn,$sql){
$rs=$conn->query($sql);
$rs->data_seek(0);
$arr = $rs->fetch_all(MYSQLI_ASSOC);
$rs->free();
return $arr;
}
?>
HASIL:
id_admin | nama_admin | telepon | status | |
ADM02 | a | a | a | Aktif |
ADM01 | sdada | 0 | asdasda | Aktif |
Tidak ada komentar:
Posting Komentar