beginner123 beginner123 - 1 month ago 8
Javascript Question

javascript - Convert multiple loop into one alert

im using checkfile to upload file by Month & Year .

If success it will load the data to database.

IF checkfile = false


I want to display an alert message
("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);

?>


I tried to have echo alert('Import Fail ');

but it give me multiple alert every single file from the folder .

Answer

Way1 : Manage the flow with flag as below. If any of the file successfully imported then change the flag and then make a condition on the flag to display alert.

$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);

?>  

Way 2: Get the name of successfully imported file names and make conditions on it.

$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);

?>  
Comments