3

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 .

1
  • thats becaus its inside a foreach loop. it will give the error for each time it loops Commented Oct 17, 2016 at 8:59

1 Answer 1

2

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

?>  
Sign up to request clarification or add additional context in comments.

13 Comments

It works! but when the $checkfile = true , it still have the alert message " import fail"
@beginner123 Have you tried way2? I have made change in it. If array size is greater then 0 then only import fail message will fire. Please check updated answer.
@beginner123 And for way1 if any file will fail then alert message with import fail will fired.
tried way 2, the succesful import still promp me "import Fail" I choose not to display the files because in the folder got too many files. removed .$failedFiles.
ya , was using the way 2 you suggest ( i removed the $failedFiles) when the import success , It still promp alert "Fail to import"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.