PHP and MySQL Web Development - P88: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | Drawing Figures and Graphing Data 407 Listing Continued exit get current results of poll regardless of whether they voted query select from poll_results if result @mysql_query query db_conn echo Could not connect to db br exit num_candidates mysql_num_rows result calculate total number of votes so far total_votes 0 while row mysql_fetch_object result total_votes row- num_votes mysql_data_seek result 0 reset result pointer Part 1 shown in Listing connects to the MySQL database updates the votes according to what the user typed and gets the new votes. After we have that information we can begin making calculations in order to draw the graph. Part 2 is shown in Listing . Listing Part 2 Sets Up All the Variables for Drawing Initial calculations for graph set up constants putenv GDFONTPATH C WINNT Fonts width 500 width of image in pixels - this will fit in 640x480 left_margin 50 space to leave on left of image right_margin 50 ditto right bar_height 40 bar_spacing bar_height 2 font arial title_size 16 point main_size 12 point small_size 12 point text_indent 10 position for text labels on left set up initial point to draw from x left_margin 60 place to draw baseline of the graph 408 Chapter 19 Generating Images Listing Continued y 50 ditto bar_unit width- x right_margin 100 one point on the graph calculate height of graph - bars plus gaps plus some margin height num_candidates bar_height bar_spacing 50 Part 2 sets up some variables that we will use to actually draw the graph. Working out the values for these sorts of variables can be tedious but a bit of forethought about what you want the finished image to look like will make the drawing process much easier. The values we use here were arrived at by sketching the desired effect on a piece of paper and estimating the required proportions. The width variable is the total width of the canvas we will also set up the left and right margins with left_margin and right_margin