我想请你帮忙。我想创建一个 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 号,然后它将在 txt 中搜索具有相同 ID 号的第一个会员。(如果一个 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.
我已经多次尝试创建脚本,但无济于事。我希望有人可以在这里帮助我。谢谢!
更新:我决定在数据库中创建两个表:commissions 和 log,以便分别保存 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 号的所有销售额的总和。如果您对改进代码有任何建议,请告诉我。谢谢!:)