如何计算txt文件中的特定数据/变量,同时使用PHP将其与另一个txt或csv进行比较?

时间:2013-06-11 16:18:44

标签: php parsing text csv count

我想请求你的帮助。我想创建一个PHP脚本,它可以计算每个联盟会员的销售数量和销售总额。涉及两个文件(csv和txt)。以下是CSV和TXT的示例数据:

//Sales.csv
Customer Id,Name,Surname,ID Number,Type,Member Type,Amount,Lead Supplier,Type,Outcome
2759,Hlompho,Molakalaka,8305066090080,Debit Order,Subscriber,69,Company,New Sale,Sucessful
6884,Ndanganen,Tshidavhu,7902065808081,Debit Order,Subscriber,69,Company,New Sale,Sucessful
11583,Shoaib,Solomon,8308015072084,Debit Order,Subscriber,59,Company,Revenue,Sucessful
15507,Rathanambal,Govender,7312190242083,Debit Order,Subscriber,69,Company,Revenue,Sucessful
17456,Dumisani,Tshabalala,8406275675081,Debit Order,Subscriber,59,Company,Revenue,Sucessful
17640,Nomadlozi,Magagula,9201110277089,Debit Order,Subscriber,59,Company,Revenue,Sucessful
21545,Jason,Goosen,9003105124080,Debit Order,Subscriber,59,Company,Revenue,Sucessful
22001,Jacques,Kok,8503025107082,Debit Order,Subscriber,59,Company,Revenue,Sucessful
25137,Natelly,Erasmus,8803210085089,Debit Order,Subscriber,59,Company,Revenue,Sucessful
25431,Mfanufikile,Nala,8211085667082,Debit Order,Subscriber,59,Company,Revenue,Sucessful

//Report.txt
Date,Affiliate ID,Unique ID,Firstname,Lastname,ID Number,Phone,Email,Status,Reference Number
2013-05-01 0:05,1017,u104101722ne3001hj,Shoaib,Solomon,8308015072084,083-651-9877,mxoalarm.alarm@gmail.com,success=true;,146213
2013-05-01 1:58,1009,kf04100923fr3053vp,Hlompho,Molakalaka,8305066090080,0781257255,nkoanat@yahoo.com,success=true;,0
2013-05-01 1:58,1018,dv04101823c730573u,nasiera,hardy,8810160145086,082-346-6104,nasierahardy@yahoo.com,success=true;,146216
2013-05-01 2:00,1013,v04101323ks3057gp,Hlompho,Molakalaka,8305066090080,0763283541,pimpmybodytattoos@gmail.com,success=true;,146217
2013-05-01 2:01,1018,n504101823mh3059w2,zaid,slamdien,8409245240085,071-232-7059,zaid.slamdien@yahoo.com,success=true;,146218
2013-05-01 2:17,1013,pr0510130qy0116j4,zaid,slamdien,8409245240085,071-232-7059,zaid.slamdien@yahoo.com,success=true;,0
2013-05-01 2:18,1013,gd0510130pj0117rm,Dumisani,Tshabalala,8406275675081,082-346-6104,nasierahardy@yahoo.com,success=true;,0
2013-05-01 3:11,1013,oh05101312x0104gf,Lucky Magnificent,Mabena,8104165398084,072-758-4097,lucky.mabena@sasol.com,success=true;,0
2013-05-01 3:34,1001,8c0510011180132t,Nomadlozi,Magagula,9201110277089,084-028-7128,chaddo913@gmail.com,success=true

基本上,脚本的作用是在csv文件的每一行中获取ID号,然后它将使用相同的ID号在txt中搜索第一个联盟。 (如果ID号在文件中多次出现,它将仅计入/记入第一个具有该ID号的联盟会员)然后它将计算联盟会员的销售数量和销售总额。联盟会员将根据其会员ID确定。如果ID号与txt中的任何联盟会员不匹配,则1将用作联盟会员ID。这是示例输出:

/* AffiliateID,NumberOfSales,TotalAmount */
1001,24,678
1009,72,328
1,3,144 //Sales with no match in the txt uses 1 as aff ID.

我已经多次尝试创建脚本,但无济于事。我希望有人可以帮助我。谢谢!

更新:我决定在数据库中创建两个表:佣金和登录,以便分别保存csv和txt数据。从这里开始,我执行了MySQL查询以获得我想要的输出。这是代码:

<?php

//Convert Spreadsheet (XLSX) to CSV
    require_once '../classes/PHPExcel/IOFactory.php';

    //Set maximum execution time to 1 hour.
    ini_set('max_execution_time', 3600);

    $excel = PHPExcel_IOFactory::load("Kudough Commision Report.xlsx");
    $writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
    $writer->setDelimiter(",");
    $writer->setEnclosure("");
    $writer->setSheetIndex(1);
    $writer->save("commissions".date('Ym').".csv");

//Establish a MySQL Database Connection.
$con = mysqli_connect("localhost","root","","kudough");

    if (mysqli_connect_errno($con)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

//Save the CSV to the database.
$file1 = "commissions".date('Ym').".csv"; //CSV filename

$handle1 = @fopen($file1, "r");
    if ($handle1) {
    while (($buffer1 = fgets($handle1, 4096)) !== false) {

        //Get Variables.
        $var1 = explode(",", $buffer1);
        $cid = @$var1[0];
        $fname = @str_replace("'","",$var1[1]);
        $lname = @str_replace("'","",$var1[2]);
        $idnumber = @$var1[3];
        $type = @$var1[4];
        $membertype = @$var1[5];
        $amount = @$var1[6];
        $leadsupplier = @$var1[7];
        $saletype = @$var1[8];
        $outcome = @$var1[9];

        $sql = "INSERT IGNORE INTO commissions (customer_id, name, surname, idnumber, type, member_type, amount, leadsupplier, saletype, outcome) VALUES ('$cid', '$fname', '$lname', '$idnumber', '$type', '$membertype', '$amount', '$leadsupplier', '$saletype', '$outcome')";

        if (!mysqli_query($con,$sql)) {
            die('Error: ' . mysqli_error($con));
        }
    }
    if (!feof($handle1)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle1);
    }

//Save the TXT to the database.
$file2 = "kudough201305.txt"; //TXT filename

$handle2 = @fopen($file2, "r");
    if ($handle2) {
    while (($buffer2 = fgets($handle2, 4096)) !== false) {

        //Get Variables.
        $var2 = explode(",", $buffer2);
        $logdate = @$var2[0];
        $aff_id = @$var2[1];
        $unique_id = @$var2[2];
        $fname = @$var2[3];
        $lname = @$var2[4];
        $idnumber = @$var2[5];
        $phone = @$var2[6];
        $email = @$var2[7];
        $trax = @$var2[8];
        $ref = @$var2[9];

        $sql = "INSERT IGNORE INTO log (logdate, aff_id, unique_id, fname, lname, idnumber, phone, email, trax, ref) VALUES ('$logdate', '$aff_id', '$unique_id', '$fname', '$lname', '$idnumber', '$phone', '$email', '$trax', '$ref')";

        if (!mysqli_query($con,$sql)) {
            die('Error: ' . mysqli_error($con));
        }
    }
    if (!feof($handle2)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle2);
    }

    //Count the number of sales and total amount of sales each affiliate has made.
$sql = "SELECT T2.aff_id AS affID, COUNT(T2.idnumber) AS numofsales, SUM(amount) AS totalsales FROM (SELECT aff_id, T1.idnumber, T1.amount FROM (SELECT * FROM commissions WHERE leadsupplier = 'IMUpstart' GROUP BY commissions.idnumber) AS T1 INNER JOIN log WHERE T1.idnumber = log.idnumber GROUP BY T1.idnumber) AS T2 GROUP BY T2.aff_id";

$rs = mysqli_query($con,$sql);

    while($row = mysqli_fetch_array($rs)) { 

        $affID = $row['affID'];
        $numofsales = $row['numofsales'];
        $totalsales = $row['totalsales'];

        //Write to CSV
        $entrydetails = $affID.", ".$numofsales.", ".$totalsales;
        WriteCSVFile($entrydetails);
    }

mysqli_close($con);
?>

如果它是正确的,我不太确定MySQL语句。我现在的问题是找到在csv中找到但不在txt中找到的那些ID号的所有销售总额。如果您有任何改进代码的建议,请告诉我。谢谢! :)

2 个答案:

答案 0 :(得分:0)

不打算为你编写整个代码,但基本上你必须逐行遍历每个文件并将所有数据拆分成多维数组(array [lineNumber] [columnNumber])。这是

的代码
$file = 'sales.csv'; //make this point to the file
$sales = array();
$file_handle = fopen($file, "r");
while (!feof($file_handle)) {
    $line = rtrim(fgets($file_handle));

}
fclose($file_handle);

这将创建销售中所有数据的$ sales数组。然后对report.txt执行相同的操作。

然后你想在循环中做一个循环来遍历两个数组并检查你需要的所有东西。也许有人会很好,并写出更多的代码

for($i=0;$i<count($sales);$i++){
 for($j=0;$j<count($report);$j++){
  $saleID = $sales[$i][3]; // sales id number of current row
  $reportID = $report[$j][5]; //report id number of current row

  if($salesID == $reportID){
   //sales id and report id match

  }

 }
}
这样的事情。其余的你可以填写

答案 1 :(得分:0)

我想建议一种不同的方法。您可以将CVS文件直接映射到MySQL表(docs)。映射后,您可以在单个查询中获取所有数字。类似的东西:

SELECT  `AffiliateID` , COUNT( * ) 
FROM  `sales` 
GROUP BY  `AffiliateID` 
LIMIT 0 , 3000

整个过程应该比尝试手动迭代文件中的所有记录要少得多。