Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We’ll take the associative array created from the results of our database query and use it to populate our library. We’ll talk about how separation of concerns allows us to swap out the data our site is using, without having to change much code.
Another Way to Prove Database Usage
If you would like another way to test that your content is actually being pulled from the database, comment out the $catalog = $results
line.
Links
Example Code
$catalog = $results->fetchAll(PDO::FETCH_ASSOC);
We have the code required to retrieve
the item information from the database.
0:00
It's time to use those results
to display our media items.
0:04
Unfortunately, changing the site to use
a database instead of a simple array
0:08
is going to require us to
rewrite everything from scratch.
0:13
Gotcha, actually it's not,
0:17
the original data.php file contains
an array of catalog information.
0:19
All we need to do is update that file
0:25
to pull the catalog information
from the database instead.
0:28
After that we can start
updating our functions to make
0:32
better use of the data from the database.
0:35
In the functions file we have the code for
0:38
manipulating the catalog information and
adding it to our site.
0:40
The data.php file
contains the data itself.
0:44
This is an example of the programming
concept known as separating concerns.
0:48
As you do more programming,
you'll hear more and
0:53
more about the concept
of separating concerns.
0:55
One of the main benefits of separating
concerns is that you can make
0:58
major changes in one area, without
having any effect on the other areas.
1:02
For our listing pages,
we separate our concerns so
1:08
that all we have to
change is our data file.
1:11
>> Back in the workspace associated
with this video, let's change data.php.
1:14
By changing this file we won't have to
rewrite any of the path to our includes.
1:20
Will include the connection.php file and
1:24
then we'll comment out
the rest of this code.
1:29
This will leave it here for reference.
1:38
Now let's go back to connection.php.
1:40
Our code creates a pdo object
that connects to the database.
1:44
It runs a query against that database and
1:48
displays the result set as
an array to the screen.
1:50
Let's change this last line, so
that instead of displaying the results
1:54
to the screen it loads it into
a variable called catalog.
1:58
The original array of catalog information,
2:05
was also stored in
a variable name catalog.
2:07
So by storing the results of our
database query in the same variable
2:10
our original code will still work.
2:14
Now let's go take a look at the home page.
2:16
It looks exactly as it did, which is good.
2:19
We haven't made any changes
to the function of the code.
2:22
All we're doing is generating the catalog
array from the database using pdo.
2:25
If I click on Books in the navigation,
we'll see our Books page, Movies,
2:30
Music and our Full Catalog.
2:35
All of those pages currently
call functions that in turn
2:39
use the catalog array.
2:42
We were able to change the source of
the data displayed on these pages
2:44
by simply putting the results of our
database query into a catalog array,
2:48
instead of recreating that array manually.
2:52
Just to make sure that
we're using the database,
2:55
let's change our connection
string to break the connection.
2:57
If our code is working properly,
this will show an error message.
3:06
This looks right.
3:09
Our site is using our code that
connects to the database and
3:10
when the string is wrong,
we're getting the error message.
3:13
Let's change that back.
3:16
And everything looks good.
3:21
>> Before we move on.
3:24
I want to point out that we will
be improving the performance
3:25
on some of these pages shortly.
3:28
Currently our code retrieves
all the items in the catalog.
3:30
This makes our code do
extra unnecessary work.
3:34
For example to display only
the books in the catalog.
3:38
The code retrieves the entire catalog,
books, music and movies,
3:41
then loops through the entire
catalog to display just the books.
3:46
This type of filtering is the type
of work at which databases excel.
3:52
We'll eventually write additional queries
to make our database handle that load.
3:57
And in turn, this makes our site faster.
4:02
Before we're done with this project, we're
going to set up multiple functions and
4:05
multiple queries to the database
to pull specific data,
4:09
instead of the entire catalog.
4:12
But before we do that,
4:14
let's modify the code we just pasted to
make things cleaner and easier to reuse.
4:16
You need to sign up for Treehouse in order to download course files.
Sign up