1. 程式人生 > 實用技巧 >php 帶條件匯出txt

php 帶條件匯出txt

  

<?php
$wherelist = array();
if (!empty($_GET['status'])) {
    $wherelist[] = " status  =".$_GET['status'];
}
if (!empty($_GET['message'])) {
    $wherelist[] = " phone like '%".$_GET['message']."%'";
}
if (count($wherelist) > 0) {
    $where = " where ".implode('and ', $wherelist);
}
include('../common/mysqlconf.php');
$sql      = "select id,phone,add_time,if(status=0,'未通過','已通過')  status from  user_partake $where";
$result   = mysql_query($sql, $conn);
$totalnum = mysql_num_rows($result);

if (empty($totalnum)) {
    echo "<script language=javascript>alert('沒有要匯出的資料');window.top.location.href='user_phone.php';</script>";
    die;
}

$content = '';
while ($res=mysql_fetch_assoc($result)){

    $content .= 'id:' . $res['id'] . ' -- 電話:' . $res['phone'] . ' -- 狀態:' . $res['status'] . ' -- 時間:' .  $res['add_time']  . "\r\n";
}
header("Content-type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Content-Disposition:attachment;filename=".'手機號列表_'.date("YmdHis").".txt");
header("Expires:0");
header("Cache-Control:must-revalidate,post-check=0,pre-check=0 ");
header("Pragma:public");
echo $content;