PHP implementation of MVC mode (2)

zhaozj2021-02-16  92

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.

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

New Post(0)