Introduction Design Method of MVC separation in PHP

Source: Internet
Author: User
Tags vars

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
  1. <? PHP
  2. Mysql_connect (...);
  3. $ Result= Mysql_query ('Select * from news order by article_date DESC');
  4. ?>
  5. <HTML>
  6. <Body>
  7. <H1> news articles
  8. <? PHPWhile($ Row= Mysql_fetch_object ($ Result) {?>
  9. <H2> <? PHPEcho $ Row-> Headline?> </H2>
  10. <P>
  11. <? PHPEcho $ Row-> Body?>
  12. </P>
  13. <? PHP }?>
  14. </Body>
  15. </Html>

Use MVC.

Model:

PHP code
  1. <? PHP
  2. FunctionGet_articles ()
  3. {
  4. Mysql_connect (...);
  5. $ Result= Mysql_query ('Select * from news order by article_date DESC');
  6. $ Articles=Array();
  7. While($ Row= Mysql_fetch_objects ($ Result)){
  8. $ Articles[] =$ Row;
  9. }
  10. Return $ Articles;
  11. }
  12. ?>

Controller:

PHP code
    1. <? PHP
    2. $ Articles= Get_articles ();
    3. Display_template ('Articles. tpl');
    4. ?>

View:

PHP code
  1. <HTML>
  2. <Body>
  3. <H1> news articles
  4. <? PHPForeach($ Articles As $ Row) {?>
  5. <H2> <? PHPEcho $ Row-> Headline?> </H2>
  6. <P>
  7. <? PHPEcho $ Row-> Body?>
  8. </P>
  9. <? PHP }?>
  10. </Body>
  11. </Html>

Please forgive me for not translating well. Original: http://reinholdweber.com /? P = 16

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.