headers and blank lines

I’m working on a project for school. I’m trying to import csv file into my database. I’m able to do that. However, I’m not sure how to do this without the header and blank lines. This is what I have so far.

[php]<?php
if(isset($_POST[“Import”]))
{
include("…/dbcc.php");
$host=“localhost”; // Host name.
$db_user=“tamdod”;
$db_password=“mgvq6z”;
$db=‘tamdod’; // Database name.
$con=@new mysqli($db_s,$db_us,$db_p,$db_n);
$co = mysql_connect(“localhost”,“tamdod”,“mgvq6z”) or die (mysql_error());
mysql_select_db(“tamdod”) or die(mysql_error());

	echo $filename=$_FILES["file"]["tmp_name"];
	//echo $ext=substr($filename,strrpos($filename,"."),(strlen($filename)-strrpos($filename,".")));
if($_FILES["file"]["size"] > 0)
{
	$file = fopen($filename, "r");
		
	
		while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
		{
			//print_r($emapData);
			$sql = "INSERT into schedule(CRN,CRS,NBR,SEC,BILL_HRS,CR_HRS,LINK,CRS_TITLE,XLST_CD,XLST_CRN,XLST_CRS,MAX_ENROLL,ACT_ENROLL,
	SCHD_CD,INST_METH,1_MTYP,1_DAY,1_TIME,1_BLDG,1_RM,2_MTYP,2_DAY,2_TIME,2_BLDG,2_RM,PRIMARY_INSTR_ID,PRIMARY_INSTR_NAME,INSTR_ID,
	INSTR_NAME,SUBTITLE,NOTES,CRS_START_DTE,CRS_END_DTE) values('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]',
	'$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]','$emapData[9]','$emapData[10]',
	'$emapData[11]','$emapData[12]','$emapData[13]','$emapData[14]','$emapData[15]','$emapData[16]','$emapData[17]',
	'$emapData[18]','$emapData[19]','$emapData[20]','$emapData[21]','$emapData[22]','$emapData[23]','$emapData[24]',
	'$emapData[25]','$emapData[26]','$emapData[27]','$emapData[28]','$emapData[29]','$emapData[30]','$emapData[31]','$emapData[32]')";
			mysql_query($sql);
		}
     fclose($file);
		echo "CSV File has been successfully Imported";
}	
else
	echo "Invalid File:Please Upload CSV File";

}

?>

Import CSV/Excel file
Import CSV/Excel file
CSV/Excel File:
[/php]

What you’re asking is pretty easy, I think.

Obviously you know how many columns you have…

The header is usually the first row right? If there is always a header, you can just skip the first row by adding a counter.

Next, you’ll just need to test for a blank row.

See the Code below.

[php]<?php
if(isset($_POST[“Import”]))
{
include("…/dbcc.php");
$host=“localhost”; // Host name.
$db_user=“tamdod”;
$db_password=“mgvq6z”;
$db=‘tamdod’; // Database name.
$con=@new mysqli($db_s,$db_us,$db_p,$db_n);
$co = mysql_connect(“localhost”,“tamdod”,“mgvq6z”) or die (mysql_error());
mysql_select_db(“tamdod”) or die(mysql_error());

	echo $filename=$_FILES["file"]["tmp_name"];
	//echo $ext=substr($filename,strrpos($filename,"."),(strlen($filename)-strrpos($filename,".")));
if($_FILES["file"]["size"] > 0)
{
	$file = fopen($filename, "r");
		//Here's your counter
	       $counter = 1;
		while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
		{

//Here’s your test to make sure it’s not the header and the row is not blank, you might need to add more column tests if the first column can be blank.

if (counter != 1 and $emapData[0] != ‘’) {
//print_r($emapData);
$sql = “INSERT into schedule(CRN,CRS,NBR,SEC,BILL_HRS,CR_HRS,LINK,CRS_TITLE,XLST_CD,XLST_CRN,XLST_CRS,MAX_ENROLL,ACT_ENROLL,
SCHD_CD,INST_METH,1_MTYP,1_DAY,1_TIME,1_BLDG,1_RM,2_MTYP,2_DAY,2_TIME,2_BLDG,2_RM,PRIMARY_INSTR_ID,PRIMARY_INSTR_NAME,INSTR_ID,
INSTR_NAME,SUBTITLE,NOTES,CRS_START_DTE,CRS_END_DTE) values(’$emapData[0]’,’$emapData[1]’,’$emapData[2]’,’$emapData[3]’,
‘$emapData[4]’,’$emapData[5]’,’$emapData[6]’,’$emapData[7]’,’$emapData[8]’,’$emapData[9]’,’$emapData[10]’,
‘$emapData[11]’,’$emapData[12]’,’$emapData[13]’,’$emapData[14]’,’$emapData[15]’,’$emapData[16]’,’$emapData[17]’,
‘$emapData[18]’,’$emapData[19]’,’$emapData[20]’,’$emapData[21]’,’$emapData[22]’,’$emapData[23]’,’$emapData[24]’,
‘$emapData[25]’,’$emapData[26]’,’$emapData[27]’,’$emapData[28]’,’$emapData[29]’,’$emapData[30]’,’$emapData[31]’,’$emapData[32]’)”;
mysql_query($sql);
}
}
fclose($file);
echo “CSV File has been successfully Imported”;
}
else
echo “Invalid File:Please Upload CSV File”;

}

?>

Import CSV/Excel file
Import CSV/Excel file
CSV/Excel File:
[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service