Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Code Challenge: Separating Concerns: Models

Hi I'm struggling with the second challenge 2/3 can someone please help me.

Next, let’s move the flavors array into that function. This task has two steps. (1) Move the code that creates the flavors array from [index.php] into the get_all_flavors() function in [flavors.php]. (2) Add code at the end of the function so that it returns the array of flavors back to whatever code calls it.

 function get_all_flavors(){
  $flavors = array();
  return $flavors;
}

12 Answers

You have two files to work with on the challenge. Index.php and flavors.php.

In index.php at the very top, you will see an array called $flavors, with a bit a list of flavors in it. You have to select and cut that entire array, and than open up flavors.php and copy your $flavors array into it. Than, return $flavors.

The code you posted there, there is nothing in your array. You are missing the flavors. Add the flavors from the $flavor array into the the empty parenthesis and you should pass.

Here is step 3 as it worked for me. <?php foreach ($flavors as $flavor) { echo "<li>" . $flavor["name"] . ' +' . $flavor["likes"] . "</li>"; } ?>

I cant believe it. I did that the first time and it did not work. Now when i did it it pass. Thanks again luckily I know I got it right the first time.

No prob! You were correct, you just didn't have your array values! :) Easy oversight!

Now i need help with this code don't know why i struggle so much now. 3/3

Next, let's change [index.php] to call this function. Before the code that displays the list of flavors, call the get_all_flavors() function and assign the return value to a $flavors variable.

I'm a bit stuck on this part, any assistance?

Nvm i got it. :)

Ok i'm stuck at part 3/3, anyone care to elaborate?

yeah i stucked at part 3/3 too and now i solved it and its pretty easy.

1.) there is a great hint in the following question part

Or

2.) the easy way:

assign your function to a variable and return this variable ... thats it.

$a = function abc(); return $a;

In index.php

<?php
require_once('flavors.php');
$flavors = get_all_flavors();
?> 

AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH So CONFUSED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

AREGAGSSDKLDSGagdjsg;ldasgkjadsgdkjasgkjkjakjgsw3w@!@!!!@!!

hey guys how did you pass the part 3?

In index.php

<?php
require_once('flavors.php');
$flavors = get_all_flavors();
?> 

I souldn't answer that kind of questions... :)

Thank you so much! Added it in the wrong place lol.

OOOOPS. I was putting a $ in front of my get_all_flavors call.

<?php
require_once('flavors.php');
$flavors = $get_all_flavors();
?>