Index.phtml
How do all these fit together? Well, this is probably best shown by building the page, which we are going to use to present the content to our users, namely index.phtml.
1: <? 2: include "constants.inc"; 3: include "mysqldb.obj"; 4: include "genericinfo.obj"; 5: include "linkbox.obj"; 6: include "resultbox.obj" 7: ?> 8: <HTML> 9: <HEAD> 10: <TITLE> 11: <? echo($TITLE); ?> 12: </TITLE> 13: <LINK TYPE="text/css" REL="stylesheet" HREF="main.css"> 14: </HEAD> 15: 16: <BODY BGCOLOR="#FFFFFF"> 17: 18: <TABLE BORDER="0" CELLPADDING="10" CELLSPACING="10"> 19: <TR VALIGN="top"> 20: <TD> 21: <? 22: 23: $db0 = new mysqldb(); 24: $db0->setsql("SELECT tem_team, tem_url FROM team ORDER BY tem_id"); 25: if ($db0->selectquery()) { 26: $lnk = new linkbox(); 27: $lnk->settitle("F1 Teams"); 28: $lnk->data = $db0->result; 29: $lnk->drawlinkbox(); 30: } else { 31: echo("[Error:] Unable to connect"); 32: } 33: 34: ?> 35: </TD> 36: <TD> 37: <? 38: 39: $db1 = new mysqldb(); 40: $db1->setsql(" SELECT 41: CONCAT("<B>",UPPER(driver.drv_surname), " ", driver.drv_forename), 42: SUM(points.pts_teampoints) as totdriverpoints 43: FROM points 44: LEFT JOIN driver ON points.drv_id = driver.drv_id 45: GROUP BY driver.drv_surname, driver.drv_forename 46: HAVING totdriverpoints <> 0 47: ORDER BY totdriverpoints DESC"); 48: 49: if ($db1->selectquery()) { 50: $rst = new resultbox(); 51: $rst->setouterwidth(175); 52: $rst->setinnerwidth(171); 53: $rst->settitle("F1 Drivers Championship"); 54: $rst->data = $db1->result; 55: $rst->drawresultbox(); 56: } else { 57: echo("[Error:] Unable to connect"); 58: } 59: 60: ?> 61: </TD> 62: <TD> 63: <? 64: 65: $db2 = new mysqldb(); 66: $db2->setsql(" SELECT 67: team.tem_team, 68: SUM(points.pts_teampoints) as totteampoints 69: FROM points 70: LEFT JOIN team ON points.tem_id = team.tem_id 71: GROUP BY team.tem_team 72: HAVING totteampoints > 0 73: ORDER BY totteampoints DESC"); 74: 75: if ($db2->selectquery()) { 76: $rst = new resultbox(); 77: $rst->setouterwidth(175); 78: $rst->setinnerwidth(171); 79: $rst->settitle("F1 Constructor's Championship"); 80: $rst->data = $db2->result; 81: $rst->drawresultbox(); 82: } else { 83: echo("[Error:] Unable to connect"); 84: } 85: 86: ?> 87: </TD> 88: </TR> 89: </TABLE> 90: </BODY> 91: </HTML>
Explanation:
Lines 1 – 7 These lines bring in all the files we have previously written, thereby making their contents available. Notice, how the constants.inc file is the first to be included. If it’s not, it’s not going to be global!
Lines 8 – 20 Here we break out into good ol’ HTML, to setup the page and bring in the style sheet.
Lines 21 – 34 Back into PHP to start using the objects.
The first item we are going to create is the F1 Teams box.
The first item we are going to create is the F1 Teams box.
Line 23 Instantiates the mysqldb class, creating an object of type mysqldb; to be referenced by $db0. This will therefore execute the constructor function in the mysqldb class, setting initial values to those set by the global constants in constants.inc.
Line 24 Calls the setsql($req_sql) function to set the sql property on the $db0 object.
Line 25 Is an interesting line. Basically, if the call to selectquery() within the mysqldb object fails (or returns as false), execution skips to line 30.
Assuming line 25 returns true, we have the data! Not so bad eh? Now let’s work with drawing the box.
Line 26 instantiates the linkbox() class creating an object of type linkbox to be referenced by $lnk. This will therefore execute the constructor function of the linkbox class, which in turn executes the constructor function of the parent class (genericinfo).
Line 27 sets the title of the linkbox, by calling the settitle($req_title) of the parent class.
Line 28 makes sure that the data available to the $lnk is the same as that which is currently in the $db0 object.
Line 29 calls the drawlinkbox() function of the linkbox class. Because $lnk now has direct access to all of the data, it is able to complete the drawing of the linkbox.One point to note in the drawlinkbox function is on lines 16 – 22. If the global constant $CSSBOXTITLE is set, then the style will be included, otherwise it won’t.
That’s it! You now have the News box drawn on screen!
Lines 35 – 36 Back into HTML to close the table cell and open a new one.
At this point, you should be able to walk through the remainder of the page to see how the other objects
are drawn.
are drawn.