<repo_name>zeromile/php-training<file_sep>/arrays.php
<?php
$myIndexArray = array("Katie", "Em", "Molly");

$myAssocArray = array(
  "name" => "Katie",
  "latinx" => True,
  "cohort" => "design"
);

$myOtherAssocArray = array();

$myOtherAssocArray["name"] = "Nikki";
<file_sep>/README.md
# Basic PHP #
## Day 01 ##
[Basic PHP/MySQL Install](basic-php-instructions.md)
 - Create ```index.php```
 - Comments ```// /* */```
 - ```echo``` Command
 - Variables
 - Strings
 - Concatenation
 - Arrays
   - Indexed
   - Associative
   - Multi-dimensional

## Day 02 ##
 - ```include_once``` vs ```require_once()```
 - Control structures
   - ```for```
   - ```if```
   - ```==``` vs ```=```
 - Functions

Challenge: Create a 3 page website (```about.php```, ```index.php```, ```contact.php```) whose navigation and page content is fed by arrays; use functions to access the arrays and echo the content. You will need:  ```content.php```, ```functions.php```, and ```nav.php``` files

## Day 03 ##
 - Make a backup of your Vagrant file ```$ mv Vagrantfile Vagrantfile.log```
 - Download [Vagrantfile](Vagrantfile) from the repo to your local ```php-training``` folder (or copy-paste to an empty ```Vagrantfile``` - click on file, view "raw", then file/save)
 - Create a basic form on the ```contact.php``` page (firstname, lastname, submit)
 - Use ```$_POST/$_GET``` to pull data into the index.php
 - Use "Null Coalescing" ```??``` operators to set default value of variable from ```$_POST```

Challenge: Split into teams of 2 and, on the White boards, write out a basic HTML page that uses PHP to set a name variable before the HTML and then echo out that variable in the body of the HTML.

Challenge: Same teams, on the white boards, before your HTML use php to set two names with an array and then use a loop in the body of the HTML to echo each name out in ```<p>``` tags.

Challenge: Use PHP to get the first and last name variables from ```$_POST``` and echo them out as the default content for the two first and last name fields in the form.

## Day 04 ##
  - Databases (what are they? how do they even work?)
    - MySQL
      - Log in to mysql locally
      - Show Databases
      - Show Tables
      - Describe tables
      - Select data
      - Create a database
      - Create a table
      - Insert rows

## Day 05 ##  
    - Enable PHP Error Reporting
      - [enable-php-error-reporting](enable-php-error-reporting.md)
    - Connect to database in PHP

## Day 06 ##

## Day 07 ##
 - Back up database with ```mysqldump```  
 - Import database using ```mysql```  
 - Update ```Vagrant``` provisioners to backup and import database
 - Begin restructure of database tables
    - create navigation table
    - update content table
 - Update ```index.php``` and ```function-new.php``` to load new navigation

## Day 08 ##

 - Enable error checking without editing ```php.ini```
    - ```.htaccess``` file directives for error checking
      - ```php_flag display_startup_errors on```
      - ```php_flag display_errors on```  
    - PHP file directives for error checking (add to ```connect.php```)
      - ```ini_set('display_errors', 1);```  
      - ```ini_set('display_startup_errors', 1);```  
      - ```error_reporting(E_ALL);```  
 - Update tables to remove ```.php``` from filename field
 - Create file redirects
   - ```.htaccess``` redirects:  

    ```
    # Turn rewrite on  
    Options +FollowSymLinks  
    RewriteEngine On

    # Redirect requests to index.php  
    RewriteCond %{REQUEST_URI} !=index.php  
    RewriteCond %{REQUEST_URI} !.*\.png$ [NC]  
    RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]  
    RewriteCond %{REQUEST_URI} !.*\.css$ [NC]  
    RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]  
    RewriteCond %{REQUEST_URI} !.*\.js$ [NC]  
    RewriteRule .* index.php
    ```  
   - ```$ sudo a2enmod rewrite```
   - ```sudo sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf```
   - ```sudo service apache2 restart```

- edit ```connect.php``` to include redirect code
   - ```ini_set('display_errors', 1);```
   - ```ini_set('display_startup_errors', 1);```
   - ```error_reporting(E_ALL);```  


 - Add code to grab page name from URL
```
        $uriSegments = explode("parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));  
        $thisPage = array_pop($uriSegments);  
        if($thisPage=="") {
          $thisPage="home";
        }  
        $thisPagename = $thisPage;
```
 - Updated ```makeNav()```
 - Updated ```makeContent()``` to loop through multiple content items


## Day 09 ##
  - Add comments to relevant files
  - Review new Vagranfile updates for enabling redirects, adding the following to the shell scripts to the APACHE2-RESTART section  
  ```
  echo "----------------APACHE2-RESTART--------"
  sudo a2enmod rewrite  
  sudo sed -i '/<Directory \\/var\\/www\\/>/,/<\\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf  
  sudo service apache2 restart  
  ```
 - Update ```.htaccess``` to allow php files
   - Comment this line
      - ```#RewriteCond %{REQUEST_URI} !=index.php```
   - And add this line after
      - ```RewriteCond %{REQUEST_URI} !.*\.php$ [NC]```  
 - Create a login link in nav function
 - Show how variables do not get passed from page to page
   - set $loggedIn
   - echo this in index.php
   - create login.php to echo
   - it fails
 - Set session variable in index first
 - Echo session variable in login
 - Change nav so that login link only shows when not "logged in" is true
 - Create ```secret.php``` which will display a special message when logged in but redirect back to index.php if not

  ```
  <?php
  session_start();  
  $loggedIn = $_SESSION['loggedin'];  

  if ( $loggedIn == "logged in" ) {  
    echo "Welcome to the secret page. K. That's it. Bye.";  
    } else {  
      header("Location: http://192.168.33.10/index");  
    }  
  ```

## Day 10 ##
Today we will continue adding to our login system.
 - Add and commit your changes
  ```
      $ git add .  
      $ git commit -m "beginning of day 10"
  ```   
 - Create and checkout day10 branch  
  ```
  $ git checkout -b day10
  ```
  OR  
  ```
  $ git branch day10
  $ git checkout day10
  ```
 - Remove these files: ```contact.php```, ```content.php```, ```nav.php```, ```about.php```  
 - remove these two lines from top of ```connect.php```  
 ```
 //$loggedIn = "not logged in";
 $_SESSION["loggedin"] = "not logged in";
```
 - Add logout link in ```function-new.php```  
 ```
 if ($loggedIn == "not logged in"){
   echo "<li><a href='login.php'>Log In</a></li>";
 } else {
   echo "<li><a href='logout.php'>Log Out</a></li>";
 }
 ```
 - Create ```logout.php```
   - ```$ touch logout.php```  
   - Add this code to ```logout.php```  
 ```
 session_start();
 $_SESSION = array();
 session_destroy();
 header("Location: http://192.168.33.10/index");
 ```
 - Update line 5 ```index.php```
 ```
 $loggedIn = $_SESSION['loggedin'] ?? "not logged in";
 ```
 - Revise ```login.php```  
 ```
 session_start();
 require_once('connect.php');

 if (!empty($_POST)){
   // if not logged in then check if login was submitted
   $userName = $_POST["username"];
   $passWord = $_POST["<PASSWORD>"];
   $sql = "SELECT username, password, realname FROM test.users WHERE username = '$userName' LIMIT 1";

   $result = $conn->query($sql);
   $row = $result->fetch_assoc();

   if ($userName == $row["username"] &&
     md5($passWord) == $row["password"])
     {
       $_SESSION["loggedin"] = "logged in";
       $loggedIn = $_SESSION["loggedin"];
     } else {
       echo "Try again";
       $loggedIn = $_SESSION["loggedin"] ?? "not logged in";
     }
 } else {
   $loggedIn = $_SESSION["loggedin"] ?? "not logged in";
 }

 if ($loggedIn == "logged in") {
   // if "logged in" then redirect to index
   header("Location: http://192.168.33.10/index");
 } else {
   // if not logged in show login form
   ?>
   <!DOCTYPE html>
   <html lang="en">
   <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <meta http-equiv="X-UA-Compatible" content="ie=edge">
     <title>Document</title>
   </head>
   <body>
     <h1>Login Page</h1>
     <form action="login.php" method="post">
       Username: <input type="text" name="username"> </br>
       Password: <input type="<PASSWORD>" name="password"> </br>
       <input type="submit" value="login">
     </form>
   </body>
   </html>
 <?php }
```
 - Create ```users``` table (id, login, password)
 - Verify log in using the MD5 Hash function in PHP

## Day 11 ##  
 - Challenge: Add code to your site that will show the user's "realname" when they are logged in to the site
 - Solution: Two lines, yo:
   - Add the ```$_SESSION["realname"]``` line to the following code in ```login.php```

 ```
  if ($userName == $row["username"] &&
   md5($passWord) == $row["password"])
   {
     $_SESSION["loggedin"] = "logged in";  
     $_SESSION["realname"] = $row["realname"]; // add this line
```
  - Add the ```$_SESSION["realname"]``` line to the followiung code in ```function-new.php```
 ```
 if ($loggedIn == "not logged in"){
   echo "<li><a href='login.php'>Log In</a></li>";
 } else {
   echo "<li><a href='logout.php'>Log Out</a></li>";
   echo "<p> Howdy " . $_SESSION["realname"] . "</p>"; //add this line
 }
 echo "</ul>";
```

## Day 12 ##
 - Challenge: Add CSS to your php-training project that does the following:
   - Horizontal navigation w/ background colors:
      - ```#BF3F7F``` when logged in
      - ```#7FBF3F``` when logged out
   - Styles applied to ```<h>``` and ```<p>``` tags (that, at the very least, change the default typefaces)

## Day 13 - Part 1: CSS Challenge Solution##
 - Video at [CSS Challenge Solution](https://youtu.be/XTf1NzeU_UY)
 - Change name of ```function-new.php``` to ```functions.php```  
 - Change the reference to ```functions.php``` in ```index.php```  
 - Add ```<?php echo $_SERVER['PHP_SELF']; ?>``` in the action portion of the form in ```login.php```  
 - Solution to CSS Challenge: Sharing is caring
    - Create ```main.css```
    - Add this css:
    ```
    h1, h2, h3, p, a {
    font-family: sans-serif;
    }

    ul {
      list-style-type: none;
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #333;
    }

    li {
      float: left;
    }

    li a {
      display: block;
      color: white;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
    }

    /* Change the link color to #111 (black) on hover */
    li a:hover {
      background-color: #111;
    }

    section {
      padding: 0 30px 0 30px;
    }
    ```
 - In ```index.php``` a
   - Add the link to the ```main.css``` file in the head section with:
   ```
   <link rel="stylesheet" href="main.css">
   ```
   - Add the ```$thisPageName``` variable into the ```makeNav()``` function call  
     ```
     makeNav($conn, $loggedIn, $thisPagename);  
     ```  
 - In ```functions.php```:  
    - Add the ```$thisPagename``` variable into the ```makeNav()``` function parameters to receive the page name  
    ```
    function makeNav($conn, $loggedIn, $thisPagename){
    ```  
    - Change the background color of the nav by revising the ```makeNav()``` function block to swap a class based on the logged in status of ```$loggedIn``` and echoing out the class in the opening ```<ul>```
      ```
      if($loggedIn == "logged in"){
        $loggedInClass = "class='loggedin'";
      } else {
        $loggedInClass = "class='notloggedin'";
      }
      echo "<ul " . $loggedInClass . ">";
      ```
    - Add an active class to the navigation link by revising the ```makeNav()``` function block to check if the supplied page name is equal to the active page name and setting the echo'd variable ```$active```.  
    ```
    while ( $row = $result->fetch_assoc() ) {
      if ( $thisPagename == $row['pagename']) {
          $active = "class='active'";
        } else {
          $active = "";
        }
      echo "<li " . $active . "><a href='" . $row['pagename'] . "'>" .$row['pagetitle']. "</a></li>";
      }
    ```
 - In ```main.css``` add the following classes  
    ```
    .active {
      background-color: #4CAF50;
    }
    .loggedin {
      background-color: #BF3F7F;
    }
    .notloggedin {
      background-color: #7FBF3F;
    }
    ```

## Day 13 - Part 2: Intro to Classes ##
A class is a created object that is a collection of variables and functions (in a class they are referred to as properties and methods). The advantage of putting your functions and variables into a class is that because a class is scoped to the instantiating object your methods and properties will be protected and also available to each other within the class.
 - Create a new file named ```class.php```
 - In the ```class.php``` file create the class construct named ```Square```:  
 ```
 <?php
 class MyClass
 {

 }
 ```
 - Now let's add a property:
 ```
 class MyClass
 {
   /*
    Create the variable, sets the default
    value, and makes the variable available
    to the other class member's methods &
    properties
   */
   public $myname = 'Jason';
 }
 ```
 - Notice that we have added the word ```public``` in front of our variable declaration. This allows the variable to be accessed from outside of the instantiated class, which means it can be used in our general program and not just inside the class.
 - There are two other variable visibility keywords: ```protected``` and ```private```, which can also be applied to methods. They set the "scope" of the class member they are applied to.
  - ```public``` - member will be available everywhere (must still be called by associating it with the class)
  - ```protected``` - member will be available only inside the class itself and inheriting and parent classes
  - ```private``` - member will be available only within the class that defines the member  
 - We can see this in action by using the php cli  
  - ```$ vagrant ssh``` into your running Vagrant box
  - ```$ cd /vagrant``` // change into your vagrant folder
  - ```$ php -a```  // start the php CLI  
 - Type each of the following lines into the CLI (at the ```php >``` prompt)
   - Load the ```class.php``` file that contains our ```MyClass``` class  
    - ```php > require('class.php');```
   - Instantiate the class in a new variable
    - ```php > $myClass = new MyClass();```  
   - Echo out the ```$myname``` variable  
    - ```php > echo $myClass->myname;```

 - Now lets add a method:  
  ```
  ...
  public $myname = 'Jason';  
  public function printHello()  
  {  
    return $this->myname;  
  }  
  ...
  ```  
 - Notice that there is a new keyword called ```return```. A return will cease execution of the function where the return is called and send any elements after it, in the same statement, back to wherever the function was called from. ```return``` Is not a function/method but a "language construct"
 - Call the function named ```printHello()``` and echo it's output  
  - ```php > echo $myClass->printHello();```
<file_sep>/mysql-cli-database-connection.md
# Connecting to a Database using the MySQL CLI #

This will be a walk-through of how to use the MySQL CLI (command line interface) to connect to MySQL, create a database, create a table, and select data from that table.

We need to start by making sure we have a database selected. We'll be using MySQL and PHP's MySQLi connector. There is another method called PDO (PHP Data Objects) which allows you to connect to 12 different database systems but we will start with the MySQLi for now.

SSH into your running php-training Vagrant box that we set up previously in Day 03. <br/>
```$ vagrant ssh```

Connect to the MySQL CLI  
```$ mysql -u root -p```

The password is ```<PASSWORD>```

You will be presented with the MySQLi CLI prompt:  
```mysql>```

Command reference:  
```SHOW DATABASES;``` - Show a list of all the DATABASES  
```SHOW TABLES in {DATABASENAME};``` - Show a list of tables in a particular DATABASE

```USE {DATABASENAME};``` - This chooses which database you want to use by default  
```SHOW TABLES;``` - Show a list of tables in the currently selected DATABASE  
```DESCRIBE {TABLENAME};``` - Show a list of the columns and column data types for the table name provided  
```SELECT * FROM {TABLENAME};``` - Selects all records and all columns from the table name provided  

```CREATE DATABASE {DATABASENAME};``` - Create a database  
```CREATE TABLE {TABLENAME} ({OPTIONS});``` -  Create a table on the chosen database (USE {DATABASENAME})  
```INSERT INTO {TABLENAME} ({COLUMNNAME},{COLUMNNAME},{COLUMNNAME}...)```  
```VALUES ({VALUE}, {VALUE}, {VALUE}...);``` - Create a record in the provided table  
<file_sep>/index.php
<?php
session_start();
require_once("connect.php");
require_once("functions.php");
$loggedIn = $_SESSION['loggedin'] ?? "not logged in";
/*
this pulls the text from after the first / in the
url and sets it to an array
*/
$uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH));

/*
this pulls the last item out of the uri array and sets
$thisPagename variabe to that item
*/
$thisPagename = array_pop($uriSegments);

// this sets the pagename if none is provided in the url
if ($thisPagename == ""){
  $thisPagename = "index";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="main.css">
  <title><?php // makeTitle($conn, $thisPagename); ?></title>
</head>
<body>
  <nav>
    <?php
      makeNav($conn, $loggedIn, $thisPagename);
      echo "<p>" . $loggedIn . "</p>";
     ?>
  </nav>
  <section>
    <div>
        <?php
        //echo $thisPagename;
       makeContent($conn, $thisPagename);
        ?>
    </div>
  </section>
</body>
</html>
<file_sep>/functions.php
<?php
function makeNav($conn, $loggedIn, $thisPagename){
    // This creates the navigation from the navigation table
    $sql = "SELECT pagename, pagetitle FROM test.navigation";
    $result = $conn->query($sql);

    if($loggedIn == "logged in"){
      $loggedInClass = "loggedin";
    } else {
      $loggedInClass = "notloggedin";
    }

    echo "<ul class='" . $loggedInClass . "'>";

    // outputs the rows of results from the navigation,
    // setting the active variable for the page that is active
    while ( $row = $result->fetch_assoc() ) {
      if ( $thisPagename == $row['pagename']){
        $active = "active";
      } else {
        $active = "";
      }

      echo "<li class='" . $active . "''><a href='" . $row['pagename'] . "'>" .$row['pagetitle']. "</a></li>";
    }

    // Outputs the login links
    if ($loggedIn == "not logged in"){
      echo "<li><a href='login.php'>Log In</a></li>";
    } else {
      echo "<li><a href='logout.php'>Log Out " . $_SESSION["realname"] . "</a></li>";
    }
    echo "</ul>";
  } // end of makeNav function

  function makeContent($conn, $thisPagename){
    /*
     this creates the content from the content
     table based on the supplied $thisPagename variable.
     It will then loop through all the matching content
     records and export those individually.
    */
    $sql = "SELECT * FROM test.content WHERE pagename = '$thisPagename'";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()){
      echo $row['contenttitle'];
      echo $row['content'];
    }
  } // end of makeContent

  function makeTitle($conn, $thisPagename){
    /*
     This will echo out the current page title
     from the content table based on the page name
     in $thisPageName
    */
    $sql = "SELECT * FROM test.content WHERE pagename = '$thisPagename'";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    echo $row['pagename'];
  }
<file_sep>/login.php
<?php
session_start();
require_once('connect.php');
require_once('functions.php');

if (!empty($_POST)){
  // if not logged in then check if login was submitted
  $userName = $_POST["username"];
  $passWord = $_POST["password"];
  $sql = "SELECT username, password, realname FROM test.users WHERE username = '$userName' LIMIT 1";

  $result = $conn->query($sql);
  $row = $result->fetch_assoc();

  if ($userName == $row["username"] &&
    md5($passWord) == $row["password"])
    {
      $_SESSION["loggedin"] = "logged in";
      $_SESSION["realname"] = $row["realname"];
      $loggedIn = $_SESSION["loggedin"];
    } else {
      echo "Try again";
      $loggedIn = $_SESSION["loggedin"] ?? "not logged in";
    }
} else {
  $loggedIn = $_SESSION["loggedin"] ?? "not logged in";
}

if ($loggedIn == "logged in") {
  // if "logged in" then redirect to index
  header("Location: http://192.168.33.10/index");
} else {
  // if not logged in show login form
  $thisPage = $_SERVER["PHP_SELF"];
  ?>
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
  </head>
  <body>
    <h1>Login Page</h1>
    <form action='<?php echo $thisPage ?>' method="post">
      Username: <input type="text" name="username"> </br>
      Password: <input type="<PASSWORD>" name="password"> </br>
      <input type="submit" value="login">
    </form>
  </body>
  </html>
<?php
}
<file_sep>/class.php
<?php
class MyClass
{
    public $myname = 'Jason';

    public function printHello()
    {
      return ($this->myname);
    }
}
<file_sep>/secret.php
<?php
session_start();
$loggedIn = $_SESSION['loggedin'];

if ( $loggedIn == "logged in" ) {
  echo "Welcome to the secret page. K. That's it. Bye.";
} else {
  header("Location: http://192.168.33.10/index");
}
<file_sep>/basic-php-instructions.md
# Basic PHP/MySQL Install #
Once you have VirtualBox and Vagrant
running you can use the following
instructions to help you stand up a
basic PHP/MySQL virtual machine.

The following instructions are INSECURE...
which means you should not do this on a
production machine that is live on the
Internets. This is only for your local testing.
```
$ mkdir php-training folder
$ cd php-training
$ vagrant init ubuntu/xenial64
```
Edit the Vagrantfile
        1. set private network to 192.168.33.10
        2. config vm synced folder to:
```
".", "/vagrant", :group => "www-data", :mount_options => ['dmode=775', 'fmode=644']
```
Save and exit Vagrantfile
```
$ vagrant up
$ vagrant ssh
```
Check for and install updates
```
$ sudo apt-get update -y
$ sudo apt-get upgrade -y
```
Install apache2, set up linking for external folder
```
$ sudo apt-get install apache2
$ sudo rm -rf /var/www/html
$ sudo ln -fs /vagrant /var/www/html
```
Install MySQL (sets password as '<PASSWORD>')
```
$ sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password secret'
$ sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password secret'
$ sudo apt-get -y install mysql-server
```
Install PHP
```
$ sudo apt-get install -y php7.0 libapache2-mod-php7.0 php7.0-curl php7.0-cli php7.0-dev php7.0-gd php7.0-intl php7.0-mcrypt php7.0-json php7.0-mysql php7.0-opcache php7.0-bcmath php7.0-mbstring php7.0-soap php7.0-xml php7.0-zip -y
```
Restart Apache, updates again, then exit
```
$ sudo service apache2 restart
$ sudo apt-get update -y
$ sudo apt-get upgrade -y
$ exit
```
Create index.php
```
$ touch index.php
```
Add some code! (using nano or your editor of preference)
```
$ nano index.php
```
Save and then test in your browser at 192.168.33.10

Now create a database to use in our class (password will be '<PASSWORD>', when asked)
```
echo "create database testdb" | mysql -u root -p
```
<file_sep>/test.sql
-- MySQL dump 10.13  Distrib 5.7.27, for Linux (x86_64)
--
-- Host: localhost    Database: test
-- ------------------------------------------------------
-- Server version	5.7.27-0ubuntu0.16.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `content`
--

DROP TABLE IF EXISTS `content`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `content` (
  `id` int(4) unsigned NOT NULL AUTO_INCREMENT,
  `pagename` varchar(30) NOT NULL,
  `contenttitle` varchar(30) NOT NULL,
  `content` mediumtext,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `content`
--

LOCK TABLES `content` WRITE;
/*!40000 ALTER TABLE `content` DISABLE KEYS */;
INSERT INTO `content` VALUES (1,'index','<h2>Welcome</h2>','<p>Welcome to our site. This is an example of a snippet of content. We are going to make more.</p>'),(2,'about','<h2>Our Story</h2>','<p>We started with a simple idea: ball bearings. And then we made more ball bearings. The end.</p>'),(3,'contact','<h2>Contact</h2>','<p>Phone:</p>\r\n<p>559-555-1212</p>\r\n<a href=\"mailto:<EMAIL>\">Bob</a>'),(6,'about','<h2>Our Team</h2>','<p>Our team is the best. They have the bigliest hearts. And their brains are so smart, smarter than the other guys for sure.</p>\r\n<h3>Our President</h3>\r\n<img src=\'http://lorempixel.com/400/200/cats/2\' alt=\'picture of our president, a cat\'>'),(7,'index','<h2>What we do</h2>','<p>What we do is nunya! Actually we do a lot of cool stuff. Did we mention ball bearings? No? Well it\'s the future. Fletch knowsit. You know it. Your Grandma is pretty sure she knows it.</p>');
/*!40000 ALTER TABLE `content` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `navigation`
--

DROP TABLE IF EXISTS `navigation`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `navigation` (
  `id` int(1) unsigned NOT NULL AUTO_INCREMENT,
  `pagename` varchar(30) NOT NULL,
  `pagetitle` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `navigation`
--

LOCK TABLES `navigation` WRITE;
/*!40000 ALTER TABLE `navigation` DISABLE KEYS */;
INSERT INTO `navigation` VALUES (1,'index','Home'),(2,'contact','Contact Us'),(3,'about','About');
/*!40000 ALTER TABLE `navigation` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `users`
--

DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `realname` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `users`
--

LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` VALUES (1,'jason','<PASSWORD>','<NAME>');
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2019-10-16  5:13:42
<file_sep>/connect.php
<?php
// this allows apache to redirect files based on .htaccess
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$servername = "localhost";
$username = "root";
$password = "<PASSWORD>";

// Create connection
$conn = new mysqli($servername,$username,$password);

// Check connection
if ($conn->connect_error) {
  die("No worky " . $conn->connect_error);
} else {
  $success = "It worked!";
}
<file_sep>/my-plugin.php
<?php
/*
* Plugin Name: Custom Directory Plugin
* Description: A custom post type
*  Author: Me
*  Author URI: Https://www.jasonlee.com
*  Version: 1.0
*/

function register_custom_post_type() {
  $args = [
    'labels' => [
      'name' => 'Directory',
      'singular_name' => 'Directory Listing',
      'add_new_item' => 'Add Directory Listing'
    ],
    'public' => true,
    'supports' => [
      'title', 'thumbnail', 'editor' //these are the options for what shows on the page thing
    ],
    'taxonomies' => []
  ];
  // register our Custom directory using the args above
  // the namd of the post type can't be more than 20 characters
  register_post_type('dir_custom_posts', $args);
}

//finding the template file\directory
function custom_include_template_function( $template_path ) {
  if( get_post_type() == 'my_directory'){
    if(is_single()){
      if ($theme_file == locate_template(['single-my_directory.php']){
        $template_path = $theme_file;
      } else {
        $template_path = plugin_dir_path(__FILE__) . '/single-my_directory.php');
      }
    }
  }
  return $template_path;
}

//add_action ( when, what )
add_action('init', 'register_custom_post_type')

//filters change information
add_filter('template_include', 'custom_include_template_function')
