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

Christine Petterssen
Christine Petterssen
920 Points

Working w/ Functions: "undefined variable: product_id"

I have the same code calling the function placed in my index.php file as I do in my shirts.php. The shirts.php file displays fine but the index.php file throws off undefined variable errors.

Notice: Undefined variable: product_id in C:\Users\Christine\Sites\acquia-drupal\shirts4mike\inc\products.php on line 8

Notice: Undefined variable: product in C:\Users\Christine\Sites\acquia-drupal\shirts4mike\inc\products.php on line 9

Notice: Undefined variable: product in C:\Users\Christine\Sites\acquia-drupal\shirts4mike\inc\products.php on line 9

Here is the code in shirts.php

<?php include ('inc/products.php');
?>

<?php 
$pageTitle = "Mikes Full Catalog of Shirts";
$section = "shirts";
include('inc/header.php'); ?>


    <div class="section shirts page">

        <div class="wrapper">
            <h1>Mikes Full Catalog of Shirts</h1>
            <ul class="products">
                <?php foreach ($products as $product_id => $product) { 
                    echo get_list_view_html ($product_id, $product);
                    } 
                ?> 
            </ul>
        </div>
    </div>

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

Here is the code in index.php

<?php 
$pageTitle = "Unique Shirts Made by a Frog";
$section = "home";
include('inc/header.php'); ?>

        <div class="section banner">

            <div class="wrapper">

                <img class="hero" src="img/mike-the-frog.png" alt="Mike the Frog says:">
                <div class="button">
                    <a href="#">
                        <h2>Hey, I&rsquo;m Mike!</h2>
                        <p>Check Out My Shirts</p>
                    </a>
                </div>
            </div>

        </div>

        <div class="section shirts latest">

            <div class="wrapper">

                <h2>Mike&rsquo;s Latest Shirts</h2>
                <?php include ('inc/products.php');?>
                    <ul class="products">
                        <?php foreach ($products as $product_id => $product) { 
                            echo get_list_view_html ($product_id, $product);
                            } 
                        ?> 
                    </ul>

            </div>

        </div>

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

here is the code in products.php file (i am only including first product in array):

<?php

function get_list_view_html($products_id, $products) {

    $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 . "</li>";

    return $output;
}

    $products[101] = array(
        "name" => "Logo Shirt, Red",
        "img" => "img/shirts/shirt-101.jpg",
        "price" => 18,
        "paypal" => "9P7DLECFD4LKE"
    );

1 Answer

Christine Petterssen
Christine Petterssen
920 Points

nevermind, figured it out as soon as i posted it. i had plural variables in my products.php file ($products_id, $products) when they should have been singular ($product_id, $product). Since my shirts.php page still displayed fine (i don't know why), I did not think to look to see if variables were wrong.