【问题标题】:Undefined offset errorin 1 in phpphp中未定义的偏移量错误1
【发布时间】: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) 才能看到你真正在处理什么。

标签: php arrays pdf fpdf


【解决方案1】:

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 语法即可将新元素添加到具有升序数字索引的数组中

【讨论】:

  • 非常感谢。这对我很有帮助。
【解决方案2】:

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[0]..$row[3] 替换为 $row['fieldname'] 之类的字段名称,也可以使用 mysql_fetch_row 代替 mysql_fetch_assoc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    相关资源
    最近更新 更多