gpt4 book ai didi

php - CSV 只导入一定数量然后超过数据进入另一个表

转载 作者:行者123 更新时间:2023-11-29 03:22:08 24 4
gpt4 key购买 nike

我这里有个问题。我不知道如何给出合适的问题标题。所以这是我的问题。

我有一组 csv 数据。假设有 30 个数据。条件是我只希望 20 条数据进入表 A,另外 10 条数据进入表 B。

您有任何关于如何使用此逻辑的示例吗?可以通过结合while循环和if语句来完成吗?顺便说一下,它是一个 PHP,我还是一个新手程序员。希望有人能帮助我。谢谢。

编辑:我当前的代码

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
{
if($leftPax > 0)
{
$registrantBean = new z_EventRegistrants();
$registrantBean->last_name = $data[1];
$registrantBean->id_no = $data[0];
$registrantBean->phone_mobile = $data[2];
$registrantBean->email1 = $data[3];
$registrantBean->gender = $data[4];
$registrantBean->date_of_birth = $data[5];
$registrantBean->age = $data[6];
$registrantBean->race = $data[7];
$registrantBean->z_eventregec5erations_ida = $registrationBean->id;
$registrantBean->save();
$count++;
error_log(print_r($_REQUEST['session'], 1));
foreach($_REQUEST['session'] as $aid=>$sid)
{
$attendanceBean = new z_EventAttendances();
$attendanceBean->name = $registrantBean->last_name;
$attendanceBean->z_eventsessions_z_eventattendancesz_eventsessions_ida = $sid;
$attendanceBean->z_eventactivities_z_eventattendances_1z_eventactivities_ida = $aid;
$attendanceBean->z_eventregistrations_z_eventattendancesz_eventregistrations_ida = $registrationBean->id;
$attendanceBean->z_eventregistrants_z_eventattendancesz_eventregistrants_ida = $registrantBean->id;
$attendanceBean->save();
}
}
else
{
$waitlistBean = new z_EventWaitlists();
$waitlistBean->name = $data[1];
$waitlistBean->save();
}
}

我想要的是,20 个数据将转到 z_EventRegistrants,另外 10 个将转到 z_EventWaitlist。

最佳答案

如果 $count == 20,你需要在你的代码中询问一些地方`

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
{

if($leftPax > 0 && $count <= 20)
{
$registrantBean = new z_EventRegistrants();
$registrantBean->last_name = $data[1];
$registrantBean->id_no = $data[0];
$registrantBean->phone_mobile = $data[2];
$registrantBean->email1 = $data[3];
$registrantBean->gender = $data[4];
$registrantBean->date_of_birth = $data[5];
$registrantBean->age = $data[6];
$registrantBean->race = $data[7];
$registrantBean->z_eventregec5erations_ida = $registrationBean->id;
$registrantBean->save();
$count++;
error_log(print_r($_REQUEST['session'], 1));
foreach($_REQUEST['session'] as $aid=>$sid)
{
$attendanceBean = new z_EventAttendances();
$attendanceBean->name = $registrantBean->last_name;
$attendanceBean->z_eventsessions_z_eventattendancesz_eventsessions_ida = $sid;
$attendanceBean->z_eventactivities_z_eventattendances_1z_eventactivities_ida = $aid;
$attendanceBean->z_eventregistrations_z_eventattendancesz_eventregistrations_ida = $registrationBean->id;
$attendanceBean->z_eventregistrants_z_eventattendancesz_eventregistrants_ida = $registrantBean->id;
$attendanceBean->save();
}
}
else
{
$waitlistBean = new z_EventWaitlists();
$waitlistBean->name = $data[1];
$waitlistBean->save();
}
}
}

`

关于php - CSV 只导入一定数量然后超过数据进入另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42499454/

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