结果:
使用php框架flight。
if(isset($_REQUEST['export'])){
$order = ['user_id' => 'DESC'];
$list_all = $db->select(
'nbs_user',
[
'nbs_user.user_id',
'nbs_user.nickname',
'nbs_user.realname',
'nbs_user.phone',
'nbs_user.inviter_user_id',
'nbs_user.inviter_agent_id',
'nbs_user.regional_ownership_agent_id',
'nbs_user.is_tester',
'nbs_user.enterprise_id',
],
[
'AND' => $and,
'LIMIT' => [$export_page, 10000],
'ORDER' => $order
]
);
$inviter_user_ids = array_column($list_all, 'inviter_user_id');
$inviter = $db->select('nbs_user', [
'user_id',
'nickname',
'phone',
'status'
],['user_id' => $inviter_user_ids ]);
$inviter = Arrays::from($inviter)->group('user_id')->obtain();
$user_ids = array_column($list_all,'user_id');
$inviter_agent_ids = array_column($list_all, 'inviter_agent_id');
$inviter_agent = $db->select('nbs_agent', [
'agent_id',
'agent_name',
'phone',
'status'
],['agent_id' => $inviter_agent_ids ]);
$inviter_agent = Arrays::from($inviter_agent)->group('agent_id')->obtain();
$regional_ownership_agent_ids = array_column($list_all, 'regional_ownership_agent_id');
$regional_ownership_agent = $db->select('nbs_agent', [
'agent_id',
'agent_name',
'phone',
'status'
],['agent_id' => $regional_ownership_agent_ids ]);
$regional_ownership_agent = Arrays::from($regional_ownership_agent)->group('agent_id')->obtain();
$enterprise_ids = array_column($list_all, 'enterprise_id');
$enterprise = $db->select('nbs_enterprise', [
'enterprise_id',
'enterprise_name',
],['enterprise_id' => $enterprise_ids ]);
$enterprise = Arrays::from($enterprise)->group('enterprise_id')->obtain();
foreach($list_all as $k => $v){
$list_all[$k]['inviter_user_name'] = isset($inviter[$v['inviter_user_id']][0]['nickname']) ? $inviter[$v['inviter_user_id']][0]['nickname'] : '';
$list_all[$k]['inviter_user_phone'] = isset($inviter[$v['inviter_user_id']][0]['phone']) ? $inviter[$v['inviter_user_id']][0]['phone'] : '';
$list_all[$k]['inviter_agent_user_name'] = isset($inviter_agent[$v['inviter_agent_id']][0]['agent_name']) ? $inviter_agent[$v['inviter_agent_id']][0]['agent_name'] : '';
$list_all[$k]['inviter_agent_user_phone'] = isset($inviter_agent[$v['inviter_agent_id']][0]['phone']) ? $inviter_agent[$v['inviter_agent_id']][0]['phone'] : '';
$list_all[$k]['regional_ownership_agent_name'] = isset($regional_ownership_agent[$v['regional_ownership_agent_id']][0]['agent_name']) ? $regional_ownership_agent[$v['regional_ownership_agent_id']][0]['agent_name'] : '';
$list_all[$k]['regional_ownership_agent_phone'] = isset($regional_ownership_agent[$v['regional_ownership_agent_id']][0]['phone']) ? $regional_ownership_agent[$v['regional_ownership_agent_id']][0]['phone'] : '';
$list_all[$k]['enterprise_name'] = isset($enterprise[$v['enterprise_id']]) ? $enterprise[$v['enterprise_id']][0]['enterprise_name'] : '';
$list_all[$k]['is_tester'] = !empty($v['is_tester']) ? '测试人员' : '用户';
}
$filename = '用户-'.($export_page).'-'.($export_page+10000).'.csv';
$titles = [];
$cols = [];
$titles = ['UID', '昵称', '真实姓名', '企业名称','手机号','测试','用户推荐人','代理商推荐', '归属区域代理商'];
$cols = ['user_id','nickname', 'realname', 'enterprise_name', 'phone', 'is_tester', 'inviter_user_name','inviter_agent_user_name','regional_ownership_agent_name'];
$rows = $list_all;
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.$filename.';');
header('Content-Transfer-Encoding: binary');
$fp = fopen('php://output', 'w');
fputs($fp, $bom =(chr(0xEF).chr(0xBB).chr(0xBF)));
fputcsv($fp, $titles);
$cnt = 0;
foreach($rows as &$row) {
$cnt++;
$item = [];
foreach($cols as $col) {
$item[] = $row[$col];
}
fputcsv($fp, $item);
if($cnt % 100 == 0) {
ob_flush();
flush();
}
}
fclose($fp);
unset($row);
unset($list_all);
die();
}