COMP3512

winter 2024

lec-php-06

Future Future

RECALL-01

<?php
if ($_SERVER['REQUEST_METHOD'] === "GET") {
    if (isset($_COOKIE['first-name']) && $_COOKIE['first-name'] !== "n/a") {
        $msg = "Welcome back, {$_COOKIE['first-name']}!";
    } else {
        $msg = "Hi there!";
        setcookie('first-name', 'n/a');
    }
}

require 'hello.view.php';

A user goes to hello.php for the first time.

<body>
    <h1><?= $msg ?></h1>
</body>

hello.php

hello.view.php

  1. What special thing will be in the response from the web server?
  2. What will be displayed in the browser?
<?php
if ($_SERVER['REQUEST_METHOD'] === "GET") {
    if (isset($_COOKIE['first-name']) && $_COOKIE['first-name'] !== "n/a") {
        $msg = "Welcome back, {$_COOKIE['first-name']}!";
    } else {
        $msg = "Hi there!";
        setcookie('first-name', 'n/a');
    }
}

require 'hello.view.php';

A user comes BACK to hello.php.

<body>
    <h1><?= $msg ?></h1>
</body>

hello.php

hello.view.php

  1. What special thing will be in the request from their browser?
  2. What could they do in their browser's devtools to make the message "Welcome back, doofus!" appear in the browser?

RECALL-01, cont

<?php

session_start();

$_SESSION['user'] = $_GET['user'] ?? 'unknown';

if ($_SESSION['user'] === "betty") {
    header("Location: here.php");
} else {
    header("Location: there.php");
}

exit();

RECALL-02

landing.php

A user goes to  landing.php?user=bobby for the first time.

  1. What special thing will be in the response from the web server?
  2. What special thing will be in the /tmp directory of the web server computer?
  3. What page will the user by redirected to?

Currently, we're using hard-coded arrays to hold important info for our admin pages in The Project.

What 3 arrays did we have again?

Obviously, that's not gonna cut it for our final product!

... and we need our PHP code to "talk" to that database to get data from the tables.

...which means that we need a database to hold those tables...

We're going to need the data in those arrays to be in database tables...

Sounds like we need a Plan!

Here's The Plan

  1. We're going to briefly talk about class/object syntax in PHP, because our Project code will use objects to "talk" to the database. Rabbits will be involved.
  2. Then I'll show you how to use the Database Client extension that's on our Codespace to create the database, tables, and data we need for the project.
  3. Finally, we'll play around with the extension a bit and see how to view and alter the data in our tables.

Next week, we'll actually start writing DB code...just in time for Milestone 4 folks to use it the week after.

let's talk about these things today:

​◉ We're gonna make rabbits. How exciting!

Classes

◉ Wait...my Codespace has a database server on it?!?
◉ How do I connect to that server?
◉ How do I create a database, tables, and data on the server?
◉ How do I add/delete/modify data from tables?

MariaDB on Our Codespaces

Classes in PHP

Brought to you by Rabbit.

BUT FIRST...

This course has 3 prerequisites, right?

ADDRESSING PREREQUISITE ANXIETY

Does this mean you have to remember all the things from all these courses?!?!

  1. COMP2511
  2. COMP2521
  3. COMP2503

Are you kidding?!?

Hell, no.

  • ...newing up objects.
  • ...calling methods on objects.
  • ...reading basic SQL queries.
  • ...writing basic SQL queries.

I am just assuming you are kinda comfortable...

That's it. Breeeeeeathe.

Let's talk Rabbits.

Feel like extra practice?

Create an Order class into lab-02!

The price calculation can be moved into the class.

And the price-per-can would make a wonderful static property.

Of course you do!

Do you have to use classes in The Project?

Nope. But go ahead if you want to.

BRAIN BREAK

MariaDB on Our Codespace

Wait...my Codespace has a database server on it?!?

You betcha!

See?

How do I connect to that server?

Click this...

...to show this.

Click it!

Click this...

Put a reasonable name in here.

This should be 'mariadb'.

Choose MariaDB.

Your gateway to database wonder.

How do I create a database, tables, and data on the server?

We can do this sort of thing manually...but I'd rather use a script!

Click this...

...which opens this.

Script results show up here. Green is good. Red, not so much.

Click on the refresh icon.

Sweet!

How do I add/delete/modify data from tables?

I'd just get in here a play around. It's pretty intuitive, tbh.

lec-php-06

By Jordan Pratt

lec-php-06

classes | DBs in Codespaces

  • 227