example
Here is a simple example of using the MVC mode.
First we need a database access class, which is a normal class.
php / ** * a simple class for querying mysql * / class dataAccess {/ ** * private * $ db stores a database resource * / var $ db; / ** * private * $ query stores a query resource * / var $ query; // Query resource // A constructor / ** * Constucts a new DataAccess object * @param $ host string hostname for dbserver * @param $ user string dbserver user * @param $ pass string dbserver user password *!. @Param $ DB STRING DATABASE NAME * / FUNCTION DATAACCESS ($ HOST, $ User, $ Pass, $ db) {$ this-> 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.
php / ** * fetches "Products" from the database * / class productmodel {/ ** * private * $ DAO an instance of the dataaccess class * / var $ dau; //! a constructor. / ** * ConstuCTS a new ProductModel object * @param $ dbobject an instance of the DataAccess class * / function ProductModel (& $ dao) {$ this-> 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.