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 Enhancing a Simple PHP Application Refactoring the Codebase Separating Concerns: MVC

Separating Concerns MVC - something wrong challenge

Hi guys,

I feel the code is correct but don't know why the answer doesn't accept it.

Bummer! The flavors array does not contain the three most popular flavors. Take a look at the get_flavors_by_likes() function in flavors.php.

I add the line below:

echo "<li>" . $flavor["name"] . " +" . $flavor["likes"] . "</li>";
index.php
<?php

// INCLUDE MODEL FILE
require_once("flavors.php");



// CONTROLLER CODE
$flavors = get_recent_flavors(3);



// VIEW CODE
?><html>
<body>

    <h1>Ye Olde Ice Cream Shoppe</h1>

    <p>We sell amazing flavors of ice cream.</p>

    <h2>Most Popular Flavors</h2>

    <ul>
    <?php
        foreach ($flavors as $flavor) {
            echo "<li>" . $flavor["name"] . " +" . $flavor["likes"] . "</li>";
        }
    ?>
    </ul>

</body>
</html>
flavors.php
<?php

// This function returns all the flavors
// ordered by the date they have been added.
function get_all_flavors() {

    $flavors = array(
        array("name" => "Vanilla", "likes" => 312),
        array("name" => "Cookie Dough", "likes" => 976),
        array("name" => "Peppermint", "likes" => 12),        
        array("name" => "Cake Batter", "likes" => 598),
        array("name" => "Avocado Chocolate", "likes" => 6),        
        array("name" => "Jalapeno So Spicy", "likes" => 3),        
    );

    return $flavors;

}

// This function receives a number as an argument.
// It returns the newest flavors, the most recent
// flavor first. (The number of flavors returned
// corresponds to the number in the argument.)
function get_recent_flavors($number) {

    $recent = array();
    $all = get_all_flavors();

    $total_flavors = count($all);
    $position = 0;

    foreach($all as $flavor) {
        $position = $position + 1;
        if ($total_flavors - $position < $number) {
            $recent[] = $flavor;
        }
    }

    return array_reverse($recent);

}

// This function receives a number as an argument.
// It returns the most popular flavors based on
// the number of likes, the most popular flavor
// first. (The number of flavors returned corresponds
// to the number in the argument.)
function get_flavors_by_likes($number) {

    $all = get_all_flavors();

    $total_flavors = count($all);
    $position = 0;

    $popular = $all;
    usort($popular, function($a, $b) {
        return $b['likes'] - $a['likes'];
    });

    return array_slice($popular, 0, $number);

}

?>

2 Answers

Alex Heil
Alex Heil
53,547 Points

hey Salman Ak , the problem is coming from the controller code - let's take a look at the bummer message and then compare it with your current code: Bummer! The flavors array does not contain the three most popular flavors. Take a look at the get_flavors_by_likes() function in flavors.php.

so this tells us 2 things:

  1. we're not outputting the most popular flavors
  2. it gives a hint to the get_flavors_by_likes() function

now let's look at the code you have here:

// CONTROLLER CODE
$flavors = get_recent_flavors(3);

this line of code currently returns the 3 recent flavors, but not the 3 most popular one's. I guess you already see the problem now and know how to fix it ;)

so if we change this line to use the function that was hinted to us everything should be fine, like this:

// CONTROLLER CODE
$flavors = get_flavors_by_likes(3);

hope that helps and have a nice day ;)

Hi Alex Heil,

Ah I see now, bummer message didn't mention regarding controller code, that's why I was struggled to find where it come from.

Thank you!!