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

Timothy Boland
Timothy Boland
18,237 Points

Working With Functions - Extra Credit

Wow...this is a wild one...Im dying to see other peoples solutions to this Extra Credit...Im guessing, I would need to change how the form css is designed on the Home Page and Shirts Listing page in order to pull this off. Would it be necessary to finish the CSS foundations badge before tackling this?

It also looks like i should move the Add To Cart functionality into its own function and call it from within get_list_view_html() function.

Am I thinking about this right?...or is it alot simpler than I think it is?

3 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I think it might be a little simpler than you're thinking it is. You would at least need to add a form, a select element, and a submit button to the get_list_view_html function. I'd recommend adding the elements first, just to get the functionality working; you should be able to get it working without changing any CSS.

After you do that, you'll have two issues that it sounds like you are already thinking about:

  • Styling: You will need to add new styles for the form on this page, and you'll have to do it in a way that doesn't break the design on the Shirt Detail page. The more CSS you know, the better you'll be able to get that working.
  • Duplicated Code: The select element and the hidden fields will need to be identical on the Shirt Detail page and in this function. There are ways to solve that, but I wouldn't worry too much about that right now. (I talk a lot more about avoiding code duplication in the next Enhancing A Simple PHP Application project.) For this extra credit assignment, feel free to copy and paste just the code you need from shirt.php into this function.

Does that help?

Timothy Boland
Timothy Boland
18,237 Points

absolutely...thanks again.

I put the following in my function

<?php 
function get_list_view_html($product_id, $product) {

   foreach($product["sizes"] as $size) {
    $output = "";

    $output = $output . "<li>";
    $output = $output . '<a href="shirt.php?id=' . $product_id . '">';
    $output = $output . '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
    $output = $output . "<p>View Details</p>";
    $output = $output . "</a>";
    $output = $output . '<select name="shirtlist" form="buy">';
    $output = $output . '<option value="'. $size .'">"' . $size . '"</option>';
    $output = $output . '</select>';
    $output = $output . '<div class="gridbutton">';
    $output = $output . '<form action="#" id="buy">';
    $output = $output . '<input type="submit" value="Add to Cart" name="submit">';
    $output = $output . '</form>';
    $output = $output . "</div>";
    $output = $output . "</li>";

    }
    return $output;
}

?>

I am getting the submit button (which I gave a new class and created new css for) and I am getting the select drop down but I can't get the different sizes to display for each product. It only shows "Large" for all but I know there's more in the $products array.

What am I doing wrong? I'm just guessing that the foreach loop can be included in this function for this purpose so this may be the issue.

Image screenshot of results here: Screenshot