0% found this document useful (0 votes)
7 views48 pages

8A.dropDowns Functions WK 8

8A.dropDowns_functions_Wk_8 (2)

Uploaded by

huyxxx567
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views48 pages

8A.dropDowns Functions WK 8

8A.dropDowns_functions_Wk_8 (2)

Uploaded by

huyxxx567
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

COMP 1841 Function Library, email, drop downs

Matt Prichard
Overview
• Recap
• Normalisation
• Adding categories
• Dynamic drop down menus
• Function library
Recap
• Week before last we added the authors table
• We connected it to the jokes table and enforced
referential integrity with foreign keys
• We updated our main select query to deal with
table joins
• We outputted our new join to our jokes view
displaying the jokes author alongside
• We completed the core CRUD system by adding
the edit joke functionality
INTRO TO DATA NORMALISATION
Normalisation is the process by which you determine the
best way to form tables and relationships.
Poor Data Structure – Example 1

• Address not split into components - how to search postcodes?


• Limited number of contacts can be stored - what if more wanted?
• Format of Contact data different
• Lots of empty [null] data
• No identifying primary key
Poor Data Structure – Example 2

Notice the error

Repeating items demonstrated in the Borough field.

6
Poor Data Structure – Example 3

• Duplication of data already in Company table


• Order date in wrong data format [text]
• Item and Qty are repeating items
• Problem with sequencing items in Order Number 6 ( no
item 2)
Poor Data Structure – Example 4

Data separated into different tables.


How would you get statistics on all orders?
Normalisation ‘Rules of Thumb’
1. Put key data (data which exists regardless of other
data) in separate tables

2. Make records uniquely identifiable

3. Use look-up tables (dropdowns) to prevent entry


errors

4. Keep all fields in a table in a 1:1 relationship to one


another (no repeating items within a record)

5. Hold each data item in only one place (no duplication


across tables)

6. Make all fields in a table depend on the key


Normalisation ‘Rule of Thumb 1’
Put key data (data which exists regardless of other data) in separate
tables

Company
Item

This data will exist regardless of whether there are any Orders or
Contacts

10
Normalisation ‘Rule of Thumb 2’
Make records uniquely identifiable

Unique Primary Keys

Company
Company_Id

11
Normalisation ‘Rule of Thumb 3’
Use look-up tables (to prevent entry errors)

Borough

Data Entry through a combo box


Normalisation ‘Rule of Thumb 4’
Keep all fields in a table in a 1:1 relationship to one another (no
repeating items within a table)

Company
Company_Id
Company Name
Address1
Address2
Address3
Postcode
Normalisation ‘Rule of Thumb 5’
Hold data item in only one place (no duplication across tables)

Set Relationships between tables

Contact

Company
Normalisation ‘Rule of Thumb 6’
Make all fields in a table depend on the key

Company
Company_Id
Company Name
Address1
Address2
Address3
Postcode

Each of the fields can only have one value,


dependant on the Company_Id.
ERD
Contact Name
Entity Relationship Diagram
M
Date Number
Quantity
Name 1
1 M 1 M
Company Order OrderItems

Address M M

1 1

Item Items
Borough Borough

The complete data structure


Adding the category table
• Last week I discussed a third table ‘categories’ in order
able to assign jokes to a relevant category.
• I covered the theory of lookup tables to manage many to
many relationships.
• As I said, we can keep this simple for the coursework by
just having each question or post assigned to a single
module.
• We will implement this today by allowing our jokes to be
assigned to a single category to avoid the complexity of
dealing with dynamic radio buttons.
New table - category
CREATE TABLE category (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
categoryName VARCHAR(255)
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB

Populate the table with some data


Alter the joke table. Add new column
‘categoryid’ and make it an index

Populate new column with existing categoryid


In designer view connect the 2 new keys like we did in
week 6 (slide 20-23), this time leave the ‘on delete’
option blank. Why?

We don’t want to be able to


delete a category if it has jokes
attached to it. You could argue
otherwise for your CW
If all has gone
well when
you now add
a new joke
you will have
a drop down
menu forcing
you to pick
an existing
category
Let’s display the category in our
web page
• What are the steps we will need to go through ?
• Update the select query to join the category table.
• Update the jokes.html.php template to display the extra
data.
• Hopefully you are starting to have an idea of where and
how we need to do this ?
jokes.php – update the query

Our (my) updated 3 table join, be very careful to


remember to use table names when accessing
ambiguous columns such as ‘id’
jokes.html.php – update the template
Line 5: simply copy paste line 4 and change joketext to
categoryName, it’s that simple. Now we have a 3 table join.

I have put in a <br /> or line break to help the positioning. You
could take this much further for your coursework.
Lets add a joke…
Last week we had to ‘bodge’ the add joke query to have a hard
coded value for author id on line 9 in addjoke.php

If we run add joke now we get this error …why?

Because we have added the categoryid column to the joke table


we have the same problem. (categoryid is set to not null)
A quick fix…
We could simply hard code a value for the categoryid as in line 9
below. This will work and allow us to add jokes again.

Really we want a better solution where the user can select an


author or category.
Dynamic drop down menus
Basic HTML for a drop down menu
We need one for authors and categories
• Which file should we put them in ?
• addjoke.html.php - the input form template
• After the textarea, before the button, so after line 3
Firstly I have added 2 placeholder drop downs to
addjoke.html.php at lines 5 and 9.

You can worry


about fine tuning
the CSS positioning
later.
Addjoke.php
We need to create the query to get the authors
from the database.
Line 19, include our DB connection
Line 21 assign a ‘select all from author’ query to $sql_a
Line 22 assign the result of the query to $authors to use in our
dropdown loop in the addjoke template
Dynamically populating the authors drop down
We need to have the option part of the drop down in a
loop and we need the author id and author name.
Add lines 7 -11 to addjoke.html.php
Finally in addjoke.php
Line 8: replace hard coded value with a placeholder :authorid
Line 12: bindvalue to the placeholder of the value from the
dropdown menu named authors
Run the system
You should now have a dropdown list coming from the author
table and when you add a new joke you see the corresponding
name in the website
Category drop down
I will leave you work this out, but basically you are duplicating
all of the same steps for authors. Except addjoke.php line 25
use a different variable name such as $sql_c for category so it
does not clash with $sql_a
Finished CRUD system
http://localhost/COMP1841/week8/index.php
Improvements (many J)

Form validation to deal with unselected drop


down menu errors.
BREAK
Building a function library
Why…. we can carry on as we are perfectly well.
But we are starting to duplicate code and break
the DRY rule.
For example we are writing….

$stmt = $pdo->prepare($sql);
$stmt->execute();

every time we interact with the database.


PHP and variable scope
• Any variable that exists in the main script will also be
available and can be changed in an include file.
• Variables created inside a function (including any
argument variables) exist only within that function, and
disappear when the function has run its course.
• Variables created outside the function are completely
inaccessible inside it.
• The only variables a function has access to are the ones
provided to it as arguments.
PHP and variable scope cont…
• In programmer-speak, the scope of these variables is the
function; they’re said to have function scope.
• In contrast, variables created in the main script outside of
any function are unavailable inside functions.
• The scope of these variables is the main script, and
they’re said to have global scope.
• Hopefully you are familiar with these concepts from your
1st year programming module ?
A simple function to count all the jokes
http://localhost/COMP1841/week8/CountAllFunction.php

Why do I get this error message ?


http://localhost/COMP1841/week8/CountAllFix.php

*
The $pdo variable is created in global scope by
DatabaseConnection.php and then passed into the function
totalJokes( ).
The object stored in the global $pdo variable is then copied to the
local variable called $pdo inside the function.
The $pdo variable needs to be passed in as an argument, because
functions only have access to data they are given; they cannot
access variables from the global scope.
Beginning our library
New file called DataBaseFunctions.php in our includes folder

Copy the function to the new file


Edit file - jokes.php
Line 4: Include our new function library.
Line 12: create a new variable $totaljokes and call our new function
remembering to pass $pdo as an argument.
Edit file - jokes.html.php
Line 1: use our $totalJokes variable and add some text

The result in the browser

http://localhost/COMP1841/week8/jokes.php
With images into a table
This is just a starting point, the css and table need tweaking

http://localhost/COMP1841/week8-02/jokes.php
Next week we will look at
expanding the function library to
cover all of our queries
Questions

You might also like