Projects and new requirements:
We have a PHP-written webmail system, there is a mail_list.php used to show the user's mailing list this page supports the FolderID parameter (because the message is present in a different folder) because the message is too many to support page flipping. Now you need to add tags to your system. Refinement: Need to add the label column in the original mailing list, and support TagID to retrieve a mailing lists (also add tag columns in the list)
Code and new requirements
mail_list.php Call the Getmaillist function, this function passes in the folder ID, page number, page size, get HTML table and navigation link
Getmaillist using the T_page class to get the requested page of data processed as an HTML table
Although T_page only supports single-table queries, it is widely used throughout the system.
Because the t_page is still risky, it is best to inherit it and then instantiate the subclass directly where multiple table queries are required
Before refactoring:
/*function: Query a mailing list for a specific page under a specified folder (HTML Table)*/functionGetmaillist ($folderId,$pageNo,$pageSize){ Global $db _obj,$pageLink,$html;//Db_obj is used from an external import, and the latter two seem to be directed out $query= "Where FolderID =$folderId...";//Omit many lines $pageclass=NewT_page; $tableName= "Mail"; $condition=$query. "ORDER BY TM Desc"; $pageclass->baseset ($tableName, "Id,sender,subject,content,...",$pageNo,$pageSize,$db _obj); $pageclass->setcondition ($condition); $record=$pageclass->readlist ();//the recordset that was found is placed in a two-dimensional array. $pageLink=$pageclass->page ();//Array to hold page-flipping buttons $html= ""; for($ipage= 0;$ipage<$pageSize;$ipage++) { $i=$ipage; $id=$record[$i["id"]; $sender=$record[$i[Sender]]; $subject=$record[$i["Subject"]; $content=$record[$i["Content"]]; $html= "<table>"; $html=$html. "<tr><td><input type=\" checkbox\ "value=\"$id\ "></td>"; $html=$html. "<td>$subject</td> "; ...$html=$html. "</table>" }}/*function: Query "one page" record, and output navigation link is used for page flipping*/classt_page{var $TableName; ...functionBaseset ($TableName,$Fields,$PageNo,$PageSize,$OBJ _db) { $this->tablename =$TableName; ...$this->maxline =$this-PageSize; $this->offset =$this->pageno *$this-PageSize; } functionSetcondition ($Condition) { $this->condition =$Condition; } functionreadlist () {$SQL= "SELECT count (*) as recordtotal from$this->tablename$this->condition "; $result=$this->obj_db->simplequery ($SQL); $row=$this->obj_db->fetchrow ($result,DB_FETCHMODE_ASSOC); $this->total =$row["Recordtotal"]; if($this->total > 0) { $SQL= "Select$this->fields fom$this->tablename$this->condition limit$this->offset,$this->maxline; "; $result=$this->obj_db->simplequery ($SQL); $this- Number=$this->obj_db->numrows ($result); while($row=$this->obj_db->fetchrow ($result,Db_fetchmode_assoc)) { $this->result[]=$row; } $this->obj_db->freeresult ($result); } return($this-Result)} functionPage () {if($this->pageno > 0) $this->pagelink[0] = "<a ...> prev </a>"; Else $this->pagelink[0] = "<a> prev </a>"; ...return $this-Pagelink; } //More function}
After refactoring
classMailpageextendst_mypage{functionsettotal () {...$this->total =$row["Recordtotal"]; } functiongetmaillist () {$Fields=$this->field. "," as TagList "; $SQL. = "Select$FieldsFrom$this->table$this->condition "; $SQL. = "LIMIT$this->offset,$this->maxline "; ... } functionaddtaglist () {$tagList=Array(); for($i= 0;$i<$this- Number;$i++) { $key=$this->result[$i["id"]; $this->result[$i["taglist"] =$tagList[$key]; } } //overrides the interface of the parent class functionreadlist () {$this-settotal (); $this-getmaillist (); $this-addtaglist (); return $this-Result; } }classTagpageextendsmailpage{functionSetcondition ($taglist) { ...$this->condition =sprintf("Where ID in (%s) is ORDER BY TM Desc",$mailList); }}
One-time refactoring record for PHP programs