Filtering by Category3:07 with Alena Holligan
We're going to add a second filter, to filter by category. We're also going to make it easier to add more filters as we go. Changing reports to fit the current needs of the company is an important tool for managing data.
switch() statements are similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.
Reminder: Leaving PARAM_INT will remove all non integers, thereby looking for a blank category instead of the category selected.
Filtering is really helping us clean up our report. 0:00 We're going to add a second filter to filter by category, but 0:03 we're also going to make it easier to add more filters as we go. 0:07 Let's start by updating our filter dropdown in reports.PHP. 0:12 Add an optgroup for projects before we add the Category. 0:16 Now we can add the Category optgroup. 0:31 Each option value will start with category followed by a colon. 0:42 Billable, Charity 0:59 and personal. 1:08 Next we need to modify our function again. 1:13 We could add an else if, but 1:20 since we're going to add more filters, 1:23 let's change this to a switch statement instead. 1:29 Now we can add the new case. 1:44 Case category. 1:46 We want our WHERE statement to equal WHERE category= placeholder. 1:52 Finally, our bind value is expecting an int, but the category will be a string. 2:04 If we pass the string category to the bind value method that includes the filter for 2:10 int, the method will remove all non-integer values from the category, 2:16 leaving the category value empty. 2:21 This is not what we want at all. 2:24 If we want to be very specific, 2:26 we could apply the case statement to our bind values as well. 2:28 But since we're already filtering this data, 2:32 I think that just adds a level of complexity that's not really needed. 2:34 Since the third parameter is optional, 2:38 let's leave that off. 2:43 Let's preview the report page in the browser again, This time, 2:49 select a category and run the report. 2:55 Now we see only the tasks assigned to projects that are in a certain category. 3:02
You need to sign up for Treehouse in order to download course files.Sign up