tommy2shoes Posted April 12, 2011 Share Posted April 12, 2011 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. Quote Link to comment Share on other sites More sharing options...
drisate Posted April 12, 2011 Share Posted April 12, 2011 It would look like this <?php while($row = mysql_fetch_array($result)){ $html_center .= $row['id']."<br>".$row['name']."<br>".$row['date']."<br>\n<hr>\n"; } $html = "<html> $html_center <i>This is a test calendar.</i>"; ?> Quote Link to comment Share on other sites More sharing options...
tommy2shoes Posted April 12, 2011 Author Share Posted April 12, 2011 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); Quote Link to comment Share on other sites More sharing options...
drisate Posted April 12, 2011 Share Posted April 12, 2011 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() Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 12, 2011 Share Posted April 12, 2011 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; } Quote Link to comment Share on other sites More sharing options...
tommy2shoes Posted April 12, 2011 Author Share Posted April 12, 2011 That worked great - very many thanks for the ideas. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.