2

i am in the middle of the development. i want to eliminate the empty rows while upload a excel from PHP using PHP excel plugin.

while($x<=$excel->sheets[0]['numRows']){
    $y=1;
    while($y<=$excel->sheets[0]['numCols']){
        $cell = isset($excel->sheets[0]['cells'][$x][$y])
            ? $excel->sheets[0]['cells'][$x][$y] 
            : '';
        if($excel->sheets[0]['cells'][$x][$y] == $excel->sheets[0]['cells'][$x][1] ){
            echo $cell;
        } else { 
            echo $cell; 
        }
        $y++;
    } 
    $x++;
} 

i used this code to get the values from excel to PHP, but if the excel has empty record the result will be modified something like extra cells. how can i fix this.

5
  • Are you really using PHPExcel, or are you using the PEAR Spreadsheet_Excel_Writer (SEW)... it looks more like the latter to me Commented Apr 9, 2013 at 8:19
  • Sorry, i used Spreadsheet_Excel_Writer only. how can i eliminate empty rows Commented Apr 9, 2013 at 8:38
  • You need to test if every cell in the row array is empty before your second while loop, and only execute that while loop if there are non-empty cells. I'd suggest using array_filter() to get a count of the non-empty cells Commented Apr 9, 2013 at 8:46
  • its says : Parse error: syntax error, unexpected T_FUNCTION in Commented Apr 9, 2013 at 10:31
  • What says that? What version of PHP are you running? My answer requires a minimum of 5.3.0; but you should be running at least that anyway as anything below that is no longer supported Commented Apr 9, 2013 at 10:34

1 Answer 1

1
if (!array_reduce(
    $excel->sheets[0]['cells'][$x],
    function ($state, $value) {
        return $state && !($value > '');
    },
    TRUE
)) {
// execute your while($y<=$excel->sheets[0]['numCols']) loop here
}
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.