Saturday, November 29, 2014

MVC Architectural Pattern


  • What is the MVC Pattern?
    • 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.

Monday, November 24, 2014

How To Create And Use Function In PHP


  • What is the fucntion?
    • 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.

Thursday, November 13, 2014

PHP For Working MySQL

              

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.
      • exec($sql_statement).
    • For example:
      • $query='INSERT INTO Product VALUES ($id, '$name', '$description' )'.
      • $db->exec($query).

Build-IN Functions


  • 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.

Getting Data From A Request


  •  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.

Saturday, November 8, 2014

PHP Data Types


  • 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).

Friday, November 7, 2014

Basic PHP Skills


  • 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.