[Original] How did I teach female students to develop php from scratch (3)

Source: Internet
Author: User
After reading a lot of comments, I said that there is no picture, no truth, and a good HD, no code. In response to many voices, with the consent of my sister, I will send a picture today. Don't worry about disappointment, or say I am a lie. Look at the chat content. In fact, it's easy to make a girl happy. When teaching her to learn something, let her feel more fulfilled and make her feel that she has actually learned it.

After reading a lot of comments, I said that there is no picture, no truth, and a good HD, no code. In response to many voices, with the consent of my sister, I will send a picture today. Don't worry about disappointment, or say I am a lie. Look at the chat content. In fact, it's easy to make a girl happy. When teaching her to learn something, let her feel more fulfilled and make her feel that she has actually learned it.

After reading a lot of comments, I said "no picture, no truth" and "a good HD, no code big image". In response to many voices, with the consent of my sister, I will send a picture today.

Don't worry about disappointment, or say I am a lie. Look at the chat content. In fact, it is easy to make a girl happy. When teaching her to learn something, let her feel more fulfilled and make her feel that she has actually learned something and made progress, in this way, she will naturally get angry and feel that it is good to stay with you as time passes fast. If you say, "You're too stupid, you don't understand it." Or, "you don't know how to teach. I have prepared it for you. Then you can confuse the teacher ", are girls happy? To help people, as long as they put the programmer's thinking about code 1% to girls, there will not always be good people getting stuck or helping people build computers late at night and ride bicycles to drive their homes.

------------------ The chat is over ------------------

Previous review: In article (2), we focus on submitting, obtaining, and processing the data in the submitted form. In the end, I also talked about how to use PHP to operate MySQL and demonstrate how to connect to the database.

Today, we will explain the following two main points: 1. How to interact with a database; 2. Simple MVC Architecture

Step 6: retrieve data from the database

In general, during the login process, we only need to retrieve data from the database and compare the data extracted from the page-verify the user name, password, and permissions without adding, deleting, and modifying the operation. Here, we take the query operation as an example. Other operations, let the girl read the book, and cannot make her a hand-held party.

First, let's take a look at the basic database table that I created in advance. The SQL statement is not provided here, so she can create it by herself. It is quite simple to use with the help of a graphical interface.

After reading the basic table, let's take a look at the PHP code section-Controller. php.

1
 ERROR:
". Mysql_error ()."
SQL statement that causes the problem :". $ SQL); 24 // run the SQL statement. if the result set is returned successfully, the value is assigned to the variable $ result. if the result set fails, run the die statement 25 26 if ($ num = mysql_num_rows ($ result )) 27 {28 while ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC) {29 // while LOOP, each row in the result set is retrieved in a loop and assigned to the array variable $ row30 if ($ row ['id'] = $ user_id & $ row ['Password'] = $ user_password & $ row ['limitation'] = $ user_limitation) {31 // judgment statement, true 32 if ($ row ['limitation'] = $ guest_limitation) only when the username, password, and permission are all true) {33 echo "This is a user page! "; 34} 35 else {36 echo" This is a admin page! "; 37} 38} 39} 40} 41 mysql_close ($ conn); 42?>

So far, we have basically completed the login process, from the login page code, form submission to obtaining and processing form data, at the end, the page is displayed based on whether the verification is successful or not, and the page is displayed based on the permissions.

In the last part of Controller. php, I used an if else statement for judgment. We can write the code into it and display it on different pages. However, the disadvantage is that the Code is squeezed into a PHP file. If it is just a login function, this shortcoming is not very obvious. When the project scale is expanded, we will find that some PHP files will be very bloated and difficult to maintain. In order to let my sister develop good habits, I decided to tell her a simple MVC hierarchy.

Step 7: simple MVC hierarchy

In fact, two articles in my blog are about MVC layering, but they are developed based on java web, but some content can still be moved over.

The full name of MVC is Model View Controller, short for model-view-controller. It is a Model of software design, it is used to organize code by separating business logic from data display.

-- Baidu encyclopedia

In general, in the following explanation, the view shows the page (HTML + CSS + JS + a small amount of PHP code) that the user can see) and submits data to the controller. The controller is the PHP file used by the Controller to control the data interaction process and page Jump. The model is the PHP file used by the Controller to process data, ).

At the beginning of the login process, the login page is our first View (View). The Code is available in front of it and I will not post it. It can be written in pure HTML code. The view submits data to the Controller, that is, our initial Controller. php. After transformation, the new Code is as follows:

1
 Login ($ user_id, $ user_password, $ user_limitation); 11 // call the login method of the $ login object with $ user_id, $ user_password, $ user_limitation as the parameter, the returned value is assigned to $ jugg12 13 if ($ jugg = 0) {// jump to different page 14 headers ('location: http: // localhost/MyMVC/user.html '); 15} 16 else {17 header ('location: http: // localhost/MyMVC/admin.html'); 18} 19?>

Since I wanted to introduce MVC to my classmates at the beginning, the name would be Controller. php at the beginning, and now it is a real name. In the code, I imported the Model. php file and used the login method in Class_Login to process data (verify the account) and return the judgment value. Finally, I jumped to different pages based on different return values. This is the role of the Controller in the login process. It receives data (such as the user name and password) from the view and submits the data to the Model for processing, finally, select different views based on the returned values to jump (in fact, the data returned by the model can be submitted to the View through the Controller for display, not to mention ).

If you understand the Controller, the Model is also easy to understand. See the Model. php code --

1
 ERROR:
". Mysql_error ()."
Problematic SQL: ". $ SQL); 19 20 if ($ num = mysql_num_rows ($ result) 21 {22 echo'
' ;23       while($row=mysql_fetch_array($result,MYSQL_ASSOC)){24           if($row['id']==$user_id&&$row['password']==$user_password&&$row['limitation']==$user_limitation){25             if($row['limitation']==$guest_limitation){26                 return 0;27                 }28             else{29                 return 1;30                 }31            }32         }33    }34            mysql_close($conn);35   }36  }37 38 ?>

I will not post the code of user.html#admin.html for the two view(view images ~

At half past one, she probably won't be too clear about the meaning of this, and she will understand it later in the project.

---------------------- Don't laugh, I am still the split line ----------------------

After writing a blog for several days in a row, I feel more and more rewarding. Thanks to all the friends who intentionally or unintentionally follow this series of blogs, without your reading and comments, I absolutely cannot write these words as enthusiastically as I do now. In addition, I am very pleased that my sister is very interested in learning and often tells me that it feels very fast. In fact, my sister has no plans to take the technical route in the future. After completing the practical training tasks specified by this school, she will be put into preparation for the postgraduate entrance exam. So she can do this. As a "technical consultant", I really have no more to say.

I was a senior student in Beijing, and I was in Guangzhou, one day south and one place north, so you don't know anything about it. Finally, let me reveal that I have taken a photo of her side face, and I'm wondering if I want to blow it out.

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.