Richard's PHP 2022 Page


You cannot learn programming by only looking at examples. Please refer to the recommended text book (Murach's PHP and MySQL 3rd Edition) for detailed explanations.

PHP originally stood for Personal Home Page, but it now stands for the recursive acronym "PHP: Hypertext Preprocessor", example here. - Quote from wikipedia.org

Step 0. Preparation, Basic HTML

Preparation, basic html review

Files / Title Description Video
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/css page
- First HTML 5 File - html 5, notepad++, W3C Markup Validator video html_ex001_basic
JavaScript page - Linking HTML & CSS Files video html_ex003_selector

 


Step 1. Install XAMPP

Files / Title Description Video
Setup environment - install XAMPP video 11_001, xampp install 1
  - What if I get this error?
"XAMPP, Apache - Error: Apache shutdown unexpectedly"

Follow these steps to fix it

video 11_002, xampp install 2


video 11_003, xampp install 3

create alias - code to create alias

video 11_004, xampp install 4


video 11_005, xampp install 5

 


Step 2. Basic PHP

- Open .TXT files to see php code

Files / Title Description Video
eg001_basic.php
eg001_basic.txt
- comments, php tag in html, decleration of variables video 11_006, basic php
eg002_basic.php
eg002_basic.txt
- variables, data types, loops

video 11_007, basic php


video 11_008, basic php

  Use W3C Markup Validation Service to validate your php program's html markup validity. video 11_009, validation, W3C Markup Validator
eg003_input.php
main.css
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)

video 11_010, eg003_1


video 11_011, eg003_2


video 11_012, eg003_3


video
11_013, eg003_4


video 11_014, eg003_5

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  

 


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

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

Files / Title 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 11_030, xampp mysql 1, cmd prompt

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 11_031, xampp mysql 2, cmd promot & phpMyAdmin
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

- code to alter db engine
- external site explanation (optional)

video 11_032, xampp eg008, 1, create database

video 11_032a, change db engine (if you hit error in video 11_032)

eg008a_connect_database.php

eg008a_connect_database.txt
main.css

  video 11_033, xampp eg008, 2, connect to database

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 11_034, xampp eg008, 3, select

eg008d_insert.php

eg008d_insert.txt

- sql insert statement
- simple php example
video 11_035, xampp eg008, 4, insert

eg008e_update.php

eg008e_update.txt

- sql update statement
- simple php example
video 11_036, xampp eg008, 5, update

eg008f_delete.php

eg008f_delete.txt

- sql delete statement
- simple php example
video 11_037, xampp eg008, 6, delete

 


Step 4. 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).

Files / Title 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

 

eg009a_database.php

eg009a_database.txt
main.css

  video 11_038, xampp eg009, 1, connect to database

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 11_039, xampp eg009, 2, index

eg009d_delete_product.php

eg009d_delete_product.txt

- sql delete statement
- php program logic
video 11_040, xampp eg009, 3, index continue

eg009e_add_product_form.php

eg009e_add_product_form.txt

- sql insert statement
- php program logic
video 11_041, xampp eg009, 4, add product

eg009f_add_product.php

eg009f_add_product.txt

  video 11_042, xampp eg009, 5, add product

eg009g_error.php

eg009g_error.txt

   

 


Step 5, 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

Files / Title 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
video 11_043 xampp eg010, 1, MVC pattern
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

database_error.txt

view/
header.php
footer.php

header.txt
footer.txt

main.css

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
video 11_044 xampp eg010, 2, database_error.php
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 video 11_045 xampp eg010, 3, controller

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)
video 11_046 xampp eg010, 4, product list

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
video 11_047 xampp eg010, 5, delete
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
video 11_048 xampp eg010, 6, add product

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 = Asynchronous JavaScript and XML

  • AJAX is NOT a new programming language.
  • Use existing standards to ... exchange data with a server, update parts of a web page - without reloading the whole page. (e.g. Google Map, Gmail, Youtube etc)
  • You should know the following before doing AJAX
    • HTML
    • CSS
    • JavaScript / DOM
Files / Title Description Video
One way of creating XMLHttpRequest object from w3schools
eg001.htm - create XMLHttpRequest object, async=false, not recommenderead, read data from text file eg001_info.txt  
eg002.htm

- create XMLHttpRequest object, async=true, the correct way, read data from text file eg001_info.txt

 
Another way of creating XMLHttpRequest object from tizag.com
eg003.htm - create XMLHttpRequest object, onkeypress event, read data from php file eg003_time.php (txt file) video 11_050 xampp ajax 1

video 11_051 xampp ajax 2
eg004.htm - create XMLHttpRequest object, get data from database, using php file eg008c_select_changed.php (txt file),
- two more files are needed eg008a_connect_database.php (txt file), and eg008b_database_error.php (txt file).
video 11_052 xampp ajax 3
eg005.htm - create XMLHttpRequest object, search data in database, using php file eg008c_select_search.php (txt file),
- two more files are needed eg008a_connect_database.php (txt file), and eg008b_database_error.php (txt file).
video 11_053 xampp ajax 4
eg006.htm - search data in database, update one form field, using php file eg008c_select_single.php (txt file),
- two more files are needed eg008a_connect_database.php (txt file), and eg008b_database_error.php (txt file).
video 11_054 xampp ajax 5

 


Mail, Dynamic Textbox etc. and Other Examples

Some other examples, including mail and dynamic textbox examples.

Files / Title 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.php

eg102_choice.txt

- to choose the number of textboxes... video 11_055 xampp dynamic textbox 1

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

video 11_056 xampp dynamic textbox 2

 

video 11_057 xampp dynamic textbox 3

Upload file

eg051.php

eg051.txt

- upload file  

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

- scan all files in a folder

- 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

eg030_session_1.php

eg030_session_1.txt

eg030_session_2.php

eg030_session_2.txt

- create session variable

 

- retrieve session variable

 

eg032_login.php

eg032_login.txt

eg032_logout.php

eg032_logout.txt

eg032_main_content.php

eg032_main_context.txt

- user login, use session variable to check if login is successful

- the content file will block user access if unauthorized access is detected

 
Regular Expressions

eg033.php

eg033.txt

- use regular expressions to extract content (deliminators)