zoukankan      html  css  js  c++  java
  • 读取两文件,不同的内容存入还有一个文件里

    <?php
    /**
      * 从两个.csv 文件里读出数据
      * 比較这两个文件不同的数据。并存入.csv 文件里
      */
    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==1) continue;
    			$orderform [] =trim($data [0],"	
    
    x0B`'");
    		}
    		fclose ( $handle );
    		return $orderform;
    	}
    	/**
    	 * 获取两个文件不同的数据
    	 *
    	 * @param String $file1        	
    	 * @param String $file2        	
    	 */
    	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;
    	}
    	/**
    	 * 数据写入.csv 文件里
    	 * @param String $filename        	
    	 * @param String $file1        	
    	 * @param String $file2        	
    	 */
    	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 );

  • 相关阅读:
    JSP简单访问数据库
    解析数据存储MySQL
    学习SSH框架
    JavaWEB中读取配置信息
    Eclipse中将Java项目转换成Web项目的方法
    JavaWEB入门
    万能数据库连接类-Oracle、DB2 、Access 、Sql Server
    小米3 打开开发者选项
    coolpad 5879logcat不能输入日志解决办法
    实用开发之-oracle表回滚到一个指定时间的操作语句
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5145984.html
Copyright © 2011-2022 走看看