Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

evanfinch
evanfinch
6,049 Points

Refactoring the Codebase - Extra Credit

Somebody can just explain me how to do that, please?

I'm stuck with it!

Near the top of our shirt.php file, we have some code that determines if we have a valid shirt ID. This code would make better sense in the model.

1.Create a function in the model called get_product that receives one argument (a shirt ID).

2.Put the logic that determines if the shirt ID is valid or not inside that function.

3.If the shirt ID is valid, have the function return the array of information about the shirt; if it's not, have it return an empty array.

4.Modify the controller code to call this function.

5.Use a native PHP function like isset() or empty() in the controller code to determine if a valid shirt was returned from the model.

3 Answers

geoffrey
geoffrey
28,724 Points

It's one month old, but here is what I did, and that seems to work.

<?php
// in the product page, the model

     function get_products($shirtId){

        $products = get_products_all();
        $selectedShirt = array();

        if(isset($shirtId)){
            $product_id = $shirtId;
            if(isset($products[$product_id])){
                $selectedShirt= $products[$product_id];
                //var_dump($selectedShirt); // troubleshooting code
        }
    }
    if(!array_key_exists($shirtId,$products) || empty($shirtId)) {
          header('Location:'.BASE_URL.'index.php');
          exit;
        }

    return $selectedShirt;
}

?>
<?php 

// here is my shirt.php page

require_once("../inc/config.php");
require_once(ROOT_PATH."inc/products.php");
$products = get_products_all();
$section = "shirts";
$pageTitle = $product["name"];
include(ROOT_PATH."inc/header.php");
$shirtId = $_GET["id"];
$product = get_products($shirtId);




?>

        <div class="section page">

            <div class="wrapper">

                <div class="breadcrumb"><a href="<?php echo BASE_URL;?>shirts/">Shirts</a> &gt; <?php echo $product["name"]; ?></div>

                <div class="shirt-picture">
                    <span>
                        <img src="<?php echo BASE_URL . $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">
                    </span>
                </div>

                <div class="shirt-details">

                    <h1><span class="price">$<?php echo $product["price"]; ?></span> <?php echo $product["name"]; ?></h1>

                    <form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
                        <input type="hidden" name="cmd" value="_s-xclick">
                        <input type="hidden" name="hosted_button_id" value="<?php echo $product["paypal"]; ?>">
                        <input type="hidden" name="item_name" value="<?php echo $product["name"]; ?>">
                        <table>
                        <tr>
                            <th>
                                <input type="hidden" name="on0" value="Size">
                                <label for="os0">Size</label>
                            </th>
                            <td>
                                <select name="os0" id="os0">
                                    <?php foreach($product["sizes"] as $size) { ?>
                                    <option value="<?php echo $size; ?>"><?php echo $size; ?> </option>
                                    <?php } ?>
                                </select>
                            </td>
                        </tr>
                        </table>
                        <input type="submit" value="Add to Cart" name="submit">
                    </form>

                    <p class="note-designer">* All shirts are designed by Mike the Frog.</p>

                </div>

            </div>

        </div>

<?php include(ROOT_PATH."inc/footer.php"); ?>

Here a simple solution

In my products php file

function get_product($shirtID) {

        $productItem = get_products_all();    

       if (isset($productItem[$shirtID])) {

           $chosenproduct = $productItem[$shirtID];

       }
        else {
           header("Location: " . BASE_URL . "shirts/");
            exit;
        }

    return $chosenproduct;
}

In my shirt.php file

<?php 

require_once("../inc/config.php");
require_once(ROOT_PATH . "inc/products.php");
$product = get_product($_GET["id"]);


$section = "shirts";
$pageTitle = $product["name"];
include(ROOT_PATH . "inc/header.php"); 
?>

        <div class="section page">

            <div class="wrapper">

                <div class="breadcrumb"><a href="<?php echo BASE_URL; ?>shirts/">Shirts</a> &gt; <?php echo $product["name"]; ?></div>

                <div class="shirt-picture">
                    <span>
                        <img src="<?php echo BASE_URL . $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">
                    </span>
                </div>

                <div class="shirt-details">

                    <h1><span class="price">$<?php echo $product["price"]; ?></span> <?php echo $product["name"]; ?></h1>

                    <form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
                        <input type="hidden" name="cmd" value="_s-xclick">
                        <input type="hidden" name="hosted_button_id" value="<?php echo $product["paypal"]; ?>">
                        <input type="hidden" name="item_name" value="<?php echo $product["name"]; ?>">
                        <table>
                        <tr>
                            <th>
                                <input type="hidden" name="on0" value="Size">
                                <label for="os0">Size</label>
                            </th>
                            <td>
                                <select name="os0" id="os0">
                                    <?php foreach($product["sizes"] as $size) { ?>
                                    <option value="<?php echo $size; ?>"><?php echo $size; ?> </option>
                                    <?php } ?>
                                </select>
                            </td>
                        </tr>
                        </table>
                        <input type="submit" value="Add to Cart" name="submit">
                    </form>

                    <p class="note-designer">* All shirts are designed by Mike the Frog.</p>

                </div>

            </div>

        </div>

<?php include(ROOT_PATH . "inc/footer.php"); ?>
Kaitlyn Rich
Kaitlyn Rich
3,028 Points

Hi Evanfinch, can you post the code you're working with in this example too? Thanks!