如何在PHP中偶尔回声一次

时间:2018-10-11 09:36:14

标签: php

在while循环条件下,我怎么只能回声一次?我得到的回声数也是循环数。非常感谢您的帮助。

这是我在if($countrow === 7){ }..中回声的部分的代码

<?php  
 if(!empty($_FILES["employee_file"]["name"]))  
 {  
      $connect = mysqli_connect("localhost", "root", "", "database");  
      $output = '';  
      $allowed_ext = array("csv");  
      $tmp = explode(".", $_FILES["employee_file"]["name"]);
      $extension = end($tmp); 
      if(in_array($extension, $allowed_ext))  
      {  
           $file_data = fopen($_FILES["employee_file"]["tmp_name"], 'r');  
           fgetcsv($file_data);        
           while($row = fgetcsv($file_data))  
           {
               $countrow = count($row);
               if($countrow === 7){
                $image = mysqli_real_escape_string($connect, $row[0]);             
                $id = mysqli_real_escape_string($connect, $row[1]);
                $rfid = mysqli_real_escape_string($connect, $row[2]);  
                $firstname = mysqli_real_escape_string($connect, $row[3]);  
                $lastname = mysqli_real_escape_string($connect, $row[4]);  
                $Role = mysqli_real_escape_string($connect, $row[5]);  
                $Details = mysqli_real_escape_string($connect, $row[6]);                
                $query = "  
                INSERT INTO table 
                     (Column names)  
                     VALUES (values)  
                ";              
                mysqli_query($connect, $query);
               //echo should be here but the value is looping.
               }
               else
               {
                   echo 'There was something wrong in csv column';
                   break;
               }               
           }
      }        
      else  
      {  
           echo 'Please select a csv file';  
      }  
 }  
 else  
 {  
      echo 'Please select a csv';  
 }  
 ?>  

1 个答案:

答案 0 :(得分:0)

您正在使用$countrow = count($row);对行中的列进行计数。

我假设您想获得第七行。因此,只要计数就可以了。

$rowCount = 0;
while($row = fgetcsv($file_data))  
{
    $rowCount ++; 
    // your code
    if($rowCount === 7){
        // etc
    }
}