dongshou2024 2016-11-25 10:21
浏览 40

如何在PHP中验证CSV列?

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条回答 默认 最新

  • dongyu8664 2016-11-25 10:34
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条