Content
You cannot learn programming by only looking at examples. Please refer to the recommended text book (Murach's PHP and MySQL 2nd Edition) for detailed explanations.
After you finished going through all examples on this page, some AJAX examples can be found on this ajax page.
PHP origionally stood for Personal Home Page, but it now stands for the recursive acronym "PHP: Hypertext Preprocessor". - Quote from wikipedia.org
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" |
- install XAMPP
The videos showed the using XAMPP for Windows, version 5.6.19 (PHP 5.6.19). |
video 07_001 video 07_002 video 07_003 video 07_004 |
code to create alias | - create alias | video 07_005 | |
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 | video 07_006 |
eg002_basic.php | eg002_basic.txt | - variables, data types, loops | |
eg003_input.php
main.css |
eg003_input.txt | - get user input, submit form to eg003_process.php
- something interesting, a loan calculator from external site (in js) |
video 07_007 |
eg004_function.php | eg004_function.txt | - function | |
eg005_array.php | eg005_array.txt | - simple array | |
eg006_associative_array.php | eg006_associative_array.txt | - associative array | |
eg006a_2d_array.php | eg006a_2d_array.txt | - 2d array (can skip this) | |
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
other sql commands (change root password etc) |
- using cmd prompt (shell) - create another super user (login: "harry"; password: "elbomonkey") |
video 07_008 video 07_009 video 07_010 |
using XAMPP MySQL web interface (phpMyAdmin) | phpMyAdmin http authentication | - Login as "root" (super user) - change "root" password (from "" to "rootpass") - login as another super user |
video 07_011 |
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 07_012 |
eg008a_connect_database.php | eg008a_connect_database.txt main.css |
video 07_013 | |
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 |
|
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 07_014 |
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 07_015 | |
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 07_016 |
eg009d_delete_product.php | eg009d_delete_product.txt | - sql delete statement - php program logic |
video 07_017 |
eg009e_add_product_form.php | eg009e_add_product_form.txt | - sql insert statement - php program logic |
|
eg009f_add_product.php | eg009f_add_product.txt | video 07_018 | |
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, Dynamic Textbox etc. (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 | 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.php | eg102_choice.txt | 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). | |
Upload file | |||
eg051.php | eg051.txt | ||
eg051_upload.php | eg051_upload.txt | need to create sub_folder "upload_folder" to save files | |
Scan files in a folder | |||
eg052_scandir.php | eg052_scandir.txt | need to create sub_folder "eg052_dir" to store files inside | |
Read the content of a text file | |||
eg053_readfile.php | eg053_readfile.txt | need to create sub_folder "eg053_dir" to store file inside | |
Use Session to check login | |||