1. First defines a word class
<?phpclass word{function Start () {Ob_start (); ob_start- Open output control buffer } function Save ($path) {$data = Ob_get_contents (); ob_get_contents- Returns the contents of the output buffer Ob_end_clean (); ob_end_clean- empties (erases) the buffer and closes the output buffer $this->wirtetoword ($path, $data); } function Wirtetoword ($FN, $data) {$fp =fopen ($FN, "w"); fopen- Open a file or URL, the second parameter is how to open fwrite ($fp, $data); fwrite- Write file (can be used safely for binary files) fclose ($FP); fclose- Close an open file pointer }}?>
2. Refer to the class and invoke the data from the nation table from the database
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8" /> <title>Document</title></Head><Body> <?php if (!isset ($_get["id")) {?> <inputtype= "button"name= "Submit"value= "Save table contents to Word"onclick= "window.location.href= ' main.php?id=print '"> <?php}?> <?php if ($_get["id"]!= "") {include ("word.class.php"); Reference the Word class in $word =new Word (); Instantiate the class $word->start (); Call the Start () method to define the beginning of the table to save}?> <TableBorder= "1"cellspacing= "0"cellpadding= "0"> <TR><th>Code</th><th>Name</th></TR> <?php include (".. /gongju/dbda.class.php "); $db =new Dbda (); $sql = "SELECT * From Nation"; $attr = $db->query ($sql); foreach ($attr as $v) {echo "<tr><td>{$v [0]}</td><td>{$v [1]}< /td></tr> "; } ?> </Table> <?php if ($_get["id"]!= "") {$word->save ("Data.doc"); Save Table End}?> </Body></HTML>
PHP Action: Saves the data in the database to Word.