Hello,

I'm using fpdf library to generate pdf invoice. I manage to view the pdf invoice for one item row only. below is the code:

    $qry = "SELECT
                invitems.itemId,
                invitems.invoiceId,
                invitems.itemNumber,
                invitems.ChargesID,
                invitems.itemDesc,
                invitems.itemAmountO,
                invitems.itemAmountP,
                invitems.discountAmount,
                charges.ChargesTitle,               
                invitems.itemqty
            FROM
                invitems
                LEFT JOIN charges ON invitems.ChargesID = charges.ChargesID
            WHERE invoiceId = ".$invoiceId."
                ORDER BY itemNumber ASC";

    $result = mysqli_query($mysqli, $qry) or die('-3'.mysqli_error());
    $rows = mysqli_fetch_assoc($result);

                        $description = $rows['ChargesTitle'];
                        $quantity = $rows['itemqty'];
                        $Official = $rows['itemAmountO'];
                        $Professional = $rows['itemAmountP'];
                        $discountAmount = $rows['discountAmount'];
                        $discount = number_format($Professional, 2, '.', '') * $quantity *(($discountAmount)/100);
                        $itemAmount = number_format($Official, 2, '.', '') + number_format($Professional, 2, '.', '');
                        $lineTotal = ($itemAmount * $quantity) - ($discount);

The code for sending data to pdf library

//Add items
$invoice->addItem($description,$quantity,$Official,$Professional,$discount,$lineTotal);

My Question is how can I fetch all invoice items using arrary or foreach.

Best regards.

Hi, you could loop the $invoice->addItem() method, but this is part of a class I don't have access to, and I don't know how this works (could you share more about it?).

Anyway, try to check the documentation of the $invoice class, to see if it handles arrays of if there's a method like addItems().

@ribrahim, your codes are correct is just that you must include the results in a foreach statement. So here goes the codes;

   while ( $rows = mysqli_fetch_assoc($result)){
                        $description = $rows['ChargesTitle'];
                        $quantity = $rows['itemqty'];
                        $Official = $rows['itemAmountO'];
                        $Professional = $rows['itemAmountP'];
                        $discountAmount = $rows['discountAmount'];
                        $discount = number_format($Professional, 2, '.', '') * $quantity *(($discountAmount)/100);
                        $itemAmount = number_format($Official, 2, '.', '') + number_format($Professional, 2, '.', '');
                        $lineTotal = ($itemAmount * $quantity) - ($discount);
}

Or

   foreach (mysqli_fetch_assoc($result) as $rows){
                        $description = $rows['ChargesTitle'];
                        $quantity = $rows['itemqty'];
                        $Official = $rows['itemAmountO'];
                        $Professional = $rows['itemAmountP'];
                        $discountAmount = $rows['discountAmount'];
                        $discount = number_format($Professional, 2, '.', '') * $quantity *(($discountAmount)/100);
                        $itemAmount = number_format($Official, 2, '.', '') + number_format($Professional, 2, '.', '');
                        $lineTotal = ($itemAmount * $quantity) - ($discount);
}

Hello and many thanks for your reply:

Gideon_1: I tried your solution and not working, I only get one item row.

Cereal: Yes the my php page call another page and contain the followig function for adding items:

    function addItem($item,$description,$quantity,$Official,$Professional,$discount=0,$total)
    {
        $p['item']          = $item;
        $p['description']   = $this->br2nl($description);
        $p['quantity']      = $quantity;
        $p['Official']      = $Official;
        $p['Professional']  = $Professional;
        $p['total']         = $total;

        if($discount!=="0") {
            $this->firstColumnWidth = 60;
            $p['discount'] = $discount;
            if(is_numeric($discount)) {
                $p['discount']  = $this->currency.' '.number_format($discount,2,$this->referenceformat[0],$this->referenceformat[1]);
            }
            $this->discountField = true;
            $this->columns = 6;
        }

        $this->items[]      = $p;
    }

@ribrahim include the invoice->additem in the foreach or while loop

Gideon_1,

if i understand you i do the following:

    while ( $rows = mysqli_fetch_assoc($result)){

                        $item = $rows['itemNumber'];
                        $description = $rows['ChargesTitle'];
                        $quantity = $rows['itemqty'];
                        $Official = $rows['itemAmountO'];
                        $Professional = $rows['itemAmountP'];
                        $discountAmount = $rows['discountAmount'];
                        $discount = number_format($Professional, 2, '.', '') * $quantity *(($discountAmount)/100);
                        $itemAmount = number_format($Official, 2, '.', '') + number_format($Professional, 2, '.', '');
                        $lineTotal = ($itemAmount * $quantity) - ($discount);
$invoice->addItem($description,false,$quantity,$Official,$Professional,$discount,$lineTotal);

            }

but got the following error: Notice: Undefined variable: invoice

Gideon_1,

Done. Thank you very much for your help. All items are listed now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.