- MVC is the architectural pattern. That is used to separate an application into three main component: The model, the view, the controller.
- The Model: Model objects are the parts of application that implement the logic for the application's data domain. Often model objects retrieve and store model state in a database.For example, a Product object can retrieve information from database, operate on it, and then update information back to product table in SQL server.
- The View: Views are the components that display the application's user interface(ui). Typically,UI is created from the model data.
- The Controller: Controllers are the components that handle user interaction. working with model and ultimately,select a view to reder.That display UI. In an MVC application, View only display information for end user.Controller handle and response to user input and interaction.
- Function is a reusable block of code.
- To write a function, code the function keyword followed by name of the function, followed by a set of parentheses.
- within the parentheses of a function, you can code an optional paramater list that contain on or more parameters.
- To code a function that returns data, code the return statement in the body of function. The return statement ends the execution of the function and return the specified value.
- When you call a fucntion, the argument in the argument list must be in the same order as the parameters in the parameter list defined by the function, and they must have compatible data types.
- Example: function avf_of_3($x,$y,$z){ return ($x+$y+$z)/3; }
- How to pass arguments by value and by reference in PHP
- By default, all argument are passed by value to the function that are called. This means that PHP send a copy of argument(the value of original variable) to the function, not the argument itself. As the result, the function can't change the value of orginal argument.
- To allow a function to modify the original argument insted of a copy, You can can code an ampersand(&) in font of the parameter. In that case, The argument is passed by reference. This mean that a reference to the original argument is sent to the function. Then, when the function changes the parameter, it actually changing the original argument.
This topic show you how to use PDO (PHP Data Object) to work with database.
- How to connect to a MySQL database.
- Creating a database object from PDO class.
- example: new PDO($dsn,$username,$password),
- Syntax for creating a data source name(DSN) for MySQL database.
- mysql:host=host_address;dbname=yourdatabase.
- For instance:Connect to a MySQL database named bookshop.
- $dsn='mysql:localhost;dbname=bookshop'
- $username='example'
- $pass='example'
- $db=new PDO($dsn,$username,$pass)
- How to execute SELECT statement.
- Once you have created PDO object you can use it's methods to execute SQL statements.To execute a SELECT statement, you use query method of PDO object.
- A method of PDO class for executing a SELECT statement.
- query($select_statement).
- For instance:
- $query='SELECT *FROM Product';
- $product=$db->query($query).
- How to INSERT and UPDATE and DELETE statements.
- A method for modifing the database.
- For example:
- $query='INSERT INTO Product VALUES ($id, '$name', '$description' )'.
- $db->exec($query).
- What is a function?
- A function is a block of code that can reusable.
- What is a build-in function?
- PHP provides many functions that you can use to work with data. The functions are refered to as build-in functions.
- Some of basic build-in function:
- number-format($number[,$decimal]): this function return number that is formatted with a comma(,)
- Exmple:$nf=number-format(12345) //12.345
- Example:$nf=number-format(12345,2) //12,345
- date($format): this function return current date with specificed format string.
- Example: $date=date('Y-m-d') // 2014/13/11
- Three functions for checking variable values.
- isset($var): return true if the variable have been set and is not null value.
- empty($var): return true if the variable hasn't been set ,contain null value or contain an empty string.
- Build-in functions that you can use to pass control to another page.
- As a PHP application executes, it moves from one web page to another. To do that, you can use the include and require function.
- include($path):Insert and run the specified file. If function fail ,it cause a warning,that allow script to continue.
- include_once($path): Same as the include function. But it make sure that the file include only once.
- require($path): Works the same include function. However, if function fails it causes a fatal error that stop script.
- require_once($path): Same as require function. But it make sure that the file include only once.
- exit($status): Exits the current PHP script.
- How to use the build-in $_GET array.
- When an HTML <form> tags uses the GET method to pass data to a php page, the values is stored in a $_GET array. Beacause this array is a part of PHP, it is called build-in array.
- Beacause this array uses the GET method, these values is passed as a part of the URL that request the pages.
- For example: detail.php?firstname=tien&lastname=dung.
- To retrieve values form $_GET array, you code as following:
- For example: $firstName=$_GET['firstname'].
- How to use the build-in $_POST array.
- When an HTML <form> tags uses the POST method to pass data to a php page. so passed data will be stored in $_POST array.
- The $_POST array works like $_GET array.
- So when should use the GET method and when should use the POST method.
- Using GET method:
- when request is for a page that gets data from a database server.
- when request can be excuted multiple times without causing any problem.
- Using POST method :
- when the request that write data to a database server.
- When excuteing the request multiple times that may cause problems.
- When you don't want to include the parameters in URL.
- When you want to transfer more than 4 KB of data.
- Data types of PHP.
- PHP provides for six data types. Each data type is used to store a different type of data.
- An integer is a whole number that can start with positive or nagative sign.
- A double value consists of a positive and nagative sign,digits, an optional decimal point, and optional decimal digist.
- A boolean data type is used to represent a boolean value that is either True or False.
- A string data type contains string that are made up of any characters.
- An array data type is used to store one or more items called elements.Each element stores a value that you can refer to with an index.
- An object data type.
- Declaring variables in PHP.
- Variable stores a value that can change as the program executes. In php, variables are easy to spot beacause they all begin with dollar sign ($).
- To declare a variable, code the dollar sign ($) followed by the variable name. Then, to assign a value to the variable, code assignment operator (=) followed by an expression that return value for the variable.
- Example: $count=10; or name='nguyen tien dung'.
- Declaring constants in PHP.
- Constants like variable except that once they are declared they can't change or undefined.
- Example: define('pi',3,14).
- How to embed PHP in HTML.
- To add PHP to a file, you code php tag that begins with <?php and end with ?>. Then you enter php code between these tags.
- How to code comments in php.
- Comment helps document what code does.
- To code a single-line-comment, code two forward slashes (//) or a pound sign (#).
- To code a multiple-line-comment , code /* flowed by the comment,followed by */
- Comment make your code easer to read.
- The definition of dynamic web page.
- A dynamic web page is a web page that is generated by a server-side program or script that is running on a server. Often,the web pages changes according to the information that is submited by the web browser to the server.
- How a web server processes a dynamic web page with PHP.
- When a web server receives a request for a dynamic web page, it uses the extension of the requested file to determine which server or program should process the request. If the extension is php, the web server calls php interpreter to process the request and the data that is submitted with the request.
- The php page can use the data that gets from the web browser to access the appropriate data from database server. The application can also store the data that it gets from the web browser into the database server.
- When php interpreter finishes processing the php page, it generates an HTML page and return it to web server. The web server then returns the page to the web browser.
- The defintion of static web page.
- Static web page is an HTML document that is stored on the server web and doesn't change in respone to user input.In other words,the content of web pages only can be change by web developers.Static web page is a plain text file with an extension of htm or html.
- How Static web pages are processed.
- When user requests a static web page,the browser send a HTTP request to the web server that includes the names of the file that is being requested. When the web server receive the request, it retrieves the web page and send it back to the web browser in an HTTP response. This response includes the HTML document that is stored in the file that was requested.
- Note:
- HyperText Markup Language (HTML) is the language that is used to design web pages of an application.
- Two protocols that web application depend upon.
- HyperText Transfer Protocol (HTTP) is the protocol the web browsers and web servers use to communicate. It sets the spefications for HTTP requests and responses.
- Transmission Control protocol/ Internet Protocol(TCP/IP) is suite of protocols that let two computers communicate over a network.