<? 

require_once("../includes/main.inc.php");

@extract($_POST);					

include("Excel/reader.php");

    function uploadFile($fieldName, $fileType, $folderName, $name = "")

    {

        $flg = 0;

        $MaxID = "";

        $ext = ""; 

        $uploadfile = "";

        if (isset($fieldName) AND $fieldName['name'] != '')

        {

            $flg = 1;

            $allowed_filetypes = $fileType;

            $max_filesize = 1048576;

            $filename = $fieldName['name'];

            if ($name == "")

                $MaxID = time() . time() . rand(1, 100);

            else

                $MaxID = $name;

            $ext = substr($filename, strpos($filename, '.'), strlen($filename) - 1);

			if($ext==".xlsx")

				$ext=".xls";

            if (!in_array($ext, $allowed_filetypes))

                echo "<h1>The file you attempted to upload is not allowed...</h1>";

            else if (filesize($fieldName['tmp_name']) > $max_filesize)

                echo "<h1>The file you attempted to upload is too large...</h1>";

            else 

            {

                $uploadfile = $folderName . "/" . $MaxID . $ext;

                if (move_uploaded_file($fieldName['tmp_name'], $uploadfile) == FALSE)

                {

                    echo "<h1>Error in Uploading File...</h1>";

                    $MaxID = "";

                }

                else

                    $MaxID = $MaxID . $ext;

            }

        }

        return $MaxID;

    }

    if(isset($_POST['submit']))

	{

		$cid = $_POST['cid'];

		

		if($_FILES['csvFile']['name']!="")

		{	

			$fileName=uploadFile($_FILES['csvFile'],array(".csv"),"excel_file");

			$row=0;

			if(($handle = fopen("excel_file/".$fileName , "r")) !== FALSE) 

			{  $i=0;

				while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 

				{ 

					$num = count($data);

					//echo '<pre>';

					//print_r($data);

					 $county_id = addslashes($data['0']);

					 $county_name = addslashes($data['1']);

					 $county_short_name = addslashes($data['2']);

					 $state_id = addslashes($data['3']);

					 $county_status = addslashes($data['4']);					

					 $title = addslashes($data['5']);					

					 $description = addslashes($data['6']);	

					 $keyword = addslashes($data['7']);

					 $county_ad1 = addslashes($data['8']);

					 $county_ad2 = addslashes($data['9']);

					 $county_ad3 = addslashes($data['10']);

					 $county_title = addslashes($data['11']);					

					 $county_descriptions = addslashes($data['12']);



					if($i > 0){		

					$sqlch = mysql_query("select `county_id` from `tbl_county` where  `county_id`='$county_id' ");

					if(mysql_num_rows($sqlch) == 0){			

					$query="INSERT INTO tbl_county(county_id, county_name, county_short_name, state_id, county_status, title, description, keyword, county_ad1, county_ad2, county_ad3, county_title, county_descriptions, county_join) 

					VALUES('".$county_id."','".$county_name."','".$county_short_name."', '".$state_id."','".$county_status."','".$title."','".$description."','".$keyword."', '".$county_ad1."','".$county_ad2."','".$county_ad3."','".$county_title."','".$county_descriptions."', now() )";

					mysql_query($query);

					}else{

						$already[] = $county_id;

					}

					

					}

					$i++;

				}

				fclose($handle);

			}

			set_session_msg("Taluk / Tehsil / City Name Record Imorted successfully.");

		}

		set_session_msg("County Name Record Imported successfully.");

		header("Location: taluk.php");

		exit;

	}

?>





<link href="styles.css" rel="stylesheet" type="text/css">

<script language="javascript" src="ajax2.js"></script>

<? include("top.inc.php");?>

<center class="msg"><?=$msg;?><?php if($already){ echo 'Below County Name ID Already exist.<br />'; /*print_r($already);*/  for($n=0; $n < count($already); $n++){ echo $already[$n].', '; } } ?></center>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

	<tr>

		<td id="pageHead">

			<div id="txtPageHead">

				<?

				if($_REQUEST['set_flag']=='update')

					echo "Edit Flash File"; else echo "Import County Name records in Excel(.csv) file";?>

			</div>

		</td>

	</tr>

</table>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

 <tr>

	<td id="content" align="center"><strong class="msg"><?=display_sess_msg(); ?></strong>

	   <form method="post" name="form2" id="form2" enctype="multipart/form-data" onsubmit="return validate(this);"> 

		<table width="80%" align="center" border=0 cellpadding="0"  cellspacing="0" class="text">

    	<tr>

		<td  colspan="2"height="30"  align="right"><a href="taluk.php?start=<?=$start?>" class="redcolor"><strong>Back To County Name List</strong></a></td>

		</tr>    

        <tr><td height="15"><div align="center"><span class="msg"><?=display_sess_msg();?></span></div><br /></td></tr>  

		

       

        <tr><td height="30">&nbsp;</td></tr>

        

       

        <tr>

		  <td class="lables"><b>Excel File : </b></td>

          <td class="feildstxt"><input name="csvFile" type="file" /></td>

        </tr>

        <tr><td height="30">&nbsp;</td></tr>

		

        <tr>

        <td align="left" class="lables">&nbsp;</td>

        <td align="left" class="feildstxt"><input class="button" type="submit" name="submit" value="Save"></td>

        </tr>

        <tr><td align="right" colspan="2" class="lables"><a href="csv-sample-file/county_sample.csv">Download Excel Sample File For Import County</a></td></tr>

   </table>	

	   </form>

	 <? include("paging.inc.php");?>

	</td>

</tr>

</table>

<? include("bottom.inc.php");?>