From 3766332db711a016900397530fbc6abda8b040a3 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Wed, 2 May 2018 11:29:32 -0400 Subject: [PATCH] all the ham --- CRUD Example/.htaccess | 6 + CRUD Example/controllers/coffee.php | 28 ++ CRUD Example/models/coffee.php | 41 +++ CRUD Example/views/coffee/index.php | 26 ++ CRUD Example/views/coffee/new.php | 13 + PHP.md | 541 ++++++++++++++++++++++++++++ PHP_CRUD.md | 51 +++ 7 files changed, 706 insertions(+) create mode 100644 CRUD Example/.htaccess create mode 100644 CRUD Example/controllers/coffee.php create mode 100644 CRUD Example/models/coffee.php create mode 100644 CRUD Example/views/coffee/index.php create mode 100644 CRUD Example/views/coffee/new.php create mode 100644 PHP.md create mode 100644 PHP_CRUD.md diff --git a/CRUD Example/.htaccess b/CRUD Example/.htaccess new file mode 100644 index 0000000..d2f596c --- /dev/null +++ b/CRUD Example/.htaccess @@ -0,0 +1,6 @@ +RewriteEngine On +RewriteCond %{REQUEST_METHOD} ^GET$ +RewriteRule ^coffee$ controllers/coffee.php?action=index +RewriteRule ^coffee/new$ controllers/coffee.php?action=new +RewriteCond %{REQUEST_METHOD} ^POST$ +RewriteRule ^coffee$ controllers/coffee.php?action=create diff --git a/CRUD Example/controllers/coffee.php b/CRUD Example/controllers/coffee.php new file mode 100644 index 0000000..c160511 --- /dev/null +++ b/CRUD Example/controllers/coffee.php @@ -0,0 +1,28 @@ +indexPage(); +} else if($_GET['action']=='new'){ + $new_coffee_controller->newPage(); +} else if($_GET['action']=='create'){ + $new_coffee_controller->createAction(); +} diff --git a/CRUD Example/models/coffee.php b/CRUD Example/models/coffee.php new file mode 100644 index 0000000..bb88867 --- /dev/null +++ b/CRUD Example/models/coffee.php @@ -0,0 +1,41 @@ +connect_error){ + die('Connection Failed: ' . $mysql_connection->connect_error); + } else { + $sql = "INSERT INTO coffee (drink, guest) VALUES ('".$drink."','".$guest."');"; + $mysql_connection->query($sql); + } + $mysql_connection->close(); + } + + static public function find(){ + //connect just once, not for every create/find + $servername = 'localhost'; + $username = 'root'; + $password = 'root'; + $dbname = 'phpexample'; + + $mysql_connection = new mysqli($servername, $username, $password, $dbname); + + if($mysql_connection->connect_error){ + $mysql_connection->close(); + die('Connection Failed: ' . $mysql_connection->connect_error); + } else { + $sql = "SELECT * FROM coffee;"; + $results = $mysql_connection->query($sql); + return $results; + } + } +} diff --git a/CRUD Example/views/coffee/index.php b/CRUD Example/views/coffee/index.php new file mode 100644 index 0000000..20d95b7 --- /dev/null +++ b/CRUD Example/views/coffee/index.php @@ -0,0 +1,26 @@ + + + + + +
+

Coffee Index Page

+ +
+
+ +
+ + \ No newline at end of file diff --git a/CRUD Example/views/coffee/new.php b/CRUD Example/views/coffee/new.php new file mode 100644 index 0000000..8d0e592 --- /dev/null +++ b/CRUD Example/views/coffee/new.php @@ -0,0 +1,13 @@ + + + + + +

Add a New Coffee To The List

+
+
+
+ +
+ + diff --git a/PHP.md b/PHP.md new file mode 100644 index 0000000..2f5d8f4 --- /dev/null +++ b/PHP.md @@ -0,0 +1,541 @@ +# PHP + +## Setup + +1. Download [MAMP] (https://www.mamp.info/en/downloads/) +1. Double click .pkg file and follow prompts +1. Double click /Applications/MAMP/MAMP +1. Point MAMP to working directory + - Click on Preferences + - Click on Web Server + - Click the folder icon next to "Document Root" and find a suitable directory to work out of + - Click OK +1. Go to +1. Error logs are in /Applications/MAMP/logs/ + - `tail -f php_error.log` +1. MAMP stands for Mac, Apache, MySQL, PHP + - this describes the tech stack + - Mac + - Your OS + - Apache + - A pre-build web server that serves static files + - Is extendable with various modules that allows it to do many things easily + - MySQL + - Your Database + - PHP + - A module for Apache that allows it to server dynamic data + +## Basics + +### Tags + +Because this is all run on top of Apache, the initial assumption is that we're serving static HTML files +- We need `` tags to show that we're writing PHP +- Think of this as if Apache/PHP is server.js and we're just writing EJS + +Instead of `<%= %>` you have `` or `` + +Instead of `<% %>` you have `` + +### Comments + +```php +// single line comment +``` +```php +/* +multi +line +comment +*/ +``` + +### Declaring/Assigning variables + +Use a $ before a variable name to tell php it is a variable. Assignment is standard. + +```php + +``` + +### Data Types + +```php + +``` +```php + +``` +```php + +``` +```php + +``` +```php + +``` +```php + +``` + +### String Operators + +Use a `.` or `.=` to combine strings. + +```php + +``` + +### Arithmetic Operators + +```php + +``` + +### Increment/Decrement Operators + +```php + +``` + +### Assignment Operators + +```php + +``` + +## Conditionals + +### Formats + +Format 1: + +```php + +``` + +Format 2: + +```php + + condition1 + + condition2 + + else + +``` + +### Comparison Operators + +```php + +``` + +Arithmetic: + +```php + $y; //greater than + $x <= $y; //less than or equal to + $x >= $y; //greater than or equal to +?> +``` + +### Logical Operators + +```php + +``` + +## Arrays + +### Indexed Arrays + +Standard array functionality + +```php + +``` + +### Associative Arrays (hashes) + +These are very similar to Ruby hashes + +```php + 35, "Ben" => 37, "Joe" => "43"); //declare + $age["Bob"] = 105; //add at a new position + echo "Bob is " . $age['Bob'] . " years old."; +?> +``` + +## Loops + +### While + +```php +"; + $x++; +} +?> +``` +```php + + +
  • + + +``` + +### For + +```php +"; +} +?> +``` +```php + +
  • The number is:
  • + +``` + +### Foreach + +```php + $value) { + echo $key . ": $value
    "; +} +?> +``` +```php + + $value): ?> + :
    + +``` + +This works for associative arrays: + +```php + 35, "Ben" => 37, "Joe" => "43"); + +foreach ($ages as $key => $value) { + echo $key . ": $value
    "; +} +?> +``` +```php + 35, "Ben" => 37, "Joe" => "43"); ?> + $value): ?> + :
    + +``` + +## Functions + +```php + +``` + +## Convenience Methods + +### Strings + +Count a string's length + +```php + +``` + +Count number of words in a string + +```php + +``` + +Reverse a string + +```php + +``` + +Find a sub string in a string + +```php + +``` + +Replace text within a string + +```php + +``` + +Get a substring based on character index + +```php + +``` + +Turn a string into an array based on some delimeter + +```php + +``` + +### Sorting Arrays + +Various functions for sorting arrays. Can be done arithmetically or alphabetically, depending on content + +- sort() - sort arrays in ascending order +- rsort() - sort arrays in descending order +- asort() - sort associative arrays in ascending order, according to the value +- ksort() - sort associative arrays in ascending order, according to the key +- arsort() - sort associative arrays in descending order, according to the value +- krsort() - sort associative arrays in descending order, according to the key + +These functions affect the actual array they are called on. + +```php +$cars = array("Volvo", "BMW", "Toyota"); +sort($cars); +print_r($cars); //BMW, Toyota, Volvo +``` + +## Classes and Objects + +### Public Members + +```php +wheels = 3; +print_r($my_car); +?> +``` + +### Constructors + +```php +wheels = $num_wheels; + } +} +$my_car = new Car(4); +?> +``` + +### Protected Members + +```php +wheels; + } + public function __construct($num_wheels){ // runs at beginning of object creation + $this->wheels = $num_wheels; + } +} + +$my_car = new Car(4); +echo $my_car->getWheels(); +echo $my_car->wheels; //error: cannot access protected property +?> +``` + +### Statics + +```php +wheels; + } + public function __construct($num_wheels){ // runs at beginning of object creation + $this->wheels = $num_wheels; + } +} +class CarFactory{ + static protected $cars = array(); + static public function create($num_wheels){ + $new_car = new Car($num_wheels); + self::$cars[] = $new_car; + return $new_car; + } + static public function find($index){ + return self::$cars[$index]; + } +} +$my_car = CarFactory::create(2); +$my_car2 = CarFactory::create(4); +print_r($my_car); +print_r(CarFactory::find(1)); +print_r(CarFactory::$cars); //errors out: protected +?> +``` + +### Inheritance + +```php +wheels = $num_wheels; + } + public function getWheels(){ + return $this->wheels; + } + public function start(){ + $this->engine_on = true; + } +} + +class Humvee extends Car { + protected $armour = 10; + public function takeDamage($damage){ + $this->armour -= $damage; + } + public function loseWheel(){ + $this->wheels--; //can access parent $wheels member + } + //can extend parent's start function + public function start(){ + parent::start(); //run parent's start function + //parent::$engine_on = false; //errors out: cannot access private member of parent + echo "WELCOME TO THE JUNGLE...";//then do something else + } +} + +$my_humvee = new Humvee(6); +print_r($my_humvee); +$my_humvee->loseWheel(); +print_r($my_humvee); +$my_humvee->takeDamage(6); +print_r($my_humvee); +$my_humvee->start(); +print_r($my_humvee); +?> +``` diff --git a/PHP_CRUD.md b/PHP_CRUD.md new file mode 100644 index 0000000..b241036 --- /dev/null +++ b/PHP_CRUD.md @@ -0,0 +1,51 @@ +# CRUD With PHP + +## Lesson Objectives + +1. Route URLs to php files +2. Connect to MySQL + +## Route URLs to php files + +Routing can be accomplished with a .htaccess file placed in your root directory for the app. It will look like this: + +``` +RewriteEngine On +RewriteRule ^coffee$ controllers/coffee.php +``` + +It uses regular expressions to map urls to files + +## Connect to MySQL + +Simple database connection and querying looks like this: + +```php +$servername = "localhost"; +$username = "root"; +$password = "root"; +$dbname = "phpexample"; + +// Create connection + $conn = new mysqli($servername, $username, $password, $dbname); + +// Check connection + if ($conn->connect_error) { + die("Connection failed: " . $conn->connect_error); +} +$sql = "SELECT * FROM coffee"; +$results = $conn->query($sql); +$conn->close(); +``` + +Looping through the results would go like this: + +```php +fetch_object()): ?> + +
  • + drink ?>: + guest ?> +
  • + +```