foreach loop print only one record from MySQL table

Nima Bahar :

I am trying to print records of a table into a pdf file using fpdf, to this I am using for each loop, but it just prints one record though it works fine without fpdf class. here is the code:

if(isset($_GET['show_id'])){
    $id = ($_GET['show_id']);
    $comments = selectAll('proposal_comment', ['proposal_id' => $id]);

    foreach($comments as $comment){
        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial','B',16);
        $pdf->Cell(100,10,$comment['message']);
        $pdf->Output();
    }
}

any idea why is it like this?

RiggsFolly :

Move the initialisation code outside the loop and then the loop will just add cells

if(isset($_GET['show_id'])){
    $id = ($_GET['show_id']);
    $comments = selectAll('proposal_comment', ['proposal_id' => $id]);

    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);

    foreach($comments as $comment){
        $pdf->Cell(100,10,$comment['message']);
    }

    $pdf->Output();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Doctrine only inserts one record after foreach loop

Mysql Foreach from one table to another

While loop showing only one record when i use nested while loop for fetch data from another table

How to search for only one particular record in MySQL table using Java?

How to get only one record from left table against each record from right table

foreign key select only one record from table

forEach loop in JS is only selecting one image from my gallery?

Displaying only one record in the table

Using Only One ForEach Loop

MySQL select only one record from similar per day

php Foreach Loop only extracting last record

Print only numbers from table from database in mysql

MySQL update only one record

How to only print one row from table in a while statement?

How to to display all value from table without one record MySQL

How can I get this query to print a record that only exists in one table?

SQL - Is it possible to print all records or only one record taking into account count(*) in a table?

Foreach loop but print one time with multiple value

How to print data for one MySqL record in Angular

for each loop post only one record with php

Php While loop only displaying one record

Create a table within a foreach loop and then print the same html element (input) for every item of the array with php (using the loop only once)

mysqldump for a table dumps only one record

MYSQLI For calculating people with only one record in table?

Foreach loop processing only one element

php - foreach loop only runs one

Iterating foreach loop only works one time

foreach loop returns only one result of post

Foreach loop returning one letter only