header("Content-type:text/html;charset=utf-8");
class Readfiledata {
private static function connect(){
require_once 'index2.php';
mysql_connect('localhost','root','');
mysql_select_db('sinapay');
}
private static function getdata($file) {
$handle = fopen ( $file, 'r' );
$orderform = array ();
$i=0;
while ( false != ($data = fgetcsv ($handle,0,',')) ) {
$i++;
if($i==0) continue;
$orderform [] =trim($data [0],"\t\n\r\0\x0B`\'");
}
fclose ( $handle );
return $orderform;
}
private static function getdiffdata($file1, $file2) {
$orderform = self::getdata ( $file1 );
$orderform2 = self::getdata ( $file2 );
$diff1 = array_diff ($orderform,$orderform2 );
$diff2 = array_diff ($orderform2,$orderform );
$todiff = array_merge ( $diff1, $diff2 );
$todif=array_values(array_unique($todiff));
return $todif;
}
private static function writefile($filename, $file1, $file2) {
$todiffdata = self::getdiffdata ( $file1, $file2 );
$newarray=array();
self::connect();
if(empty($todiffdata)){
echo "两个文件的数据一致";
die;
}
$toparray=array("charge_id","bussined_id");
$condition='';
$counarray=count($todiffdata)-1;
foreach($todiffdata as $key=>$val){
if($key==$counarray){
$condition.="charge_id=$val";
}else{
$condition.="charge_id=$val or ";
}
}
$sql="select charge_id,business_id from sinapay_charge_final where $condition";
$result=mysql_query($sql);
while($res=mysql_fetch_array($result)){
$data=array('charge_id'=>$res['charge_id'],'business_id'=>$res['business_id']);
$newarray[]=$data;
}
if (! is_file ( $filename)) {
touch ( $filename);
}
$handle = fopen ($filename, 'a' );
fputcsv($handle, $toparray);
foreach($newarray as $value){
fputcsv ( $handle, $value );
}
fclose ( $handle );
}
public static function main($filename,$file1,$file2) {
self::writefile ( $filename, $file1, $file2 );
}
}
$filename = 'total.csv';
$file1 = 'ac.csv';
$file2 = 'ad.csv';
Readfiledata::main ($filename, $file1, $file2 );