简体   繁体   中英

PHP Iterating CSV with different columns

I have a CSV defined like the data this.

"PID",                 "FName",         "LName",       "Email"
2425751712402934017,
1037862,                "Jason",        "Van Hooser",   "jvanhooser@example.com"
961741,                 "Alana",        "Traxler",      "atraxler@example.com"
1100854,                "Emily",        "Walcheck",     "ewalcheck@example.com"
1166892,                "Mary",         "Thomas",       "mthomas@example.com"
8853065679823467777,
1179079,                "Donna",        "Thimm",        "dthimm@example.com"
927671,                 "Lillian",      "Wasson",       "lwasson@example.com"
1175139,                "Barry",        "Tollison",     "btollison@example.com"
1058086,                "Christina",    "Viktorin",     "cviktorin@example.com"

What I need to do is iterate through it and when it comes to the lines where there is only the PID field with the long number, I need to store that in a variable ($wkey) and then use it in an insert statement. I know we could put the value on each row but the process that outputs the file cannot do that.

Hwere is my code:

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if($flag) {
        $flag = false;
        continue;
    }
    $import = "INSERT into exp_wb_bulk_reg(`WKey`,`PID`,`FName`,`LName`, `Email`,`status`) "
            . "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
    // Use the sql to insert into the table
}        
fclose($handle);

How would I modify this to do what I need?

Here's working code:

// skip header line
$data = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    // get wkey for lines that has empty second column
    if(trim($data[1]) == "") {
        $wkey = $data[0];
    }
    if(trim($data[1]) != "") {
        $import = "INSERT into exp_wb_bulk_registrations(`WebinarKey`, `PID`,`FName`,`LName`, `Email`,`status`) "
                . "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
        echo $import."<br />";
    }
}        
fclose($handle);

You could just check the size of $data array. Something like:

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if(count($data) == 4){
        $import = "INSERT into exp_wb_bulk_reg(`WKey`,`PID`,`FName`,`LName`, `Email`,`status`) "
            . "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
        // Use the sql to insert into the table
    } else if(count($data) == ?) {
        //DO STUFF
    }
}        
fclose($handle);

Here's my variant:

$wkey = ''; // just for sure

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
{
    if(count($data)==1) {  // hey, we found a wKey! let's remember it
        $wkey = $data[0]; continue; 
    }

    $import = "INSERT into exp_wb_bulk_reg(`WKey`,`PID`,`FName`,`LName`, `Email`,`status`) "
            . "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
}        
fclose($handle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM