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 can use a custom function to ensure that our items are displayed consistently across all the list pages in our store. In this video, we’ll create that function and talk about where to place the code. We’ll also talk about concatenation, the process of chaining these values together in a chain.
We're now ready to write a custom function
that we can use to display the list view
0:00
of an item in both index.php and
in catalog.php.
0:04
It's a good idea to name
things in a descriptive way
0:09
instead of just making it something
short and easy to reference in our code.
0:12
This way when someone else,
even our future selves,
0:16
reads this code later,
it's easy to understand what's happening.
0:19
Let's get started.
0:23
We'll name this function get_item_html.
0:25
For the parameters,
we'll want to pass in the item ID, and
0:27
the interior array for the single item.
0:32
You can include multiple parameters for a
function by separating them with a comma.
0:35
In this new function,
we'll want to build a string
0:39
with the HTML needed to display
the item in our list view.
0:42
We'll then want the function to pass
back the HTML as the return value.
0:46
Now, where should we put this function?
0:52
We can't put it in catalog.php because we
also need it to be available in index.php.
0:54
We'll need to put this in an include
file so that both pages can access it.
1:00
Since this file will contain functions,
1:06
let's create a new include file named
functions.php in our inc folder.
1:08
We open up our PHP code and
add our new function.
1:14
Get item HTML.
1:21
We include our parameters, ID and
item, and open and close our function.
1:23
We already have the code that builds the
HTML output for a single item in the list.
1:31
It's over in the catalog.php
file right now.
1:37
But we need to move it over
here into our function so
1:40
that other files can access it.
1:43
In catalog.php,
the code we need is right here.
1:45
So let's copy this and
paste it into our function.
1:50
This works, but it's better with functions
like this to return the HTML to the main
1:55
code as a piece of text instead of
displaying it straight to the screen.
1:59
This gives our main code a little bit more
flexibility when calling the functions.
2:03
So instead of echo,
let's assign this string to a variable.
2:08
Let's call it output,
equals, and the string.
2:13
Let's clean this up a little bit and
then return our output.
2:17
Since we moved the code
out of catalog.php,
2:27
we need to replace it with
the code that calls this function.
2:29
In catalog.php we want to echo
2:32
get item html.
2:38
We want to pass the ID and the item.
2:44
Now we need to modify our
foreach loop to get our item ID.
2:49
The ID is the key for the single item
we have loaded into our variable.
2:55
So we add ID, equals greater than and
then our item.
2:59
Just like we used when
we created our array.
3:06
For each item now, as we loop through
them one at a time, we pass the item
3:09
information into our function and
get back the HTML for that item.
3:12
We then echo out the return
value to the screen.
3:17
We haven't included our function page yet,
so let's do that now.
3:20
Up at the top we'll include
3:23
inc/functions.php and
3:28
save this file.
3:33
The catalog page hasn't changed much.
3:37
It's working essentially the same
way as it was before, but
3:39
we now have the code in a function
that we can use on other pages.
3:42
The page that we want to call
this function on is index.php.
3:46
I'll copy this whole foreach loop and
paste it into our index file.
3:50
We'll replace the list items with
3:58
the call to our function
within our foreach loop.
4:03
We need to add the include for
4:11
our data .php and our functions .php.
4:16
Now let's save this page and
take a look at it in our browser.
4:22
Now when we refresh the page
you can see how all 12 items
4:29
are showing on the homepage
just like the catalog page.
4:34
However, on the homepage, we only want
to display four random selections.
4:38
Awesome work.
4:45
We just finished creating
our first custom function.
4:45
Next, we'll check out a built in php
function to choose those four random items
4:48
that we display as
suggestions on our home page.
4:53
You need to sign up for Treehouse in order to download course files.
Sign up