2016-03-10 17 views
8

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 Function.php

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 biết những gì tôi đang làm sai trong kịch bản PHP của tôi. Dường như mọi thứ đều chính xác.

Có ai có ý tưởng nào để giải quyết nó không?

Dưới đây là kịch bản PHP của tôi:

<?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); 
?> 
+0

Phiên bản PHPExcel nào? –

+0

@MarkBaker, Xin chào, tôi đang sử dụng PHPExcel phiên bản 1.8.0. – user979974

+0

Đó là lỗi đã được sửa cho phiên bản 1.8.1 –

Trả lời

7

Chỉ cần loại bỏ các "break;" câu lệnh trong tệp functions.php. Khi ngắt là sau khi tuyên bố trở lại, do đó, nó đưa ra lỗi nghiêm trọng.

+0

Chỉ cần làm rõ - PHP phàn nàn không phải vì * break * statemant được tìm thấy sau * return *, nhưng vì nó không phải là bên trong switch hoặc loop block –

+0

Đối với tôi cũng cùng một vấn đề nhưng bây giờ nó làm việc –

1

Cách thích hợp để giải quyết vấn đề không tương thích với thư viện của bên thứ 3 mã nguồn mở sẽ được:

  1. Kiểm tra nếu có đã được phát hành phiên bản cập nhật của thư viện bạn đang gặp vấn đề với
  2. Gửi một lỗi vào thư viện của người bảo trì và nếu bạn sửa một tệp khác với bản sửa lỗi của mình hoặc yêu cầu git pull

Trong trường hợp của bạn, chỉ cần tải xuống PHPExcel từ github, giải nén và ghi đè các thư viện cũ.

+0

Tôi vừa cập nhật PHPExcel và bây giờ mọi thứ hoạt động hoàn hảo! : thumbsup: –