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