20

I am trying PHPExcel and I get an error in output when I perform my script:

Fatal error: 'break' not in the 'loop' or 'switch' context in /opt/lampp/htdocs/Xlsphp/test/Classes/PHPExcel/Calculation/Functions.php on line 581

I don't know what I am doing wrong in my PHP script. It seems that everything is correct.

Does anybody have any idea how to solve it?

Here's my PHP script:

<?php
require_once 'Classes/PHPExcel.php';
require_once 'config.php';

$sql = 'SELECT * FROM tablevalues';
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$fileName = 'test.xls';

// initialise excel column name
// currently limited to queries with less than 27 columns
$columnArray = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");

// Instantiate a new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0);
// Initialise the Excel row number
$rowCount = 1;
// fetch result set column information
$finfo = mysqli_fetch_fields($result);
// initialise columnlenght counter
$columnlenght = 0;
foreach ($finfo as $val) {
    // set column header values
    $objPHPExcel->getActiveSheet()->SetCellValue($columnArray[$columnlenght++] . $rowCount, $val->name);
}
// make the column headers bold
$objPHPExcel->getActiveSheet()->getStyle($columnArray[0]."1:".$columnArray[$columnlenght]."1")->getFont()->setBold(true);

$rowCount++;
// Iterate through each result from the SQL query in turn
// We fetch each database result row into $row in turn

while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
    for ($i = 0; $i < $columnlenght; $i++) {
        $objPHPExcel->getActiveSheet()->SetCellValue($columnArray[$i] . $rowCount, $row[$i]);
    }
    $rowCount++;
}
// set header information to force download
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
// Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file
// Write the Excel file to filename some_excel_file.xlsx in the current directory
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
// Write the Excel file to filename some_excel_file.xlsx in the current directory
$objWriter->save('php://output');

mysqli_close($conn);
?>
Antti29
  • 2,953
  • 12
  • 34
  • 36
user979974
  • 883
  • 3
  • 13
  • 32

3 Answers3

39

Just remove the "break;" statement in functions.php file. As break is after return statement, so it giving fatal error.

Ankit
  • 638
  • 1
  • 8
  • 10
  • 2
    Just to clarify - PHP complains not because the *break* statemant is found after *return*, but because it is not inside switch or loop block – Tihomir Mitkov Nov 18 '16 at 10:19
  • 4
    This wasn't an issue apparently under PHP 5.x but now after upgrading a site to 7.x the error crops up. – Chris Goodman Dec 02 '18 at 01:19
3

The proper way to resolve incompatibility problem with 3rd party open source libraries would be to:

  1. Check if there is already released updated version of library you are having problem with
  2. Submit a bug to library's maintainer and if you have fix a diff file with your fix, or git pull request

In your case, just download PHPExcel from github, unpack and overwrite old libraries.

Aleksandar Pavić
  • 3,143
  • 1
  • 34
  • 36
0

Use below save as excel code. It's just a sample. Modify it as per your requirements.

<?php 
// Load the database configuration file 
include 'connect.php';


// Filter the excel data 
function filterData(&$str){ 
    $str = preg_replace("/\t/", "\\t", $str); 
    $str = preg_replace("/\r?\n/", "\\n", $str); 
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; 
} 
 
// Excel file name for download 
$fileName = "members-data_" . date('d-m-Y') . ".xls"; 

   $fields = array('column1','column2');

// Display column names as first row 
$excelData = implode("\t", array_values($fields)) . "\n"; 
 
// Fetch records from database 
$query = $link->query("SELECT  * FROM tablename order by id desc"); 

if($query->num_rows > 0){ 
    // Output each row of the data 
    while($row = $query->fetch_assoc()){ 
        $status = ($row['statuse'] == 1)?'Active':'Inactive'; //Please create statuse column in your table giving tinyint as datatype and length/value=1 and in comments mention 1=Active | 0=Inactive

        $lineData = array($row['value1'],$row['value2'],); 
        array_walk($lineData, 'filterData'); 
        $excelData .= implode("\t", array_values($lineData)) . "\n"; 
    } 
}else{ 
    $excelData .= 'No records found...'. "\n"; 
} 
 
// Headers for download 
header("Content-Type: application/vnd.ms-excel"); 
header("Content-Disposition: attachment; filename=\"$fileName\""); 
 print chr(255) . chr(254).mb_convert_encoding($excelData, 'UTF-16LE', 'UTF-8');
// Render excel data 
//echo $excelData; 
 
exit;