0

file.csv structure:

file.csv

Before uploading to folder upload, I want to check the column.

The algorithm is like:

if ((column 1 = 'no') && (column 2 = 'product_name') && (column 3 = 'product_price') && (column 4 = 'qty'))
{
    upload();
}  
else
{
    die;
}

Is there any coding for this case using php coding or sql to validate?

1 Answer 1

1

We have to load the csv file in location and fetch the csv files content and validate the content.

Here is my example:

$destination=C:\Users\AIT\Desktop\SC MArgins new.csv;
$contents = file ($destination);
for($i=1; $i<sizeof($contents); $i++)
{ 
$line = trim($contents[$i],'",'); 
 $arr = explode(',',$line);
if($arr[0]=='')
            {
                $rows.='Item#,';
            }

            if($arr[1]=='')
            {
             $rows.='Product Name,';
            }

            if($arr[2]=='')
            {
            $rows.='Product Code,';
            }

            if($arr[3]=='')
            {

            $rows.='Product Grade,';

            }
if($rows!=''){
            $row1.='Row '.$i.'-'. $rows.'can not be empty'."<br>";}
            $k.=$row1;
            }
if($k!='' ){        
            $common->setMsg($row1, 'box_warning');
            return false;

}

This code will help u to get which column is missing. Follow this code and let me know if any issues on processing the csv

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.