Fpdf线,多单元,阵列

时间:2017-02-28 04:30:07

标签: php arrays fpdf

我不知道我在做什么,但不知怎的,我设法让我的代码或多或少得到我想要的东西。问题是,我认为我可以使这更简单,但我在编码部门不是很聪明,所以我不知道该怎么做。为了解释,我需要我的pdf看起来像这样:

How it should look

我能够使用此代码获得该图像:

function FirstPageLines() {
    $this - > SetFont('Arial2', '', 7);
    $arrComponents = array("Milk", "Grains", "Vegetables", "Fruits", "Meats");
    $arrMilk = array("Fat Free", "Low Fat", "Yogurt");
    $arrGrains = array("Crackers", "Cookies", "Bread");
    $arrVegs = array("Onion", "Lettuce", "Carrot");
    $arrFruits = array("Pineapple", "Orange", "Grape");
    $arrMeats = array("Chicken", "Fajitas", "Pork", "Ribs");

    $yLine = 10;
    $xLine = 33;
    $yText = 20;
    ///////////////////////////Milk Section///////////////////////////// 
    $this - > SetFont('Arial', 'B', 12);
    $this - > SetXY(10, 10);
    $this - > SetFillColor(160, 160, 160); //set fill color to grey
    $this - > MultiCell(590, 14, "Milks", 1, 'L', 1);
    $this - > Ln();

    $length = count($arrMilk);
    for ($i = 0; $i < $length; $i++) {
        $this - > SetFont('Arial', 'I', 11);
        $this - > Text($xLine, $yLine + 24, $arrMilk[$i]);
        $this - > Line($xLine, $yLine + 25, 600, $yLine + 25);
        $yText = $yText + 50;
        $yLine = $yLine + 13;
    }

    ///////////////////////////Grains Section///////////////////////////// 
    $this - > Ln();
    $this - > SetFont('Arial', 'B', 12);
    $this - > SetXY($xLine - 20, $yLine + 20);
    $this - > SetFillColor(160, 160, 160); //set fill color to grey
    $this - > MultiCell(590, 14, "Grains", 1, 'L', 1);

    $length = count($arrGrains);
    for ($i = 0; $i < $length; $i++) {
        $this - > Ln();
        $this - > SetFont('Arial', 'I', 11);
        $this - > Text($xLine, $yLine + 45, $arrGrains[$i]);
        $this - > Line($xLine, $yLine + 45, 600, $yLine + 45);
        $yText = $yText + 50;
        $yLine = $yLine + 13;
    }
    ////////////////////////Vegetables Section/////////////////////////////
    $this - > Ln();
    $this - > SetFont('Arial', 'B', 12);
    $this - > SetXY($xLine - 20, $yLine + 40);
    $this - > SetFillColor(160, 160, 160); //set fill color to grey
    $this - > MultiCell(590, 14, "Vegetables", 1, 'L', 1);

    $length = count($arrVegs);
    for ($i = 0; $i < $length; $i++) {
        $this - > Ln();
        $this - > SetFont('Arial', 'I', 11);
        $this - > Text($xLine, $yLine + 65, $arrVegs[$i]);
        $this - > Line($xLine, $yLine + 65, 600, $yLine + 65);
        $yText = $yText + 50;
        $yLine = $yLine + 13;
    }
    /////////////////////////Fruits Section/////////////////////////////             
    $this - > Ln();
    $this - > SetFont('Arial', 'B', 12);
    $this - > SetXY($xLine - 20, $yLine + 60);
    $this - > SetFillColor(160, 160, 160); //set fill color to grey
    $this - > MultiCell(590, 14, "Fruits", 1, 'L', 1);

    $length = count($arrFruits);
    for ($i = 0; $i < $length; $i++) {
        $this - > Ln();
        $this - > SetFont('Arial', 'I', 11);
        $this - > Text($xLine, $yLine + 85, $arrFruits[$i]);
        $this - > Line($xLine, $yLine + 85, 600, $yLine + 85);
        $yText = $yText + 50;
        $yLine = $yLine + 13;
    }
    //////////////////////////Meats Section/////////////////////////////      
    $this - > Ln();
    $this - > SetFont('Arial', 'B', 12);
    $this - > SetXY($xLine - 20, $yLine + 80);
    $this - > SetFillColor(160, 160, 160); //set fill color to grey
    $this - > MultiCell(590, 14, "Meats", 1, 'L', 1);

    $length = count($arrMeats);
    for ($i = 0; $i < $length; $i++) {
        $this - > Ln();
        $this - > SetFont('Arial', 'I', 11);
        $this - > Text($xLine, $yLine + 105, $arrMeats[$i]);
        $this - > Line($xLine, $yLine + 105, 600, $yLine + 105);
        $yText = $yText + 50;
        $yLine = $yLine + 13;
    }

所以,我的问题是:我想在顶部使用组件$arrComponents=array("Milk","Grains","Vegetables","Fruits","Meats");

而不是必须在灰色框中单独写入每个标题。我还希望其他数组填充在相应的标题下(牛奶标题下的牛奶项目;谷物标题下的谷物等)。如果没有物品,我也希望没有标题显示,因为有时客户可能不需要任何牛奶,但只需要一两个其他组件。最后,我测试了我的代码,看看当数组中有各种项目时会发生什么,我注意到一旦项目填满一页,下一页就会停止显示标题下的项目。标题显示,但不显示其下的项目。我知道我也可以使用一些帮助清理这个代码,所以它不会那么混乱。

1 个答案:

答案 0 :(得分:1)

使用关联数组定义组件。

$components = array(
    "Milk" => array("Fat Free", "Low Fat", "Yogurt"),
    "Grains" => array("Crackers", "Cookies", "Bread"),
    "Vegetables" => array("Onion", "Lettuce", "Carrot"),
    "Fruits" => array("Pineapple", "Orange", "Grape"),
    "Meats" => array("Chicken", "Fajitas", "Pork", "Ribs")
);

看起来您已经创建了FPDF的子类。好!让我们添加一种方法将关联数组传递给该子类。

class MRod_FPDF extends FPDF {
    var $components;

    function SetComponents($components) {
        $this->components = $components;
    }
}

添加灰色标题框是一项重复性任务,所以让我们将该代码移动到自己的方法中:

    function AddComponentHeader($component) {
        $this->SetFont('Arial', 'B', 12);
        $this->SetFillColor(160, 160, 160);
        $this->MultiCell(190, 12, $component, 1, 'L', 1);
    }

为每个组件添加项目也是一项重复性任务,所以让我们为此创建一个方法。 (我还为每个项目的X偏移添加了一个变量。)

请注意,我使用MultiCell代替Text来节省处理坐标的麻烦,这使得代码在我们创建的文档内容扩展时更加可靠到多页。

    var $itemX = 30;

    function AddItems($items) {
        $this->SetFont('Arial', 'I', 11);
        foreach ($items as $item) {
            $this->SetX($this->itemX);
            $this->MultiCell(167, 8, $item);
            $y = $this->GetY();
            $this->Line($this->itemX, $y, 200, $y);
        }
        $this->Ln(5);
    }

您的FirstPageLines方法现在就像这样简单:

    function FirstPageLines() {
        foreach ($this->components as $component => $items) {
            if (!empty($items)) {
                $this->AddComponentHeader($component);
                $this->AddItems($items);
            }
        }
    }

查找完整代码here