[Randy Hoyt] Our get product single function has one query right now
that retrieves data from the products table.
Let's add another query to retrieve the size data for the shirt detail pages.
Open up products.php in your text editor
and scroll down to our get products single function.
We'll need to add the sizes to our products array
in the same format they were in before.
Let's scroll back up to remind ourselves what the products array used to look like.
Inside each product we had an array element with a key of sizes.
Inside that element we had an array with one element for each size.
We didn't specify the keys so they must have been indexed 0, 1, and so on.
Let's scroll back down to our get products single function.
We'll want to create that same structure here
so that we don't have to modify any of our controller or view code.
We get the main shirt details here.
If we have a shirt at this point then we also want to look up sizes,
but if no shirt matched the SKU, there's no need to do that.
If product is a boolean false, we could actually just return
that false without proceeding any further.
We could use a conditional to check that, like this—
[typing]
This is called an early return.
Once you have everything you need from the function
you can put a return here to return the value.
You can have multiple return statements in a function
and once one is encountered then the function returns the value
and stops executing any more code in that function.
You'll often see multiple return statements nested in conditional
so that only one of them will be executed.
If a conditional only has one command in it
you don't technically need the curly brackets.
You could technically write this like this—
You technically don't even need the hard return.
This conditional is perfectly valid.
I don't usually like to write conditionals like this
because it can be confusing to scan and see exactly
what code is contained in the conditional and what code is not.
But an early return with only one command inside the conditional
is a perfect case where I would probably do that.
If no product matches the SKU, we do an early return with a boolean false.
If we do have a product though, we need to add sizes to it.
We'll first create that sizes array element as an empty array.
[typing]
Next, we'll want to query the database for a list of sizes.
The code to run this query will look pretty similar to what we've seen before.
We'll start with a try/catch block.
[typing]
We'll want to execute a query, and load the results into a results variable.
We have already included the database file
so can keep using the db variable.
We'll be looking up sizes by the SKU we received as input
so we'll need a prepared statement again.
[typing]
Let me type in the query we looked at last time.
[typing]
To make this query easier to type we'll use aliases.
If you're not familiar with aliases or any of the other syntax in this query,
I'll once again recommend that you check out
the Joining Relational Data Between Tables in SQL badge
in the Database Foundations course.
[typing]
This query is getting really long
so let me show you a trick with PHP strings—
you don't have to put the whole string on one line.
Inside the quotation marks you can press the enter or return key to put in a new line.
Those new lines or hard returns do get sent to MySQL.
SQL queries can have hard returns in them though,
so everything works just fine.
You'll often see longer queries written in PHP
as strings with multiple lines, like this—
[typing]
We'll want to use a placeholder for the SKU again like before.
[typing]
This query will give us all the sizes available for the given SKU.
We'll then need to bind our SKU variable to the placeholder.
[typing]
Then, we can execute the query.
[typing]
This will retrieve all the sizes available for this specific shirt into the results object.