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 Build a Simple PHP Application Working With Functions Creating the Shirt Display Function

getting an error saying undefined function but did exactly as done in the video please help

when i try to go to shirts page it showing an error saying that the function is undefined. when i put the whole function in shirts page its working but again when i put it in the products page its giving an error

this is the code in the shirts.php page

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

    <div class="section shirts page">

        <div class = "wrapper">
            <h1>Mike&rsquo;s 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>

and this is the function in products page

products.php
<?php

function get_list_view_html($product_id, $product) {

    $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;
}

Hi Ahmed,

Can you post your entire "products.php" file with the get_list_view_html function in there? Basically, post what you have when it's not working.

It sounds like the file is being included properly because you say you can see the products when that function is placed directly in the shirts page.

My guess would be that you have an error of some kind in the placement of that function in "products.php"

this is the funtion which is on top of the page in products.php

<?php

function get_list_view_html($product_id, $product) {

    $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;
}?>
<?php 

and below this are the items array

I'm not seeing a problem there.

Just to be clear, when you put the function in shirts.php you still have your products array inside products.php, correct?

And that all works properly meaning the include must be working correctly?

Can you post the exact error message you're getting?

Have you tried downloading the project files for this video and running that in another folder and then comparing your 2 files with Randy's and trying to spot any differences?

I reviewed the video and I know that Randy only has 1 php block in "products.php" whereas you have 2 and he includes the "products.php" file at the top of "shirts.php" but those 2 things should not make a difference.

I downloaded the project files for this course and I made those changes to match the code you've shown and it still worked properly.

4 Answers

Make double sure that you're including the file you think you're including.

I would throw this:

<?php

echo "you have arrived";
exit;

at the top of products.php and see if you get that output before the page loads.

You could make double sure you're checking the right function by playing with function exists:

<?php


if (function_exists('function_name')) {
    echo "This function exists! Yay!";
} else {
    echo "something is totally wrong";
}
exit;

when i checked if the function exists by having it in the shirts.php page its showing as not having that function but i dont understand still cause its exactly as how the video shows yet i dont get an output. but if i include the function within the shirts page its showing up correctly. if the products.php page was not included it shud not show the items in the products.php once i include the function within the shirts page right?

For a bug as strange as this one, it's always worth checking everything! So I would still make sure you're hitting the right file.

You're not defining the function after an exit; or something?

it's a tricky one! Is your code on github?

i dont use github i just started from this site..

How are you getting on with this one?