Content

You cannot learn programming by only looking at examples. Please refer to the recommended text book (Murach's PHP and MySQL) for detailed explanations.
After you finished going through all examples on this page, some AJAX examples can be found on this ajax page.


Basic php examples:

Php etc file / title Txt file / link Description Video
step 1. preparation, basic html review, install XAMPP, create alias for Apache web server
Preparation   - Show File Extensions in Windows 8/8.1 video 00_001
    - install notepad++ video 00_002
    - Create Screen Captures, Save As PDF Files video 00_003
html Review
html/css page

JavaScript page
- First HTML 5 File - html 5, notepad++, W3C Markup Validator video html_ex001_basic
    - Linking HTML & CSS Files video html_ex003_selector
Setup environment What if I get this error?
"XAMPP, Apache - Error: Apache shutdown unexpectedly"

Follw the steps to fix it

- install XAMPP

The videos showed the using XAMPP for Windows, version 1.8.3-5 (PHP 5.5.15). The latest version does almost the same thing. The main difference is that it has a slightly different Dashboard. Please take note.
video 02_001

video 02_002 (new version, v5.6.12, watch this one)

video 02_002 (OLD version, v1.8.305)

video 02_003
  code to create alias - create alias video 02_004
Step 2. basic php examples - you need to open the txt files to see php code
eg001_basic.php eg001_basic.txt - comments, php tag in html, decleration of variables  
eg002_basic.php eg002_basic.txt - variables, data types, loops  
eg003_input.php

eg003_process.php

eg003_input.txt

eg003_process.txt

- get user input, submit form to eg003_process.php
- something interesting, a loan calculator from external site (in js)
 
eg004_function.php eg004_function.txt - function  
eg005_array.php eg005_array.php - simple array  
eg006_associative_array.php eg006_associative_array.txt - associative array  
 

 

MySQL and PDO example (eg008, php textbook ch. 4):

Connect to MySQL database using PDO (PHP Data Objects). Select, insert, update, delete.

Php etc file Txt file Description Video
step 0 create MySQL database, create MySQL user for web access
using MySQL cmd prompt (shell) in XAMPP cmd_sql.txt - using cmd prompt (shell)
- create another super user
(login: "harry"; password: "elbomonkey")
video 02_005
using XAMPP MySQL web interface (phpMyAdmin)   - Login as "root" (super user)
- change "root" password (from "" to "rootpass")
- login as another super user
video 02_006 (please skip the second half of this video, old xampp version)
step 1 create database, connect to database, and show error if something goes wrong
eg008_create_db.sql eg008_create_db.txt - create sql file, use phpmyadmin to create database in MySQL DB server
- ER diagram
video 02_007
eg008a_connect_database.php eg008a_connect_database.txt
main.css
  video 02_008
eg008b_database_error.php eg008b_database_error.txt    
step 2 insert, update, and delete database records (simple one line SQL statement)
eg008c_select.php eg008c_select.txt - sql select statement
- simple php example
video 02_009
eg008d_insert.php eg008d_insert.txt - sql insert statement
- simple php example
 
eg008e_update.php eg008e_update.txt - sql update statement
- simple php example
video 02_010
eg008f_delete.php eg008f_delete.txt - sql delete statement
- simple php example
 
 

 

MySQL and PDO example (eg009, php textbook ch. 4):

Connect to MySQL database using PDO (PHP Data Objects). Select, insert, update, delete. This example is more complicated than eg008 (simple one statement files).

Php etc file Txt file Description Video
step 0 login to MySQL database
using XAMPP MySQL web interface (phpMyAdmin)   - login: "harry"; password: "elbomonkey"
 
step 1 create database, connect to database, and show error if something goes wrong
eg008_create_db.sql eg008_create_db.txt - done in eg008
- ER diagram
done in eg008
eg009a_database.php eg009a_database.txt
main.css
  video 02_011
eg009b_database_error.php eg009b_database_error.txt    
step 2 insert, update, and delete database records (simple one line SQL statement)
eg009c_index.php eg009c_index.txt - sql select statement
- php program logic
video 02_012
eg009d_delete_product.php eg009d_delete_product.txt - sql delete statement
- php program logic
video 02_013
eg009e_add_product_form.php eg009e_add_product_form.txt - sql insert statement
- php program logic
video 02_014
eg009f_add_product.php eg009f_add_product.txt    
eg009g_error.php
eg009g_error.txt    
 

 

MVC pattern example (eg010, php textbook ch. 5, half of ch. 5 example - product manager only):

Controller = index.php
Model = database.php; product_db.php; category_db.php
View = product_list.php; product_add.php; database_error.php

Php etc file Txt file Description Video
step 0 create MySQL database, create MySQL user for web access
eg008_create_db.sql eg008_create_db.txt - done in eg008
- ER diagram
done in eg008
create 4 subfolders:
- product_manager/
- model/
- errors/
- view/
index.php is put in "product_manager/" subfolder    
step 1 create Model (Mvc)= represent the data
model/
database.php
database.txt - create model folder
- connect to database
 
step 1 create View (mVc)= user interface
errors/
database_error.php

view/
header.php
footer.php

main.css
database_error.txt


header.txt
footer.txt


main.css
- create errors folder
- create file, database_error.php includes header.php and footer.php

- create view folder
- create files, header.php and footer.php includes main.css

 
step 1 create Model (Mvc)= represent the data
model/
category_db.php
category_db.txt
- create files
- create function "get_category_name()" to get default category name from database table
 
step 1 create Controller (mvC)= receive user request
product_manager/index.php index.txt - include files, use function "get_category_name()" to get category name  
step 2 expand MVC, add program functions to list products
step 2 update/create Model (Mvc)= represent the data
model/
category_db.php
category_db.txt
- update
- create function "get_categories()" to get all category names from database table
 
model/product_db.php product_db.txt - create new file
- create function "get_products_by_category()" to get all product names
 
step 2 update/create Controller (mvC)= receive user request
product_manager/index.php index.txt - update
- include product_db.php file
- use function "get_categories()" to get all category names
- use function "get_products_by_category()" to get all product names
- include product_list.php (new file below)
 
product_manager/product_list.php product_list.txt - create new file
- include ../view/header.php
- include ../view/footer.php
- use $categories from index.php, display
- use $category_name and $products from index.php, display, create delete buttons, using post action
- create add product link, using get action
 
step 3 expand MVC, add program functions to delete products
step 3 update Controller (mvC)= receive user request
product_manager/index.php
index.txt
- update
- handle delete if action is delete_product
- use function "delete_product()" to be created in model/product_db.php below
 
step 3 update Model (Mvc)= represent the data
model/product_db.php product_db.txt - update
- add function "delete_product()" to delete
 
step 4 expand MVC, add program functions to show add product form
step 4 update Controller (mvC)= receive user request
product_manager/index.php
index.txt
- update
- handle add if action is show_add_form
- use function "get_categories()" which has been create in step 2 in model/category_db.php
- include file product_add.php
 
product_manager/product_add.php product_add.txt - create new file
- handle add if action is show_add_form
- include ../view/header.php
- include ../view/footer.php
- use $categories from index.php, display
- capture user input, submit to index.php, action is add_product as hidden field
- add link to index.php, action is list_products
 
step 5 expand MVC, add program functions to add product
step 5 update Controller (mvC)= receive user request
product_manager/index.php
index.txt
- update
- handle add if action is add_product
- check empty field, if yes, include file ../errors/error.php
- else, use "add_product()" function in model/product_db.php
- send it back to self, keep $category_id
 
step 5 create View (mVc)= user interface
errors/
error.php
error.txt
- create file, error.php
- include ../view/header.php
- include ../view/footer.php
- show $error from index.php
 
step 5 update Model (Mvc)= represent the data
model/product_db.php product_db.txt - update
- add function "add_product()" to add to database
 
 

 

AJAX Examples:

AJAX Examples can be found on this ajax page.

 

 

Mail and Dynamic Textbox (hence other form elements) Examples:

There are some other examples, including mail and dynamic textbox examples.

Php etc file Txt file Description Video
Simple mail example
eg100_mail.php eg100_mail.txt simple mail example (works on most hosting service), it does not work with localhost unless you reconfig or install PEAR mail.
 
use JavaScript to dynamically create textboxes
eg101_dynamic_textbox.php eg101_dynamic_textbox.txt

eg101_dynamic_process.txt

use JavaScript to dynamically create textboxes, and use another php file eg101_dynamic_process.php (text file) to pick up input.  
use php to dynamically create textboxes
eg102_choice.htm   to choose the number of textboxes...  
eg102_dynamic_textbox.php
eg102_dynamic_textbox.txt use php to dynamically create textboxes, and use another php file eg101_dynamic_process.php (text file) to pick up input (same as above).