php中未定义的偏移量错误1

时间:2015-08-18 14:06:15

标签: php arrays pdf fpdf

我在索引中找不到错误。你能帮我解决这个问题。 错误发生在fancyTable函数的第二个循环中。

  

错误是这样的未定义偏移量,0,未定义的偏移量   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();
?> 

2 个答案:

答案 0 :(得分:0)

mysqli_fetch_assoc返回一个关联数组,按列名索引。 对于数字索引结果,请使用mysqli_fetch_row

function LoadData() {
    //your sql generating code here
    $dataArray = array(); // make a new array to hold all your data
    //$index = 0;
    while ($row = mysqli_fetch_row($result)) { 
        $dataArray[] = $row;
        //$index++;
    }
    return $dataArray;
}

此外,$index变量不是必需的,只需使用$array[] = $var语法就可以向数组中添加新元素,并使用递增的数字索引

答案 1 :(得分:0)

mysql_fetch_assoc将字段名称作为键

返回
 while ($row = mysqli_fetch_assoc($result)) { 
    $dataArray[$index] = $row;
    $index++;
 }

成为

 $dataArray[0]["fieldname"] = "value";

你正试图通过整数键

来获取字段
 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;
}

您可以使用$ row ['fieldname']等字段名替换$ row [0] .. $ row [3],也可以使用mysql_fetch_row代替mysql_fetch_assoc