【发布时间】:2015-08-18 14:06:14
【问题描述】:
我在索引中找不到错误。请您帮我解决这个问题。 错误出现在 fancyTable 函数的第二个循环中。
错误是这样的 Undefined offset in 0, Undefined offset in 1, undefined offset in 2, undefined offset in 3
class PDF extends FPDF {
function LoadData() {
//Read file lines
$con = new Connection();
$connect = $con->getConnection();
$sector = $_POST['text_sector'];
$start_yr = $_POST['text_start_year'];
$end_yr = $_POST['text_end_year'];
if ($start_yr != '' || $end_yr != '') {
if ($end_yr == '') {
$end_yr = date("Y-m-d");
}
if ($start_yr == '') {
$start_yr = strtotime('' . date("Y-m-d") . ' -10 year');
$start_yr = date("Y-m-d", $start_yr);
}
//echo $start_yr;
//echo $end_yr;
$sql = "sql is too long to write here";
} else {
$sql = 'sql is too long to write here';
}
$result = mysqli_query($connect, $sql);
$dataArray = array(); // make a new array to hold all your data
$index = 0;
while ($row = mysqli_fetch_assoc($result)) {
$dataArray[$index] = $row;
$index++;
}
return $dataArray;
}
function FancyTable($header, $data) {
$this->SetFillColor(255, 0, 0);
$this->SetTextColor(255);
$this->SetDrawColor(128, 0, 0);
$this->SetLineWidth(.3);
$this->SetFont('', 'B');
//Header
$w = array(25, 50, 25, 25, 35);
for ($i = 0; $i < count($header); $i++) {
$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
}
$this->Ln();
//Color and font restoration
$this->SetFillColor(224, 235, 255);
$this->SetTextColor(0);
$this->SetFont('');
//Data
$fill = 0;
foreach ($data as $row) {
//$pdf->Cell($w, $h, $txt, $border, $ln, $align)
$this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
$this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
$this->Cell($w[2], 6, $row[2], 'LR', 0, 'L', $fill);
$this->Cell($w[3], 6, $row[3], 'LR', 0, 'L', $fill);
$this->Cell($w[4], 6, number_format($row[4]), 'LR', 0, 'R', $fill);
$this->Ln();
$fill = !$fill;
}
$this->Cell(array_sum($w), 0, '', 'T');
}
}
$pdf = new PDF();
//Column titles
$header = array(' No', 'Name', 'Client', 'Completed Date', 'Total Amount');
$dataArray = $pdf->LoadData();
$pdf->SetFont('Arial', '', 12);
$pdf->AddPage();
$pdf->FancyTable($header, $dataArray);
$pdf->Output();
?>
【问题讨论】:
-
您正试图访问一个不存在的数组索引。你必须
var_dump($w, $row)才能看到你真正在处理什么。