I am uploading excel in PHP using PHPExcel_IOFactory, In excel there are some columns which should be compulsory filled like B,D,I,J and more.
My need is to first change Column B, if its blank an error should occur else check column D, if blank error else check further columns.
I have written below code, but i didn't got to check one column at a time:
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);
for($i=2;$i<=$arrayCount;$i++){
$b = $allDataInSheet[$i]["B"];
$d = $allDataInSheet[$i]["D"];
$i = $allDataInSheet[$i]["I"];
}
There are almost 20 columns compulsory, how can i check each column at a time.
Be careful. You're using $i
in the for($i=2...
line, but you set it to something else inside the loop with $i = $allData...
.
To meet your requirements (including starting with 2nd row), this is what I would do
$firstK = key($allDataInSheet); //save the first key
foreach($allDataInSheet as $k => $row){
if($k===$firstK) continue; //skip the first row
if(empty($b = $row['B'])){
// Error. column B is empty
}elseif(empty($c = $row['C'])){
// B is not empty. $b has its contents
// however, Error: column C is empty
}elseif(empty($i = $row['I'])){
// B and C are not empty. $b and $c have their contents
// however, Error: column I is empty
...
}else{
// All required columns have data
// $b, $c ... $i ... hold the contents
}
//...continue processing the row. You can use variables $b, $c...
}