PHP implementation of MVC mode (2)

zhaozj2021-02-16  48

example

Here is a simple example of using the MVC mode.

First we need a database access class, which is a normal class.

db = mysql_pconnect ($ HOST, $ User, $ Pass); mysql_select_db ($ db, $ THIS -> dB);} //! an accessor / ** * fetches a query resources and stores it in a local member * @Param $ sql string the database query to run * @return void * / function fetch ($ sql) { $ this-> query = mysql_unbuffered_query ($ SQL, $ this-> dB); // Perform Query Here} //! an accessor / ** * Returns An Associative Array of A Query Row * @Return Mixed * / Function GetRow () {IF ($ row = mysql_fetch_Array ($ this-> query, mysql_assoc) RETURN $ ROW; Else Return False;}}?>

Place the model on it.

dao = & $ dao;}! // A manipulator / ** * Tells the $ dboject to Store this query as a resource * @Param $ start the row to start from * @Param $ rows the number of rows to fetch * @return void * / function listproducts ($ start = 1, $ rows = 50) {$ this- > DAO-> FETCH ("SELECT * FROM PROMITS LIMIT". $ start. "," $ rows);} //! a manipulator / ** * tells the $ dboject to store query ask a resource * @Param $ ID A Primary Key for a Row * @Return Void * / Function ListProduct ($ THIS-> DAO-> FETCH ("Select * from products where produter. $ ID." '";} / /! A manipulato r / ** * Fetches a product as an associative array from the $ dbobject * @return mixed * / function getProduct () {if ($ product = $ this-> dao-> getRow ()) return $ product; else return false One thing to note is that between the model and data access classes, their interactions will never be more than one line - no multi-line is transmitted, so it will slow down. The same program For the use of the mode, it only needs to keep a line (ROW) - other handed Query Resource - in other words, we let MySQL keep our results for us.

Next is the view - I have dropped HTML to save space, you can view the full code of this article.

? model!. = & $ Model;} //! A manipulator / ** * builds the top of an html page * @return void * / function header () {} //! A manipulator / ** * builds the bottom of an HTML Page * @Return void * / function footer () {} //! a manipulator / ** * displays a single product * @return void * / function productItem ($ ID = 1) {$ this-> model-> listproduct ($ ID); while ($ product = $ this-> model-> getProduct ()) {// bind data to html}} ! // A manipulator / ** * Builds a product table * @return void * / function productTable ($ rownum = 1) {$ rowsperpage = '20 '; $ this-> model-> listProducts ($ rownum, $ rowsperpage) WHILE ($ product = $ this-> model-> getProduct ()) {// bind data to html}} //! An accessor / ** @ Returns the rendered html * @return string * / function display () { Return $ this-> output;}}?> PS: Original is a post; but the dead activities can't come up --________- bless

Part1 http://www.9cbs.net/develop/read_article.asp?id=21639

Part2 http://www.9cbs.net/develop/read_article.asp?id=21640part3 http://www.9cbs.net/develop/read_article.asp?id=21641

转载请注明原文地址:https://www.9cbs.com/read-25646.html

New Post(0)