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 Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Creating the Display Function

john paul
john paul
3,191 Points

My browser is generating many warnings

I have checked the syntax many times but I am still getting the following errors Warning: include(inc/data.php): failed to open stream: No such file or directory in /home/treehouse/workspace/index.php on line 2

Warning: include(): Failed opening 'inc/data.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /home/treehouse/workspace/index.php on line 2

Warning: include(inc/functions.php): failed to open stream: No such file or directory in /home/treehouse/workspace/index.php on line 3

Warning: include(): Failed opening 'inc/functions.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /home/treehouse/workspace/index.php on line 3

Notice: Undefined variable: catalog in /home/treehouse/workspace/index.php on line 17

Warning: Invalid argument supplied for foreach() in /home/treehouse/workspace/index.php on line 17


Here is my index.php file '''php <?php include("inc/data.php"); include("inc/functions.php");

$pageTitle = "Personal Media Library"; $section = null;

include("inc/header.php"); ?> <div class="section catalog random">

        <div class="wrapper">

            <h2>May we suggest something?</h2>

                            <ul class="items">
                        <?php 
                foreach($catalog as $id => $item) {
                    echo get_item_html($id,$item);
                }
                ?>                          
            </ul>

        </div>

    </div>

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


My functions.php file '''php <?php function get_item_html($id,$item) { $output = "<li><a href='#'><img src='" . $item["img"] . "' alt='" . $item["title"] . "' />" . "<p>View Details</p> . "</a></li>"; return $output; } '''_______________________________________ Catalog.php file '''php <?php include("inc/data.php"); include("inc/functions.php");

$pageTitle = "Full Catalog"; $section = null; // a variable created to handle underlining of nav elements when active. null is used for the full catalog as we do not want it underlined.

if (isset ($_GET["cat"])) { //if the cat query exists execute this code. if it doesnt then the default is "Full Catalog". if ($_GET["cat"] == "books") { $pageTitle = "Books"; // $pageTitle is the variable used in the files for the Title tag $section = "books"; } else if ($_GET["cat"] == "movies") { $pageTitle = "Movies"; $section = "movies"; } else if ($_GET["cat"] == "music") { $pageTitle = "Music"; $section = "music"; } }

include("inc/header.php"); ?>

<div class="section catalog page">

<div class="wrapper">

<h1><?php echo $pageTitle; ?> </h1>

    <ul class="items">
        <?php 
        foreach($catalog as $id => $item) {
        echo get_item_html($id,$item);

} ?> </ul>

 </div>

</div>

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



my data.php file '''php <?php $catalog[101] = [ "title" => "A Design Patterns: Elements of Reusable Object-Oriented Software", "img" => "img/media/design_patterns.jpg", "genre" => "Tech", "format" => "Paperback", "year" => 1994, "category" => "Books", "authors" => [ "Erich Gamma", "Richard Helm", "Ralph Johnson", "John Vlissides" ], "publisher" => "Prentice Hall", "isbn" => '978-0201633610' ]; $catalog[102] = [ "title" => "Clean Code: A Handbook of Agile Software Craftsmanship", "img" => "img/media/clean_code.jpg", "genre" => "Tech", "format" => "Ebook", "year" => 2008, "category" => "Books", "authors" => [ "Robert C. Martin" ], "publisher" => "Prentice Hall", "isbn" => '978-0132350884' ]; $catalog[103] = [ "title" => "Refactoring: Improving the Design of Existing Code", "img" => "img/media/refactoring.jpg", "genre" => "Tech", "format" => "Hardcover", "year" => 1999, "category" => "Books", "authors" => [ "Martin Fowler", "Kent Beck", "John Brant", "William Opdyke", "Don Roberts" ], "publisher" => "Addison-Wesley Professional", "isbn" => '978-0201485677' ]; $catalog[104] = [ "title" => "The Clean Coder: A Code of Conduct for Professional Programmers", "img" => "img/media/clean_coder.jpg", "genre" => "Tech", "format" => "Audio", "year" => 2011, "category" => "Books", "authors" => [ "Robert C. Martin" ], "publisher" => "Prentice Hall", "isbn" => '007-6092046981' ]; //Movies $catalog[201] = [ "title" => "Forrest Gump", "img" => "img/media/forest_gump.jpg", "genre" => "Drama", "format" => "DVD", "year" => 1994, "category" => "Movies", "director" => "Robert Zemeckis", "writers" => [ "Winston Groom", "Eric Roth" ], "stars" => [ "Tom Hanks", "Rebecca Williams", "Sally Field", "Michael Conner Humphreys" ] ]; $catalog[202] = [ "title" => "Office Space", "img" => "img/media/office_space.jpg", "genre" => "Comedy", "format" => "Blu-ray", "year" => 1999, "category" => "Movies", "director" => "Mike Judge", "writers" => [ "William Goldman" ], "stars" => [ "Ron Livingston", "Jennifer Aniston", "David Herman", "Ajay Naidu", "Diedrich Bader", "Stephen Root" ] ]; $catalog[203] = [ "title" => "The Lord of the Rings: The Fellowship of the Ring", "img" => "img/media/lotr.jpg", "genre" => "Drama", "format" => "Blu-ray", "year" => 2001, "category" => "Movies", "director" => "Peter Jackson", "writers" => [ "J.R.R. Tolkien", "Fran Walsh", "Philippa Boyens", "Peter Jackson" ], "stars" => [ "Ron Livingston", "Jennifer Aniston", "David Herman", "Ajay Naidu", "Diedrich Bader", "Stephen Root" ] ]; $catalog[204] = [ "title" => "The Princess Bride", "img" => "img/media/princess_bride.jpg", "genre" => "Comedy", "format" => "DVD", "year" => 1987, "category" => "Movies", "director" => "Rob Reiner", "writers" => [ "William Goldman" ], "stars" => [ "Cary Elwes", "Mandy Patinkin", "Robin Wright", "Chris Sarandon", "Christopher Guest", "Wallace Shawn", "André the Giant", "Fred Savage", "Peter Falk", "Billy Crystal" ] ]; //Music $catalog[301] = [ "title" => "Beethoven: Complete Symphonies", "img" => "img/media/beethoven.jpg", "genre" => "Clasical", "format" => "CD", "year" => 2012, "category" => "Music", "artist" => "Ludwig van Beethoven" ]; $catalog[302] = [ "title" => "Elvis Forever", "img" => "img/media/elvis_presley.jpg", "genre" => "Rock", "format" => "Vinyl", "year" => 2015, "category" => "Music", "artist" => "Elvis Presley" ]; $catalog[303] = [ "title" => "No Fences", "img" => "img/media/garth_brooks.jpg", "genre" => "Country", "format" => "Cassette", "year" => 1990, "category" => "Music", "artist" => "Garth Brooks" ]; $catalog[304] = [ "title" => "The Very Thought of You", "img" => "img/media/nat_king_cole.jpg", "genre" => "Jaz", "format" => "MP3", "year" => 2008, "category" => "Music", "artist" => "Nat King Cole" ]; '''

Nedim Becirovic
Nedim Becirovic
9,394 Points

It would be useful if we could see your folder structure. From what error are telling is that you do not have data.php and functions.php inside inc folder. Check path to them from index.php -> include("inc/data.php"); include("inc/functions.php")

3 Answers

Nedim Becirovic
Nedim Becirovic
9,394 Points

As you can see data.php and functions.php are not inside inc folder. If you move them there you will fixed those issues.

john paul
john paul
3,191 Points

Oops !!! how embarrassing... I thought they were, but not so !! Thanks Nedim , all is well now. Many thanks.