Tag Archives: Source Code

Angular 2 Tutorial – movies App

Last time I wrote a blog created the MovieAPI and now I am using that API as back end services to create an angular 2 app.

the final source code could find at my github repository.  here is the link download

For the MovieAPI open it, build it will install the packages automatically, then run the service first.

After the service is done, go to the angular2 folder, open command window

–run npm install,

— npm start

It will look like this:




Step 1 : Setting up your angular 2 application, I already have a blog  click here to found it. Or also could download from the source file from https://github.com/angular/quickstart/blob/master/README.md but it add a lot of unnecessary setting, probably confuse for beginning learner.

It was better do few times manually setting it up to get understand all of the necessary for start anguar 2 application.

Step 2:  In angualr 2 everything is component, for me normally like to create the service first. I just create a file named movies.service.ts under folder movies

import {Injectable} from 'angular2/core';
import {IMovie} from './movie';
import {Http, RequestOptions , Response, Headers} from 'angular2/http';
import {Observable} from 'rxjs/Observable';

export class MovieService {   
    private _movieUrl = 'http://localhost:53871/api/movies';

    constructor(private _http: Http) { }

    getMovies(): Observable<IMovie[]> {
        return this._http.get(this._movieUrl)
            .map((response: Response) => <IMovie[]>response.json()) //arrow function, tranceform response to json object and is array of IMovie[]
            .do(data => console.log("All: " + JSON.stringify(data))) //for debug

    createMovie(movie: IMovie): Observable {
        let body = JSON.stringify(movie); 
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });

        return this._http.post(this._movieUrl, body, options)               

    private extractData(res: Response) {
        let body = res.json();
        return body.data || {};

    private handleError(error: Response) {
        return Observable.throw(error.json().error || 'Server error');

In there MovieService I have a IMovie here is my movie.ts I put it in the same folder with MovieService .

//interface won't compile to js file, it just for typescript do type checking
 export interface IMovie { 
    movieId: number;
    title: string;
    genre: string;
    classification: string;
    releaseDate: number;
    rating: number;
    cast: string[];


export class Movie implements IMovie {
    movieId: number;
    title: string;
    genre: string;
    classification: string;
    releaseDate: number;
    rating: number;
    cast: string[];

Step 3: Inject the services.
Go to the app.component.ts this is our container component, for anguar 2 only need inject once, then all of the nest container could using it ( by import it ).

Inject a service has two steps:

1) import the file into the app.component,

2) Inject it as providers

here is my app.component.ts


Step 4: Create the movies-list.component.ts

//for using component, simply import it into this file
import {Component, OnInit} from 'angular2/core';
import { ROUTER_DIRECTIVES } from 'angular2/router';

import {IMovie} from './movie';
import {MovieService} from './movies.service';
import {MovieFilterPipe} from './movie-filter.pipe';
import { StarComponent } from '../shared/star.component';

    selector: 'sz-movies',
    templateUrl: 'app/movies/movies-list.component.html',
    styleUrls: ['app/movies/movies-list.component.css'],  
    pipes: [MovieFilterPipe],
    directives: [StarComponent, ROUTER_DIRECTIVES]
export class MovieListComponent implements OnInit {
    pageTitle: string = 'Movie List';   
    listFilter: string;
    errorMessage: string;
    movies: IMovie[];

    constructor(private _movieService: MovieService) {

    ngOnInit(): void {
            movies => this.movies = movies,
            error => this.errorMessage = error);


here is the movies-list.component.htmlmovies_html


for this component I have a nested StarComponent.

Setp 5: create nested StarComponent, here is the star.component.ts under the share folder

import { Component, OnInit, Input} from 'angular2/core';
    selector: 'sz-star',
    templateUrl: 'app/shared/star.component.html',
    styleUrls: ['app/shared/star.component.css']
export class StarComponent implements OnInit {
    @Input() rating: number;
    starWidth: number;
    ngOnInit(): void {
        this.starWidth = this.rating * 86 / 5;




In angular 2: data flow is unidirectional, for passing data from view to the class

first: you passing data into the directive could bind it to the property


Second: need import the Input from angular2/core, and bind it to the property


Now, one nested movies-list.component and nested star.componet down.

Now using the same steps create the MovieCreateComponent

import {Component, OnInit} from 'angular2/core';

import {IMovie, Movie} from '../movie';
import {Router} from 'angular2/router';
import {MovieService} from '../movies.service';

    templateUrl: 'app/movies/create/movie-create.component.html'

export class MovieCreateComponent implements OnInit {
    title: string = 'Create New Movie';
    newMovie: IMovie;

        private _moviesService: MovieService,
        private _router: Router
    ) { }

    ngOnInit() {
        this.newMovie = new Movie;

    createMovie(event) {
        let _this = this;

            .subscribe(function () {

    cancelCreate(event) {


In the index.html page, put the baseurl:

Done, have fun!

Use PHP cookie to create a program that stores the date and time of a user’s last visit.

Use PHP cookie to create a  program that stores the date and time of a user’s last visit.

here is your source code.

    $lastVisit = “<p>Your last visit was on “.$_COOKIE[‘lastVisit’];
    $lastVisit = “<p>This is your first visit!</p>”;    
setcookie(“lastVisit”, date(“F j, Y, g:i a”),time()+60*60*24*365);

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>

<?php echo $lastVisit; ?>

Displaying a Random Image

here is the function source code:

var imageArr = new Array(“addiction”, “amy”,”abba”, “aphex”,”backstreet”, “bjork”, “bowie”, “donna”,”elliott”, “fergie”, “groove”, “him”,”jeff”, “kanye”, “linkin”, “lisagerrard”,”maddy”, “mariah”, “marvin”, “mary”,”melon”, “neyo”, “nick”, “nin”,”pumpkins”, “rhcp”, “rihanna”, “rob”,”rufus”, “seether”, “staticx”, “stereophonics”,”tim”);

var imgCt = imageArr.length;

function choosePic()
        randomNum = Math.floor(Math.random()*imgCt);

Here is the source code for your product solution

//execute search function
function filter(){    
    //get input value
    var q=document.getElementById(‘txtsearch’).value;
    var regex = new RegExp(q, ‘i’);// ignore uppercase
    //get #datasource’s table tr element from secont row, pecifies a function to run for each tr element.
    $(‘#datasource tr’).slice(1).each(function(i, tr){
        var tr = $(tr);
        var str = tr.text();//get  tr element text value
        //if statement to check whether the regex value equal than str value            
            tr.show();//show match tr elements
            tr.hide();//hide not match  tr elements    

source code for vieworders.php

  //create short variable name
  <title>Bob’s Auto Parts – Customer Orders</title>
<h1>Bob’s Auto Parts</h1>
<h2>Customer Orders</h2>

   @$fp = fopen(“$DOCUMENT_ROOT/orders/orders.txt”, ‘rb’);

   if (!$fp) {
     echo “<p><strong>No orders pending.
           Please try again later.</strong></p>”;

   while (!feof($fp)) {
      $order= fgets($fp, 999);
      echo $order.”<br />”;

   echo “Final position of the file pointer is “.(ftell($fp));
   echo “<br />”;
   echo “After rewind, the position is “.(ftell($fp));
   echo “<br />”;



a simple sample for loop and while loop in php

$prices = array(‘Tires’=>100, ‘Oil’=>10, ‘Spark Plugs’=>4);

The foreach loop has a slightly different structure when using associative arrays.You
can use it exactly as you did in the previous example, or you can incorporate the keys as
foreach ($prices as $key => $value) {
echo $key.” – “.$value.”<br />”;

The following code lists the contents of the $prices array using the each() construct:
while ($element = each($prices)) {
echo $element[‘key’];
echo ” – “;
echo $element[‘value’];
echo “<br />”;

There is a more elegant and more common way of doing the same thing.The construct
list() can be used to split an array into a number of values.You can separate two
of the values that the each() function gives you like this:
while (list($product, $price) = each($prices)) {
echo “$product – $price<br />”;

The output of this script fragment is shown in Figure 3.2.

Tires – 100
Oil – 10
Spark Plugs – 4