How could I echo only once in while loop condition? The number of echo I am getting is also the number of loop. Thank you very much for your help.
This is the code the part I am echo is in 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';
}
?>
You are counting columns in the row by using $countrow = count($row);
.
I assume you want to get the 7th row. So just count as you loop.
$rowCount = 0;
while($row = fgetcsv($file_data))
{
$rowCount ++;
// your code
if($rowCount === 7){
// etc
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.