Csv value comparison

There are six inverter and each inverter generates csv file which contains details about the inverter. I was Creating an application where user will select the Inverter no and uploads the excel file.

But the problem is, User can select inverter one and can upload inverter 2 csv file…Thus entering wrong data.

Each csv file first column first cell(A1 cell) contains Inverter number…What is the Php code to check that cell value against what user selection…

My html code:

 <h2>Select The Inverter No...</h2>
<label for="Inverter">Inverter</label>
<select name="options">
			   <option>Select The Value</option>
		<option value="1">Inverter 1</option>
	    <option value="2">Inverter 2 </option>
		<option value="3">Inverter 3</option>
		<option value="4">Inverter 4</option> 
		<option value="5">Inverter 5</option> 
		<option value="6">Inverter 6</option>
</select><br/>
<label for="upload">Upload File</label>		
<input type="file" name="file" id="file" class="Upload" onchange="validate_fileupload(this.value);" /><br/><br/><br/>	 
<input type="submit" value="submit" name="submit" class="button"/>

My .php code:

if(isset($_POST["options"],$_POST["submit"])){
		$count=0;
 if($_FILES["file"]["size"] > 0)
		 {

		  	$file = fopen($filename, "r");
			while (($check = fgetcsv($file, 10000, ",")) !== FALSE)
			{
				if($count == 0) { 
				$count++;
						if ($check[0] ==  "INVERTER"+$inverter)

                                     {
					echo "<script type=\"text/javascript\">
										alert(\"It's inverter one associated file.\");
										</script>";
				     }

                                                               {
								        echo "<script type=\"text/javascript\">
										alert(\"File is not assosiated with inverter one.\");
										</script>";
							   }

My csv file:

     A                                       B         C       D
1 Inverter6					
2 My Plant					
3 					
4 11-02-2016 13:59					
5 25-04-2014 11:29					
6 Serial number (CTRL; POW; AUX; COMM):	 056303T353 	 078001T383 	 049222T453 		
7 Product number (CTRL; POW; AUX; COMM):	 139B0120   	 139B0121   	     139B0501   	 	
8 Software version (CTRL; POW; AUX; COMM):	 1.49.1	 0.00.0	 0.00.0	 3.60.6

why do you even let them choose the inverter-#, just take the one from the CSV.

I agree, as the CSV file contains the information, there is no need to have the user input it. But I would probably have a stage where, after the user uploads the CSV file, your code extracts the inverter number, displays it (and perhaps the date, if that is useful to know) and asks the user to confirm that it’s the correct file.

I see two problems in your code:

  1. You are comparing the first element of the first line in the CSV with “INVERTER” plus the value, when your sample data shows that the string is “Inverter” plus the value. So either get the case correct, or convert it to uppercase before you do the comparison.

  2. You add on the value of $inverter to do the comparison which, unless I can’t see it, doesn’t seem to have been set anywhere. You’re missing a line to get that value from $_POST['options'] as far as I can see.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.