gpt4 book ai didi

javascript - 将多个循环转换为一个警报

转载 作者:搜寻专家 更新时间:2023-10-31 21:02:02 25 4
gpt4 key购买 nike

我正在使用 checkfile 按月份和年份上传文件。

如果成功,它会将数据加载到数据库。

IF checkfile = false 

我想显示一条警告消息("import Fail")

foreach ($files1 as &$value) {

$checkfile = strpos($value,$input1 );

if($checkfile === false) {
//echo "<script>alert('Import Fail ');</script>";
}
else
{
echo "Import $value successfully! <br>" ;

$query = "load data local infile '//192.168.100.3/Groups/$location1/Timesheet/$value'
replace

$result = $connection->query($query) or exit("Error code ({$connection->errno}): {$connection->error}");

}

}

unset($value);

?>

我试着让 echo alert('Import Fail ');

但它会为文件夹中的每个文件发出多重警报。

最佳答案

Way1 : 使用如下标志管理流程。如果任何文件成功导入,则更改标志,然后在标志上设置条件以显示警报。

$flag = 0;
foreach ($files1 as &$value) {

$checkfile = strpos($value,$input1 );

if($checkfile === false) {
//echo "<script>alert('Import Fail ');</script>";
}
else
{
$flag = 1;
echo "Import $value successfully! <br>" ;

// $query1 =
// "Delete FROM hklcanet_pha.psr this
// WHERE year(psr.ReportDate) = $input3
// AND month(psr.ReportDate) = $input2";


$query = "load data local infile '//192.168.100.3/Groups/$location1/Timesheet/$value'
replace
into table hklcanet_pha.psr fields terminated by ','
optionally enclosed by '\"'
lines terminated by '\n'
ignore 1 lines
(`ReportDate`, @dummy, @dummy, `Team_refno`,`Name/Description`,`Status`,`PIC`,`RequestDate`,`TargetEndDate`,`ActualEndDate`,`PlanStartDate`,`ActualStartDate`,`PlanUATDate`,`ActualUATDate`,`PlanImplement`,`ActualImplement` )";

//echo "Import $value successfully! <br>" ;


$result = $connection->query($query) or exit("Error code ({$connection->errno}): {$connection->error}");

}

}
if($flag == 1){
echo "<script>alert('Import Fail ');</script>";
}
unset($value);

?>

方式二:获取导入成功的文件名的名称,并对其进行条件

$flag = [];
foreach ($files1 as &$value) {

$checkfile = strpos($value,$input1 );

if($checkfile === false) {
//echo "<script>alert('Import Fail ');</script>";
}
else
{
//Store successful file names in array
$flag[] = $value;

echo "Import $value successfully! <br>" ;

// $query1 =
// "Delete FROM hklcanet_pha.psr this
// WHERE year(psr.ReportDate) = $input3
// AND month(psr.ReportDate) = $input2";


$query = "load data local infile '//192.168.100.3/Groups/$location1/Timesheet/$value'
replace
into table hklcanet_pha.psr fields terminated by ','
optionally enclosed by '\"'
lines terminated by '\n'
ignore 1 lines
(`ReportDate`, @dummy, @dummy, `Team_refno`,`Name/Description`,`Status`,`PIC`,`RequestDate`,`TargetEndDate`,`ActualEndDate`,`PlanStartDate`,`ActualStartDate`,`PlanUATDate`,`ActualUATDate`,`PlanImplement`,`ActualImplement` )";

//echo "Import $value successfully! <br>" ;


$result = $connection->query($query) or exit("Error code ({$connection->errno}): {$connection->error}");

}

}
if(sizeof($flag) == 0){
//Display all the file names in alert
$failedFiles = implode(",",$flag);
echo "<script>alert('Import Failed for files : ".$failedFiles."');</script>";
}
unset($value);

?>

关于javascript - 将多个循环转换为一个警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40082172/

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