Jump to content

TCPDF and Loop


tommy2shoes

Recommended Posts

Hi

 

I am trying to work out how to add a loop statement to my pdf generation (I think that's what I need).

 

I have a mysql table - events with 3 fields - id, name and date.

 

In my php page which is to generate the pdf I have:

 

mysql_select_db($database_events, $events);

$result = mysql_query("SELECT  id, date, name

                    FROM events_pbw

                    WHERE id>200

                    ");

 

while($row = mysql_fetch_array($result))

  {

    $id = $row['id'];

    $date = $row['date'];

    $name = $row['name'];

}

 

 

and in amongst the $html part of the page I have:

 

$html ="<html>

 

$id<br>

$name<br>

$date<br>

 

<i>This is a test calendar.</i>

 

This all works fine but (obviously) just gives one result. What do I need to add to make it list out all of the possible events?

 

Many thanks in advance.

Link to comment
Share on other sites

Thank you but I get an error - Undefined variable: html_center on line 45

 

Line 45 is:

    $html_center .= $row['id']."<br>".$row['name']."<br>".$row['date']."<br>\n<hr>\n";

 

This part of my code is:

 

$result = mysql_query("SELECT  id, date, name

                    FROM events

                    WHERE id>200

                    ");

 

while($row = mysql_fetch_array($result))

  {

    $html_center .= $row['id']."<br>".$row['name']."<br>".$row['date']."<br>\n<hr>\n";

}

 

 

// create new PDF document

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

Link to comment
Share on other sites

It means that your Querry:SELECT  id, date, name FROM events WHERE id>200

Is returning nothing ...

 

Try SELECT  * FROM events

 

If that works it meand that your id>200 is returning nothing. If it does not work it means your events table is empty.

 

If you wana me sure you don't get that error in the future you can test it with isset()

Link to comment
Share on other sites

You're trying to concatenate data to a variable that hasn't been initialized, thereby generating an 'Undefined Variable' warning. Initialize the variable as an empty string before trying to concatenate to it. Alternately, use an array to hold the values, and loop through it (or implode() it).

 

$html_center = '';
while($row = mysql_fetch_array($result))
  {
       $html_center .= {$row['id']}<br>{$row['name']}<br>{$row['date']}<br>\n<hr>\n";
}

 

OR

 

$html_center = array();
while($row = mysql_fetch_array($result))
  {
       $html_center[] = "{$row['id']}<br>{$row['name']}<br>{$row['date']}<br>\n<hr>\n";
}

// to echo later
foreach( $html_center as $v ) {
     echo $v;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.