To better demonstrate the way MVC works, we use a simple newsArticleThe publishing system can be used as an example in two ways: MVC and MVC.
We will only give a basic demonstration, read some article lists from the database, and display them on the page. The general process is to connect to the database, query the database, and output HTML results cyclically. The followingCodeThat's what we do. (Fresh water feels strange, and the language organization is poor. Fortunately, the code is easier to understand)
PHP code
-
- <? PHP
-
- Mysql_connect (...);
-
- $ Result= Mysql_query ('Select * from news order by article_date DESC');
-
- ?>
-
- <HTML>
-
- <Body>
- <H1> news articles
-
- <? PHPWhile($ Row= Mysql_fetch_object ($ Result) {?>
-
- <H2> <? PHPEcho $ Row-> Headline?> </H2>
-
- <P>
- <? PHPEcho $ Row-> Body?>
-
- </P>
-
- <? PHP }?>
-
- </Body>
-
- </Html>
Use MVC.
Model:
PHP code
-
- <? PHP
-
- FunctionGet_articles ()
- {
-
- Mysql_connect (...);
-
- $ Result= Mysql_query ('Select * from news order by article_date DESC');
-
- $ Articles=Array();
-
- While($ Row= Mysql_fetch_objects ($ Result)){
-
- $ Articles[] =$ Row;
- }
-
- Return $ Articles;
-
- }
-
- ?>
Controller:
PHP code
- <? PHP
- $ Articles= Get_articles ();
- Display_template ('Articles. tpl');
- ?>
View:
PHP code
-
- <HTML>
-
- <Body>
-
- <H1> news articles
- <? PHPForeach($ Articles As $ Row) {?>
-
- <H2> <? PHPEcho $ Row-> Headline?> </H2>
-
- <P>
-
- <? PHPEcho $ Row-> Body?>
- </P>
-
- <? PHP }?>
-
- </Body>
-
- </Html>
Please forgive me for not translating well. Original: http://reinholdweber.com /? P = 16