In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
(1) first of all, prepare to download the latest version of the thinkPHP framework and extract it to the location of the project you are going to develop. The operation demonstration is shown in the figure:
Where index.php is the entry file, that is, all requests must go through this file before it can be completed. Application is the application directory, and during the development process, all the files we need to manipulate manually are in this folder. Public is the resource file directory thinkPHP is the frame directory
1JI index.php entry file
2, what is the entry file, that is, all functional operations can only be completed through this file.
The code is as follows:
Where the $connection array defines the database link information, and the $tablePrefix defines an empty prefix.
3. The controller is actually a class, made up of methods. For example, TODOlist, I have defined five methods. To omit the specific code in the method, take a look at the architecture first:
In the view module, a controller (a class) corresponds to a folder, the name of the folder is the name of the class, and the name of this class is Index, then the name of the folder is Index, and then in this folder, each method corresponds to a html file, which is the template file for the method. For example, the index method corresponds to the index.html template file.
4.
Public function index () {
$title= Welcome Page
$bodytitle= Welcome to todolist
$this- > assign ("bodytitle", "$bodytitle")
$username=session ("username")
If (empty ($username)) {
$login=U ('Home/Index/login')
$register=U ('Home/Index/register')
$this- > assign ("link1", "login")
$this- > assign ("link2", "register")
} else {
$logout=U ('Home/Index/logout')
$changepassword=U ('Home/Index/changepassword')
$planlist=U ('Home/Index/planlist')
$this- > assign ("link1", "logout")
$this- > assign ("link2", "change password")
$this- > assign ("link3", "your list")
}
$this- > display ()
}
Public function register () {
$user=D ("usermessage")
$error=array ()
If ($_ SERVER ['REQUEST_METHOD'] = =' POST') {
If (! empty ($_ POST ['username'])) {
$data ['name'] = $_ POST [' username']
} else {
$error [] = "Please enter a user name"
}
If (! empty ($_ POST ['password'])) {
$data ['password'] = $_ POST [' password']
} else {
$error [] = "Please enter your password"
}
If (empty ($error)) {
$ringing username-> where ("name=' {$data ['name']}'")-> select ()
If (empty ($r)) {
$r1roomusername-> add ($data)
If ($R1) {
$this- > assign ("error", "Congratulations, successful registration")
} else {
$this- > assign ("error", "system is busy, please register again")
}
} else {
$this- > assign ("error", "Sorry, this user name is already registered")
}
} else {
Foreach ($error as $msg) {
$this- > assign ("error", "$msg")
}
}
}
$this- > display ()
}
Public function login () {
$user=D ("usermessage")
$user- > create ()
$error=array ()
If ($_ SERVER ['REQUEST_METHOD'] = =' POST') {
If (! empty ($_ POST ['username'])) {
$username=$_POST ['username']
} else {
$error [] = "Please enter a user name"
}
If (! empty ($_ POST ['password'])) {
$password=$_POST ['password']
} else {
$error [] = "Please enter your password"
}
If (empty ($error)) {
$ringing username-> where ("name='$username'")-> select ()
If (! empty ($r)) {
$r1roomusername-> where ("name='$username' and password='$password'")-> find ()
If (! empty ($R1)) {
$this- > assign ("error", "login successful")
$this- > assign ("error1", "{$R1 ['user_id']}")
Session ("username", "$username")
Session ('user_id',$r1 [' user_id'])
} else {
$this- > assign ("error", "password error, please re-enter")
}
} else {
$this- > assign ("error", "Sorry, this user name is not registered")
}
} else {
Foreach ($error as $msg) {
$this- > assign ("error", "$msg")
}
}
}
$this- > display ()
}
Public function logout () {
$username=session ('username')
If (! empty ($username)) {
Session (null)
$username=session ('username')
If (empty ($username)) {
$this- > assign ('logout',' logged out successfully')
} else {
$this- > assign ('logout',' logout failed')
}
} else {
$this- > assign ('logout',' you are not logged in')
}
$this- > display (login)
}
Public function changepassword () {
$error=array ()
$user=D ('usermessage')
$username=session ('username')
$user_id=session ('user_id')
If (! empty ($username)) {
If ($_ SERVER ['REQUEST_METHOD'] = =' POST') {
If (! empty ($_ POST ['newpassword'])) {
$data ['password'] = $_ POST [' newpassword']
/ / $user- > password==$_POST ['newpassword']
} else {
$error [] = "Please enter your password"
}
If (empty ($error)) {
$ringing username-> where ("user_id='$user_id'")-> save ($data)
$this- > assign ("error2", "$r")
If ($r) {
$this- > assign ("error", "Congratulations, password changed successfully")
} else {
$this- > assign ("error", "system is busy, please change your password again")
}
} else {
Foreach ($error as $msg) {
$this- > assign ("error", "$msg")
}
}
}
} else {
$this- > assign ('error',' please log in first')
}
$this- > display ()
}
Public function planlist () {
$username=session ('username')
$user_id=session ("user_id")
$this- > assign ("username", "$username")
/ / $user=D ("usermessage")
$list=D ("listmessage")
$list- > create ()
If ($_ SERVER ['REQUEST_METHOD'] = =' POST') {
If ($_ POST ['orderhidden'] = =' a') {
If (! empty ($_ POST ['content'])) {
$data ['content'] = $_ POST [' content']
$data ['user_id'] = $user_id
$ringing lists-> add ($data)
If ($r) {
$this- > assign ("message", "added successfully")
} else {
$this- > assign ("message", "add failed")
}
} else {
$this- > assign ("message", "Please enter content")
}
}
If ($_ POST ['orderhidden'] = =' b') {
$list_id=$_POST ['listhidden']
$ringing lists-> where ("list_id='$list_id'")-> delete ()
If ($r) {
$this- > assign ("message", "deleted successfully")
} else {
$this- > assign ("message", "Please click again to delete")
}
}
}
$ringing lists-> field ("content,list_id")-> where ("user_id='$user_id'")-> select ()
/ * if (! empty ($r)) {
$num++
}
This- > assign ("num", $num); * /
$this- > assign ("select", $r)
$this- > display ()
}
His template file index.html
{$title}
{$bodytitle}
{$link2}
{$link3}
Register.html
Login page
User name:
Password:
Please consist of alphanumeric or underscores with a length of 5-21
Return to the home page
Log in
{$error}
Login.html
Login page
User name:
Password:
Change password
Return to the home page
Logoff
{$error}
{$logout}
{$error1}
Changepassword.html
Change password page
New password:
Log in
Return to the home page
{$error}
{$error1}
{$error2}
Planlist.html
Schedule list
{$username} Welcome to your plan list
{$message}
{$list.content}
Content:
Change password
Return to the home page
Logoff
5. The following is to explain the knowledge points in the above code: 1) dynamic generation of URL: using the U method built in thinkPHP. When I click on different links, the location of each module is changing, so the fixed path is not feasible, the path must be changeable. In the controller, the U method is used as follows:
$planlist=U ('Home/Index/planlist')
$this- > assign ("link3", "your list")
In the template file, the U method is used as follows
Logoff
1. Summarize it as U (module / controller / method). If the module name is omitted, it defaults to the current module name
2. Instantiate the model class: use D method or M method
$user=D ("usermessage")
$list=D ("listmessage")
3) CURD operation and coherent operation method
(1) create method: used to create data objects, the most typical application is to automatically create data objects based on form data, but the data objects are stored in memory and are not actually written to the database. You know to use add or save methods.
(2) save method:
$user- > where ("user_id='$user_id'")-> save ($data)
The return value is the number of rows of records affected
The return value is the automatically growing primary key, that is, an integer data
(3) select method: used for data reading, that is, obtaining multiple rows of records in the data table
$user- > where ("name=' {$data ['name']}'")-> select ()
If there is an error in the query, the return value is false. If the query result is empty, null is returned. Otherwise, a two-dimensional array is returned.
(4) delete method: used for data deletion
$list- > where ("list_id='$list_id'")-> delete ()
The return value is the number of rows of records deleted
These four operations, called CURD operations, are basic operations on the database.
(5) data method: used to set the value of the current data object to be operated on
$data ['password'] = $_ POST [' password']
(6) add method is used for data writing.
$user- > add ($data)
(7) where method, which is mainly used to add restrictions on database operations.
$user- > where ("user_id='$user_id'")-> save ($data)
$r1roomusername-> where ("name='$username' and password='$password'")-> find ()
(8) field method: select the field to return or operate on
$list- > field ("content,list_id")-> where ("user_id='$user_id'")-> select ()
4) setting and deletion of session
Session ("username", "$username")
Session ('user_id',$r1 [' user_id'])
Session (null)
5) template file: thinkphp has a built-in XML-based template engine thinktemplate, is the same as smarty template engine in variable output and formulation of template files
$this- > assign ()
$this- > display ()
I also used a circular tag foreach for the output of the plan list
The variable output in the method: $this- > assign ("select", $r)
Circular output in a template file
{$list.content}
Programmer Hangzhou Software Test Hangzhou APP Development Hangzhou PHP engineer
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.