gpt4 book ai didi

php - 在 SQL 循环和输出文件中创建 "blocks"

转载 作者:行者123 更新时间:2023-11-30 00:56:08 24 4
gpt4 key购买 nike

我有一个包含以下列的表格(电子邮件):

日期;地位;帐户;服务器;生日

有 10,000 条记录。
我想将这些记录分成 block (csv 文件),每个服务器最多 50 条记录。

例如:

    server1 : 13 records
server2 : 576 records
server3 : 97 records
server4 : 7 records

etc

列表1

11/12/13; 1; name@server1.com; 01/01
11/12/13; 1; name@server1.com; 01/01
11/12/13; 0; name@server2.com; 01/01
11/12/13; 1; name@server2.com; 01/01
11/12/13; 1; name@server2.com; 01/01
( Max 50 records server2 ... continues with the next server )
11/12/13; 0; name@server3.com; 01/01
11/12/13; 0; name@server3.com; 01/01
...

列表2

( begins at record nº 51 from Server2 )
11/12/13 0; name@server2.com; 01/01
11/12/13 0; name@server2.com; 01/01
( more 50 records from server2 ... and continues with the next server that have more than 50 records too )
11/12/13 0; name@server5.com; 01/01
....

列表3

( begins at record nº 101 from server2 )
11/12/13 0; name@server2.com ; 01 / 01
11/12/13 0; name@server2.com ; 01 / 01
( more 50 records server2 ... and continues with the next server that have more than 100 records too )
11/12/13 0; name@server6.com; 01/01
....

直到记录结束。

我尝试了很多但没有成功( while , foreach , do while ,数组,...)

很抱歉我的新手问题,谢谢。

最佳答案

我找到了一个简单但不优雅的解决方案。
我创建了一个新列“导出”,初始值为 0,并在循环后设置为 1。
但是...我仍在使用“F5 循环”创建下一个列表。

// Columns : email, domain, status_email, name, birthday, source, exported

$db = new db;

// max e-mails for domain inside the same list
DEFINE(MAX_DOMAIN, 50);

// I have three sources - store, e-commence, blog
// and I need to create separate lists
$source = "blog";

$filename = $source . "_" . date('Y') . date('m') . date('d') . "_"
. date('H') . date('i') . date('s') . ".csv";
$fp = fopen($filename, 'a');
$output = "status_email;name;birthday;email\n";
fwrite($fp, $output);

$sql = "SELECT distinct(domain) as domain FROM table WHERE source = $source";
$res = $db->sql_exec($sql);

foreach ($res as $domain) {
$domain = $domain[0];
$domains[$domain] = 0;
}

$sql = "SELECT email, domain, status_email, name, birthday
FROM table
WHERE exported = 0 AND source = $source";
$records = $db->sql_exec($sql);

foreach ($records as $record) {
$email = $record[0];
$domain = $record[1];
$status_email = $record[2];
$name = $record[3];
$birthday = $record[4];

if ($domains[$domain] <= MAX_DOMAIN) {
$domains[$domain]++;
$sql_update = "UPDATE table set exported = 1
WHERE email='$email' and domain='$domain'";
$db->sql_exec($sql_update);
$output = "$status_email;$name;$birthday;$email@$domain\n";
fwrite($fp, $output);
}
}
fclose($fp);

关于php - 在 SQL 循环和输出文件中创建 "blocks",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20535880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com